Maps in AngelScript

Maps are associative data types (sometimes called hashes or dicts in other languages).

To create an empty dictionary in AngelScript, use the built-in function dictionary.

void main() {
    dictionary m;
    
    // Set key/value pairs using typical syntax.
    m.set("k1", 7);
    m.set("k2", 13);

    // Printing a dictionary with print will show all of its key/value pairs.
    print("dictionary: " + m + "\n");

    // Get a value for a key with get.
    int v1;
    m.get("k1", v1);
    print("v1: " + v1 + "\n");

    // If the key doesn’t exist, a default zero value is returned.
    int v3;
    bool found = m.get("k3", v3);
    print("v3: " + v3 + ", found: " + found + "\n");

    // The size method returns the number of key/value pairs in the dictionary.
    print("len: " + m.getSize() + "\n");

    // The delete method removes key/value pairs from a dictionary.
    m.delete("k2");
    print("dictionary: " + m + "\n");

    // To clear all key/value pairs from a dictionary, use the clear method.
    m.deleteAll();
    print("dictionary: " + m + "\n");

    // The has method indicates if the key was present in the dictionary.
    bool prs = m.exists("k2");
    print("prs: " + prs + "\n");

    // Declare and initialize a new dictionary in the same line with this syntax.
    dictionary n = {{"foo", 1}, {"bar", 2}};
    print("dictionary: " + n + "\n");

    // Compare two dictionaries using custom comparison.
    dictionary n2 = {{"foo", 1}, {"bar", 2}};
    if (n.equals(n2)) {
        print("n == n2\n");
    }
}

Note that dictionaries appear in the form {k:v, k:v} when printed.

$ run my_script.as
dictionary: {k1=>7, k2=>13}
v1: 7
v3: 0, found: false
len: 2
dictionary: {k1=>7}
dictionary: {}
prs: false
dictionary: {foo=>1, bar=>2}
n == n2