-
Notifications
You must be signed in to change notification settings - Fork 0
Dictionary
A Dictionary allows you to store key/value string pairs and look up values be referencing their associated key. Within the dictionary each key must be unique, but the value does not have to be. This means that you may have the same value assigned for multiple keys, but not a single key with multiple values.
Because of how dictionaries work internally, searching for a key in dictionary is faster than you'd search for it in a regular array. The advantage increases with the number of items searched. They are also fast to add or remove items.
A Dictionary has two general properties: sort style and compare style.
Sort style defines whether items are stored sorted or unsorted, based on their keys. The unsorted containers are commonly somewhat faster to search in, but the items will be stored in an undefined order.
Compare style determines whether string keys are compared as case sensitive or case insensitive. For example, in a case-sensitive dictionary the strings "Parameter" and "parameter" will be seen as two different keys, but in a case-insensitive dictionary they will be seen as the same key. Compare style defines both key uniqueness and sorting.
At the moment a dictionary does not let you directly access all of its internal data, but has GetKeysAsArray and GetValuesAsArray functions that will write all items to a dynamic array, which you may parse, print, and otherwise use as you see fit. The order of items in these arrays will match their order within the dictionary, hence if the dictionary was sorted the array will be sorted as well.
To summarize, dictionaries make convenient storage for key/value pairs. If you do not need pairs but rather a sequence of unique values, you may wish to use a Set instead.
Compatibility: Dictionary struct is supported by AGS 3.5.0 and later versions.
static Dictionary* Dictionary.Create(SortStyle sortStyle, StringCompareStyle compareStyle)
Creates a new empty Dictionary of the given properties. If you don't pass any options the Dictionary is unsorted and case-insensitive. Note that you cannot change sorting style and case sensitivity later, you would have to create another Dictionary and then re-add the values there.
Example:
Dictionary* myDictionary = Dictionary.Create();
void Dictionary.Clear()
Removes all items from the dictionary.
bool Dictionary.Contains(String key)
Returns true if the key is in the dictionary, otherwise - false.
Example:
Dictionary* myDictionary = Dictionary.Create();
myDictionary.Set("my-key","my-value");
if(myDictionary.Contains("my-key")){
Display("has my key!");
}
This will add "my-key" key, assign "my-value" to that key and then test whether that key was added successfully.
See Also: Dictionary.Get, Dictionary.Set
String Dictionary.Get(String key)
Gets the value for the given key. Returns null if such a key does not exist.
Example:
Dictionary* myDictionary = Dictionary.Create();
myDictionary.Set("a-key","a-value");
String myValue = myDictionary.Get("a-key");
Here myValue variable will be assigned "a-value" from the dictionary.
See Also: Dictionary.Set
String[] Dictionary.GetKeysAsArray()
Creates a dynamic array filled with keys in the same order as they are stored in the Dictionary.
Returns null if this Dictionary is empty.
Example:
Dictionary* myDictionary = Dictionary.Create();
myDictionary.Set("my-key1","my-value1");
myDictionary.Set("my-key2","my-value2");
String keys[] = myDictionary.GetKeysAsArray();
for (int i = 0; i < myDictionary.ItemCount; i++)
Display("#%d: %s", i, keys[i]);
In the above example the keys will be displayed on screen one by one, preceded by their index.
See Also: Dictionary.ItemCount
String[] Dictionary.GetValuesAsArray()
Creates a dynamic array filled with values in the same order as they are stored in the Dictionary.
Returns null if this Dictionary is empty.
Example:
Dictionary* myDictionary = Dictionary.Create();
myDictionary.Set("my-key1","my-value1");
myDictionary.Set("my-key2","my-value2");
String values[] = myDictionary.GetValuesAsArray();
for (int i = 0; i < myDictionary.ItemCount; i++)
Display("#%d: %s", i, values[i]);
In the above example the values will be displayed on screen one by one, preceded by their index.
See Also: Dictionary.ItemCount
bool Dictionary.Remove(String key)
Removes a key/value pair from the dictionary, returns true on success and false if there was no such key.
See Also: Dictionary.Set
bool Dictionary.Set(String key, String value)
Assigns a value to the given key. If the key did not exist then it will be created, otherwise the old value for this key will be overwritten with a new one.
See Also: Dictionary.Get, Dictionary.Remove
StringCompareStyle Dictionary.CompareStyle
Returns the string comparison method for this dictionary, which determines whether its keys are case-sensitive.
See Also: Dictionary.SortStyle
int Dictionary.ItemCount
Gets the number of key/value pairs currently in the dictionary.
SortStyle Dictionary.SortStyle
Returns the key sorting method for this dictionary.
See Also: Dictionary.CompareStyle
Getting Started in AGS
Editor Reference
- Music and sound
- Distributing your game
- Backing up your game
- The text parser
- Translations
- Global variables
- Custom Properties
- Plugins
- Lip sync
- New Game templates
- Debugging features
- Auto-number speech files
- Integration with Windows
- Source Control integration
Engine
Scripting
- Scripting tutorial part 1
- Scripting tutorial part 2
- Pointers in AGS
- Calling global functions from local scripts
- The script header
- String formatting
- Multiple Scripts
- Understanding blocking scripts
- Dynamic Arrays
- Extender functions
- Game variables
- Predefined global script functions
- repeatedly_execute (_always)
- Custom dialog options rendering
- Built-in enumerated types
- Script language keywords
- AudioChannel functions and properties
- AudioClip functions and properties
- Character functions and properties
- DateTime functions and properties
- Dialog functions and properties
- DialogOptionsRenderingInfo functions and properties
- DrawingSurface functions and properties
- DynamicSprite functions and properties
- File functions and properties
- Game / Global functions
- GUI functions and properties
- GUI control functions and properties
- GUI Button functions and properties
- GUI InvWindow functions and properties
- GUI Label functions and properties
- GUI List Box functions and properties
- GUI Slider properties
- GUI Text Box functions and properties
- Hotspot functions and properties
- Inventory item functions and properties
- Maths functions and properties
- Mouse functions and properties
- Multimedia functions
- Object functions and properties
- Overlay functions and properties
- Palette functions
- Parser functions
- Region functions and properties
- Room functions
- Screen functions
- Speech functions and properties
- String functions
- System functions and properties
- Text display / Speech functions
- ViewFrame functions and properties
Working on Legacy games
Upgrading from a previous version
- Upgrading to AGS 2.7
- Upgrading to AGS 2.71
- Upgrading to AGS 3.0
- Upgrading to AGS 3.1
- Upgrading to AGS 3.2
- Upgrading to AGS 3.3
- Upgrading to AGS 3.3.5
- Upgrading to AGS 3.4
- Upgrading to AGS 3.4.1
Legal Notice
Getting in touch