-
Notifications
You must be signed in to change notification settings - Fork 9
Dictionary
Dictionary allows you to store key/value string pairs and look values up by a string key. In the dictionary key must be unique, but value does not have to be. This means that you may have same value assigned for multiple keys, but not same 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 plain array. Its advantage increases with the number of items. They are also fast in adding and removing items dynamically.
Dictionary has two general properties: sort style and compare style.
Sort style defines whether items are stored sorted or unsorted. 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 case-sensitive dictionary strings "Parameter" and "parameter" will be seen as two different keys, but in case-insensitive they will be seen as identical. Compare style defines both key uniqueness and sorting.
At the moment a dictionary itself does not let you see access all of its internal data at once directly, 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 be matching one inside 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 - then look for Set.
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 move 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 successfuly.
See Also: Dictionary.Get, Dictionary.Set
String Dictionary.Get(String key)
Gets value by the key. Returns null if such 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 same order as they are stored in the Dictionary.
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 same order as they are stored in the Dictionary.
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 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, if there was such key already then old assigned value will be overwritten with a new one.
See Also: Dictionary.Get, Dictionary.Remove
StringCompareStyle Dictionary.CompareStyle
Gets if this dictionary 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
Gets the method keys are arranged in 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