1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
Dictionary
==========
A Dictionary is a collection of key-value pairs. Dictionary keys are strings. Their corresponding values can be of any type. To create a Dictionary, use the `{ key_1: value_1, key_2: value_2, ..., key_n: value_n }` syntax rather than the `spawn()` function.
Example:
```
object "Application"
{
dictionary = {
"Surge": 35,
"Neon": 20,
"Charge": 37.5,
"Gimacian": 70
};
state "main"
{
// usage example
Console.print(dictionary["Surge"]); // will print 35
Console.print(dictionary["Neon"]); // will print 20
// will print all entries
foreach(entry in dictionary)
Console.print(entry.key + ": " + entry.value);
// done!
Application.exit();
}
}
```
Output:
```
35
20
Surge: 35
Neon: 20
Charge: 37.5
Gimacian: 70
```
> **Note:**
>
> Whenever you define a dictionary, you spawn a new object. One is advised to **NOT** define dictionaries within states, because the code within states run continuously. Therefore, new objects will be created at every frame of the application, not just once.
Properties
----------
#### count
`count`: number, read-only.
The number of elements in the Dictionary.
Functions
---------
#### get
`get(key)`
Gets the value of the specified key in the Dictionary. Instead of calling `get()` directly, you may equivalently use the `[ ]` operator.
*Arguments*
* `key`: string.
*Returns*
The value corresponding to the specified key, or `null` if there is no such an entry in the Dictionary.
*Example*
```
dict = { "Surge": 10 };
ten = dict["Surge"];
```
#### set
`set(key, value)`
Sets the value of the specified key in the Dictionary. Instead of calling `set()` directly, you may equivalently use the `[ ]` operator.
*Arguments*
* `key`: string.
* `value`: any type.
*Example*
```
dict = { };
dict["Surge"] = 10;
```
#### clear
`clear()`
Removes all entries from the Dictionary.
#### delete
`delete(key)`
Deletes the entry having the specified key.
*Arguments*
* `key`: string. The key of the entry to be removed.
#### has
`has(key)`
Checks if an entry having the specified key belongs to the Dictionary.
*Arguments*
* `key`: string. The key of the entry.
*Returns*
Returns `true` if the Dictionary has such an entry.
#### keys
`keys()`
Gets a collection containing the keys of the Dictionary.
*Returns*
Returns a new [Array](/reference/array) containing the keys of the Dictionary.
#### iterator
`iterator()`
Spawns an iterator.
*Returns*
An iterator to loop through the elements of the Dictionary.
#### toString
`toString()`
Converts the Dictionary to a string.
*Returns*
A string.
|