Skip to main content

Maps

The Map type represents a lookup of keys to values.

Keys can only be Keyable values like numbers, symbols, strings. Only integer numbers can be used as a key. If a floating point number is used as a key, it will be rounded to the nearest integer closest to zero.

Map literals are specified between braces { }, and their values can be read using either a . or [] syntax:

let map = { abc = 123, def = 456 }
map.abc = 789 // equivalent to map[$abc] = 789
map["key1"] = 999 // strings can be used as a key

Fields can be stored in Maps as well:

pub field NumStrikes
map.NumStrikes = 8 // maps can store fields too, just like entities

let myNewMap = {
NumStrikes = 3, // set a field in a map literal
}

You can iterate over each key-value pair in a Map using a for loop. Each key-value pair will be returned in an array, which can be destructured:

let map = { abc = 123, def = 456 }
for [key, value] in map {
// do something with `key` and `value`
}

If you want to only iterate over the keys, you can use the MapKeys function:

let map = { abc = 123, def = 456 }
for key in map.MapKeys {
// do something with `key`
}

If you only want to iterate over the values, you can use the MapValues function:

let map = { abc = 123, def = 456 }
for value in map.MapValues {
// do something with `value`
}