Variables in UnrealScript

In UnrealScript, variables are explicitly declared and used by the compiler to check type-correctness of function calls.

class VariablesExample extends Object;

function Example()
{
    // Declare and initialize a variable
    local string a;
    a = "initial";
    `log(a);

    // Declare multiple variables at once
    local int b, c;
    b = 1;
    c = 2;
    `log("b=" $ b $ ", c=" $ c);

    // UnrealScript will infer the type of initialized variables
    local bool d;
    d = True;
    `log("d=" $ d);

    // Variables declared without initialization are zero-valued
    local int e;
    `log("e=" $ e);

    // UnrealScript doesn't have a shorthand declaration and initialization syntax
    // So we use the standard declaration and assignment
    local string f;
    f = "apple";
    `log(f);
}

defaultproperties
{
}

In UnrealScript, variables are typically declared using the local keyword inside functions. The language doesn’t have a direct equivalent to Go’s := syntax for shorthand declaration and initialization.

To run this code in UnrealScript:

  1. Create a new script file with a .uc extension (e.g., VariablesExample.uc).
  2. Place the file in your project’s Classes directory.
  3. Compile the script as part of your UnrealScript project.
  4. You can then call the Example() function from elsewhere in your code, or set it up to run when needed in your game or application.

The output would look similar to this:

initial
b=1, c=2
d=True
e=0
apple

Note that in UnrealScript, we use the backtick (`) for logging instead of a print function. The $ operator is used for string concatenation.

UnrealScript doesn’t have a standalone execution model like Go. It’s typically part of a larger Unreal Engine project and runs within that context.