Dictionaries

v3.0

Overview

The Dictionaries module adds serializable dictionary-like functionality to your project. Essentially a class that act like a dictionary, but is not, including key/value pairs and helper methods.
The scripts are fairly simple and straightforward and while it can be used with our other modules, this is a great addition to any project which needs serializable key/value pair functionality.
The latest update changes the values from individual fields to Lists, which provides additional functionality, while preserving the easy of selecting a single entry using a method that does not require an index, and will return the first entry in the list.
The new v3.0 update changes the class name to Dictionaries (previously Dictionary), so it should not conflict with any existing data, but you may want to migrate your data over. Always remember to backup before updating your project.
​
There is a build in migration script which should try to migrate your Dictionary objects automatically.
This is the old video, and will be updated soon for v3.0
The Dictionaries class currently supports the following types:
  • string
  • int
  • float
  • bool
  • Animation
  • Sprite
  • Texture2D
  • AudioClip
  • Vector3
  • Vector2
  • Color
  • GameObject (used as "Prefab")
  • Stat (and StatUid)
  • ItemObject (and ItemObjectUid)
  • ItemAttribute (and ItemAttributeUid)
  • ItemObjectType (string)
  • ItemAttributeType (string)

Setup

New Dictionaries

You can add a new Dictionaries to your class.
using UnityEngine;
using InfinityPBR.Modules;
​
public class DictionariesExample : MonoBehaviour
{
public Dictionaries dictionaries = new Dictionaries("Dictionary Name");
}

Cloning a Dictionaries object

Other Game Modules in our series make use of Dictionaries as well. In some cases, you may wish to clone a Dictionaries object that was previously set up as part of another module. This will create a full clone of the object, which you can then manipulate without affecting the original.
In this example, we will clone the dictionary from an Item.
dictionaries = gameItemObject.dictionaries.Clone();

Getting values

Note that the method used for retrieval is depenent on the type of value you'd like to retrieve.
string key = "Demo Key Name";
​
// Get the first value from the list of values
float floatValue = dictionaries.ValueFloat(key);
string stringValue = dictionaries.ValueString(key);
GameObject prefabValue = dictionaries.ValuePrefab(key);
​
// Get a specific index
Sprite spriteValue = dictionaries.ValueSprite(key, false, 3);
Color colorValue = dictionaries.Value.Sprite(key, false, 2);
​
// Get a random value from the list
string randomString = dictionaries.ValueString(key, true);
Vector3 randomVector3 = dictionaries.ValueVector3(key, true);
​
// Get the entire List of values
List<int> listOfInts = dictionaries.ValuesString(key);
List<Stat> listOfStats = dictionaries.ValuesStat(key);
At runtime, the Stat, ItemObject, and ItemAttribute values will not be serialized. When adding them, the uid of each is also stored, and those are strings which are serialized.
When you call ValueStat(), ValueItemObject(), or ValueItemAttribute() at runtime, you will need to have the Repositories prefabs in your project. The script will automatically look up the object you are seeking by the uid stored, using the Repositories.
Once the object is found, it will be saved for future use at runtime.

Setting, adding, & removing values

string key = "Demo Key Name";
​
// The type will be derived automatically
​
// Set a value into index 0
dictionaries.SetValue(key, someAnimation);
​
// Set a value into a specific index
dictionaries.SetValue(key, someColor, 5);
​
// Set a value ItemObjectType or ItemAttributeType
dictionaries.SetValueItemObjectType(key, objectType);
dictionaries.SetValueItemAttributeType(key, objectType, 9);
​
// Add a value to a List
dictionaries.AddValue(key, someValue);
​
// Adding a new key value while populating the first index of it's value
// public void AddKeyValue(string value, string key, bool allowDuplicates = false)
dictionaries.AddKeyValue(someValue, key); // Type is determined automatically
​
// Remove a value
dictionaries.RemoveValue(key, someValue);
dictionaries.RemoveValueItemObjectType(key, objectType);
dictionaries.RemoveValueItemAttributeType(key, objectType);

Other methods

Please look at the Dictionaries.cs and KeyValue.cs scripts for more methods and to see how they operate.
string key = "Demo Key Name";
​
// Return a clone of a Dictionaries object
Dictionaries clonedDictionaries = dictionaries.Clone();
​
// Return true if a keyValue exists by the provided key
bool keyExists = dictionaries.HasKeyValue(key);
​
// Each KeyValue has a uid. Lookup the index of a keyValue by uid
int keyValueIndex = dictionaries.GetIndexFromUid(uid);
​
// Get the entire KeyValue object
KeyValue keyValue = dictionaries.GetKeyValue(key);
KeyValue otherKeyValue = dictionaries.GetKeyValueByUid(uid);
​
// Rename a key
dictionaries.RenameKey(oldKey, newKey);
​
// Get the first index of a key
int firstIndex = dictionaries.FirstIndex(key);
​
// Add a KeyValue without any values
dictionaries.AddKeyValue(key);
dictionaries.AddKeyValue(key, true); // if true, then duplicates will be allowed.
​