Slices in UnrealScript

class SlicesExample extends Object;

// Slices are an important data type in UnrealScript, giving
// a more powerful interface to sequences than arrays.

// Unlike arrays, dynamic arrays in UnrealScript are typed only by the
// elements they contain (not the number of elements).
// An uninitialized dynamic array has length 0.
var array<string> s;

function Init()
{
    local array<string> c;
    local array<string> l;
    local array<string> t;
    local array< array<int> > twoD;
    local int i, j, innerLen;

    `log("uninit:" @ s @ s.Length == 0);

    // To create an empty dynamic array with non-zero length, we can use
    // the AddItem function multiple times.
    s.Length = 3;
    `log("emp:" @ s @ "len:" @ s.Length);

    // We can set and get just like with arrays.
    s[0] = "a";
    s[1] = "b";
    s[2] = "c";
    `log("set:" @ s);
    `log("get:" @ s[2]);

    // Length returns the length of the dynamic array as expected.
    `log("len:" @ s.Length);

    // In addition to these basic operations, dynamic arrays support
    // several more that make them richer than static arrays.
    // We can use AddItem to append elements.
    s.AddItem("d");
    s.AddItem("e");
    s.AddItem("f");
    `log("apd:" @ s);

    // We can copy dynamic arrays using the assignment operator.
    c = s;
    `log("cpy:" @ c);

    // Dynamic arrays support a "slice" operator with the syntax
    // array.GetRange(startIndex, count).
    l = s.GetRange(2, 3);
    `log("sl1:" @ l);

    // This gets all elements up to (but excluding) index 5.
    l = s.GetRange(0, 5);
    `log("sl2:" @ l);

    // And this gets all elements from index 2 to the end.
    l = s.GetRange(2, s.Length - 2);
    `log("sl3:" @ l);

    // We can declare and initialize a variable for a dynamic array
    // in a single line as well.
    t.AddItem("g");
    t.AddItem("h");
    t.AddItem("i");
    `log("dcl:" @ t);

    // UnrealScript doesn't have a built-in slices package, so we'll skip
    // the equality check for arrays.

    // Dynamic arrays can be composed into multi-dimensional data
    // structures. The length of the inner arrays can vary.
    twoD.Length = 3;
    for (i = 0; i < 3; i++)
    {
        innerLen = i + 1;
        twoD[i].Length = innerLen;
        for (j = 0; j < innerLen; j++)
        {
            twoD[i][j] = i + j;
        }
    }
    `log("2d:" @ twoD);
}

DefaultProperties
{
}

This UnrealScript code demonstrates the usage of dynamic arrays, which are similar to slices in Go. Here are some key points about the translation:

  1. UnrealScript uses array<T> for dynamic arrays, which are similar to slices in Go.
  2. Instead of make(), we use the Length property to set the initial size of an array.
  3. append() is replaced with AddItem() in UnrealScript.
  4. UnrealScript doesn’t have a built-in copy() function for arrays, so we use the assignment operator.
  5. Slicing in UnrealScript is done using the GetRange() method.
  6. UnrealScript doesn’t have a direct equivalent to Go’s slices package, so those operations are omitted.
  7. Logging is done using the log command instead of fmt.Println().
  8. The Init() function is used instead of main() as the entry point for the example.

Note that UnrealScript has some limitations compared to Go, particularly in terms of built-in functions for array manipulation. However, this example showcases how to achieve similar functionality using UnrealScript’s features.