Methods in UnrealScript

UnrealScript supports methods defined on struct types, similar to classes in object-oriented programming.

class Rect extends Object;

var int width;
var int height;

// This `Area` method is defined for the Rect class
function int Area()
{
    return width * height;
}

// Methods can be defined for the class
function int Perim()
{
    return 2 * width + 2 * height;
}

// The `static` keyword is used for functions that don't require an instance
static function void Main()
{
    local Rect r;
    
    r = new class'Rect';
    r.width = 10;
    r.height = 5;

    // Here we call the 2 methods defined for our class
    `log("area: " $ r.Area());
    `log("perim:" $ r.Perim());

    // UnrealScript doesn't have pointers, so we'll just use the same instance
    `log("area: " $ r.Area());
    `log("perim:" $ r.Perim());
}

DefaultProperties
{
}

To run this code in UnrealScript:

$ # This would be run within the Unreal Engine environment
$ # The exact method depends on your setup and version of Unreal Engine

UnrealScript doesn’t have the same concept of standalone executables as Go. Instead, this code would typically be part of a larger UnrealScript project within the Unreal Engine environment.

UnrealScript uses classes instead of structs, and doesn’t have the concept of pointer and value receivers like Go does. All method calls in UnrealScript are effectively on the object instance.

Next, we’ll look at UnrealScript’s mechanism for grouping and naming related sets of methods: interfaces and classes.