Working with UObjects

Athenaware's Lua implemention was designed to make working with objects easy and similar to how you'd do it in C++.

Accessing Object Members

In Lua, the colon (:) and dot (.) are both used for accessing members (methods or fields) of an object. However, they have a slight difference in terms of syntax and behavior.

The dot (.) operator is used to access a field directly. It is used with the following syntax:

object.member

Here, object is the table or object, and member is the name of the field within that object. When using the dot operator.

On the other hand, the colon (:) operator is used to call table functions and explicitly pass the object itself as the first argument. It is used with the following syntax:

object:func(arguments)

Here, object is the object, func is the name of the function within the object, and arguments are the arguments to be passed to the function.

Members/fields can also be modified from Lua:

local vec = FVector()
vec.X = 123.0
vec.Y = 0
vec.Z = 12

Construction

Only ScriptStruct's can be constructed. Trying to construct anything beyond that is not supported and may result in a crash.

In order to construct, we need to first load the class via GetStruct.

Constructing structs can be achieved by "calling" the pre-defined ScriptStruct with your supplied parameters. The order of which the parameters are layed out in the struct is the same order you'll supply it in the constructor. For example:

The FVector struct is defined as followed in UE4:

struct FVector {
    float X;
    float Y;
    float Z;
};

Creating an instance of an FVector, we'd do the following:

local vectorA = FVector(1, 2, 3) -- {X=1, Y=2, Z=3}
local vectorB = FVector(2, 4) -- {X=2, Y=4, Z=0} since Z wasn't passed in, it will be 0

-- We can also initialize a vector and assign values later...
local vectorC = FVector()
vectorC.X = 1.0
vectorC.Y = 12.0
vector.Z = 43.0

Object Validity

All UObjects held in lua are weak references. In order to check if the object is valid, you can use UObject::IsValid. This function will return true or false depending on whether or not the function is a valid UObject.

local storage = CONTROLLER
if storage:IsValid() then
    -- valid object, can be used
    local player = storage:K2_GetPawn()
else
    storage = nil -- nil the value since it's not valid
end

UObject::IsA

Check if an object is of a type of class. Returns true if it is, false if not.

local AShip = GetStruct('Athena', 'Ship')
function onActorTick(actor)
    if actor:IsA(AShip) then
        -- this actor is a ship
    end
end

Last updated