Dictionaries Methods

v4.0

Getting and setting values in a Dictionaries object is simple and straightforward. There are also additional helper methods, such as getting a random value from a list of items.

Getting Values

Getting values generally will utilize the Value<T>() method shown below. By default, the method returns the first item in the list -- often it may be the only item. However, you can provide a specific index, or even grab a random value from the list.

public T Value<T>(string key, bool random = false, int index = 0)

// Grab the "Flavor Text" of an item object
FlavorText.text = itemObject.dictionaries.Value<string>("Flavor Text");

// Choose a random portrait from a set of available Sprites
// In this example the "class" of a player is stored as an ItemAttribute
var playerPortrait = selectedClass.dictionaries.Value<Sprite>("Portraits", true);

Safely Getting a Value

Calling Value<T>() may throw an error if the key is not present in the dictionary. You can avoid this, and add any missing Keys automatically, by using ValueSafe<T>().

public T ValueSafe<T>(string key, bool random = false, int index = 0)

Additional "Get" Methods

// Get a KeyValue object
var foundKeyValue = dictionaries.Key(key);

// Count the items
var numberOfKeyValues = dictionaries.Count(key);
var totalKeyValues = dictionaries.Count(); // No key provided, so counts all KeyValues

Get All Values

Get an Array of all values with Values<T>()

Adding Values

You can add new information at runtime.

Add To Value

For int and float values, the AddToValue() method provides an easy way to add and subtract from the current values.

KeyValue Methods

Last updated