Maps in UnrealScript

In the provided example, Unrealscript is the target language. Here’s the translation of the Go code example focused on maps into Unrealscript, along with appropriate explanations in Markdown format suitable for Hugo.


Maps are Unrealscript’s built-in associative data type, sometimes called hashes or dicts in other languages.

To create an empty map, use the default keyword:

class MyMapExample extends Object;
function CreateEmptyMap()
{
    local map<string, int> MyMap;
    MyMap.default;
}

Set key/value pairs using typical MyMap[key] = val syntax.

function SetValues()
{
    local map<string, int> MyMap;
    MyMap("k1") = 7;
    MyMap("k2") = 13;
    `log("MyMap: " @ MyMap);
}

Printing a map will show all of its key/value pairs.

function PrintMap()
{
    local map<string, int> MyMap;
    MyMap("k1") = 7;
    MyMap("k2") = 13;
    `log("MyMap: " @ MyMap);
}

defaultproperties
{
    MyMap=(("k1", 7), ("k2", 13))
}

Get a value for a key with MyMap[key].

function GetValue()
{
    local map<string, int> MyMap;
    local int v1;
    MyMap("k1") = 7;
    v1 = MyMap("k1");
    `log("v1: " @ v1);
}

If the key doesn’t exist, the zero value of the value type is returned.

function GetValueForNonExistingKey()
{
    local map<string, int> MyMap;
    local int v3;
    MyMap("k1") = 7;
    v3 = MyMap("k3");
    `log("v3: " @ v3); // Output will be 0
}

The builtin Len returns the number of key/value pairs when called on a map.

function LenOfMap()
{
    local map<string, int> MyMap;
    MyMap("k1") = 7;
    MyMap("k2") = 13;
    `log("len: " @ MyMap.Length());
}

The builtin Remove removes key/value pairs from a map.

function RemoveKey()
{
    local map<string, int> MyMap;
    MyMap("k1") = 7;
    MyMap("k2") = 13;
    MyMap.Remove("k2");
    `log("MyMap: " @ MyMap);
}

To remove all key/value pairs from a map, use the Clear method.

function ClearAllKeys()
{
    local map<string, int> MyMap;
    MyMap("k1") = 7;
    MyMap("k2") = 13;
    MyMap.clear();
    `log("MyMap: " @ MyMap);
}

The optional second return value when getting a value from a map indicates if the key was present in the map.

function CheckKeyPresence()
{
    local map<string, int> MyMap;
    local bool prs;
    local int _;
    
    MyMap("k1") = 10;

    (_, prs) = MyMap.Find("k2");
    `log("prs: " @ prs); // Output will be false
}

You can also declare and initialize a new map in the same line with this syntax.

function InitializeMap()
{
    local map<string, int> MyMap;
    MyMap = (("foo", 1), ("bar", 2));
    `log("MyMap: " @ MyMap);
}

The maps package contains a number of useful utility functions for maps.

function MapEqualityCheck()
{
    local map<string, int> Map1, Map2;

    Map1 = (("foo", 1), ("bar", 2));
    Map2 = (("foo", 1), ("bar", 2));

    if (MapsEqual(Map1, Map2))
    {
        `log("Map1 == Map2");
    }
}

function bool MapsEqual(map<string, int> Map1, map<string, int> Map2)
{
    local string key;
    foreach Map1.Keys(key)
    {
        if (Map1[key] != Map2[key])
            return false;
    }
    return true;
}

Now you have learned about maps in Unrealscript, their basic operations, and some utility functions. Next, you can explore more about the language.