-
Notifications
You must be signed in to change notification settings - Fork 9
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.
IMPORTANT: this returns a copy of Dictionary contents existing at the time of function call. If you change Dictionary later, that won't affect previously created key array and you would have to call this function again.
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.
IMPORTANT: this returns a copy of Dictionary contents existing at the time of function call. If you change Dictionary later, that won't affect previously created values array and you would have to call this function again.
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
- New Game templates
- Editor Preferences
- General Settings
- Default Setup
- Colours Editor
- Room Editor
- Character Editor
- Cursor Editor
- Dialog Editor
- Font Preview
- GUI Editor
- Inventory Items Editor
- View Editor
- Sprite Manager
- Music and sound
- Voice speech
- Script Modules
- System limits
- Log Panel
- Plugins
- Other Features
Engine
Scripting
- Scripting Tutorial
- Scripting Language
-
Scripting API
- Script API Overview
- Standard Constants
- Standard Enumerated Types
- Standard Types
- Game variables
- Global arrays
- Global event handlers
- repeatedly_execute / repeatedly_execute_always
- Custom dialog options rendering
- Global functions: general
- Global functions: message display
- Global functions: multimedia actions
- Global functions: palette operations
- Global functions: room actions
- Global functions: screen effects
- Global functions: wait
- AudioChannel functions and properties
- AudioClip functions and properties
- Camera functions and properties
- Character functions and properties
- DateTime functions and properties
- Dialog functions and properties
- DialogOptionsRenderingInfo functions and properties
- Dictionary functions and properties
- DrawingSurface functions and properties
- DynamicSprite functions and properties
- File functions and properties
- Game functions and properties
- 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
- Object functions and properties
- Overlay functions and properties
- Parser functions
- Region functions and properties
- Room functions and properties
- Screen functions and properties
- Set functions and properties
- Speech functions and properties
- String functions
- System functions and properties
- TextWindowGUI functions and properties
- ViewFrame functions and properties
- Viewport functions and properties
- Obsolete Script API
- Event Types
- Key code table
- Audio in script
Legal Notice
Getting in touch
Misc