Generics in UnrealScript

class GenericExamples extends Object;

// As an example of a generic function, SlicesIndex takes
// an array of any comparable type and an element of that
// type and returns the index of the first occurrence of
// v in s, or -1 if not present.
static function int SlicesIndex(array<string> s, string v)
{
    local int i;
    
    for (i = 0; i < s.Length; i++)
    {
        if (v == s[i])
        {
            return i;
        }
    }
    return -1;
}

// As an example of a generic type, List is a
// singly-linked list with values of any type.
struct List
{
    var Element head;
    var Element tail;
};

struct Element
{
    var Element next;
    var string val;
};

// We can define methods on types just like we
// do on regular types.
static function Push(out List lst, string v)
{
    local Element newElement;
    
    newElement.val = v;
    
    if (lst.tail == None)
    {
        lst.head = newElement;
        lst.tail = lst.head;
    }
    else
    {
        lst.tail.next = newElement;
        lst.tail = lst.tail.next;
    }
}

// AllElements returns all the List elements as an array.
static function array<string> AllElements(List lst)
{
    local array<string> elems;
    local Element e;
    
    for (e = lst.head; e != None; e = e.next)
    {
        elems.AddItem(e.val);
    }
    return elems;
}

static function Main()
{
    local array<string> s;
    local List lst;
    local array<string> listElements;
    
    s.AddItem("foo");
    s.AddItem("bar");
    s.AddItem("zoo");
    
    // When invoking functions, we don't have type inference in UnrealScript,
    // so we always need to specify the types explicitly.
    `log("index of zoo:" @ SlicesIndex(s, "zoo"));
    
    Push(lst, "10");
    Push(lst, "13");
    Push(lst, "23");
    
    listElements = AllElements(lst);
    `log("list:" @ class'Engine'.static.JoinArray(listElements, " "));
}

defaultproperties
{
}

UnrealScript doesn’t support generics in the same way as Go, so we’ve had to make some adjustments:

  1. The SlicesIndex function is implemented for strings only, as UnrealScript doesn’t have a concept of “comparable” types.

  2. The List and Element structures are defined with string as the value type, as UnrealScript doesn’t support generic types.

  3. The Push and AllElements functions work with the List structure, but are limited to string values.

  4. In the Main function, we use `log instead of fmt.Println for output.

  5. We use class'Engine'.static.JoinArray to join the array elements into a string for output.

To run this code in Unreal Engine:

  1. Create a new UnrealScript file named GenericExamples.uc in your project’s Classes folder.
  2. Paste the above code into the file.
  3. Compile the script in the Unreal Editor.
  4. You can then call the Main function from other parts of your game code or set up a trigger to execute it.

Note that UnrealScript has been largely replaced by C++ and Blueprint in modern Unreal Engine versions, but this example demonstrates how you might approach similar concepts in the UnrealScript environment.