Caveats

This implementation has a few caveats.

Operands

Unfortunately a limitation of unreal engine is that without hardcoding things myself, there is no way to know what operands are available for each ScriptStruct. For example, it is not possible to do the following by default:

local FVector = LoadStruct('CoreUObject', 'Vector')
local addition = FVector(1, 1, 1) + FVector(5, 5, 5)

As a workaround, there are functions available in the KismetMathLibrary which will allow you to do such equations. Instead of the above you'd need to do this:

local Math = GetClass('Engine', 'KismetMathLibrary')
local FVector = GetStruct('CoreUObject', 'Vector')
local newVector = Math:Add_VectorVector(FVector(0, 0, 0), FVector(1, 2, 3))

Now doing the above can get very messy when you're constantly doing math operations with vectors or any other struct for that matter. So what's a better way? Your own wrapper type! The following is an example that'll allow you to work with vectors.

local Vec3 = setmetatable({ x = x or 0, y = y or 0, z = z or 0 }, { __call = function(_, ...) return setmetatable({...}, { __index = Vec3}) end })
Vec3.__add = function(a, b) return Vec3(a.x + b.x, a.y + b.y, a.z + b.z) end
Vec3.__sub= function(a, b) return Vec3(a.x - b.x, a.y - b.y, a.z - b.z) end
Vec3.__mul= function(a, b) return Vec3(a.x * b, a.y * b, a.z * b) end
Vec3.__add = function(a, b) return Vector(a.x + b.x, a.y + b.y, a.z + b.z) end
Vec3.ToStruct = function(a) return FVector(a.x, a.y, a.z) end -- make sure to load FVector

local vector = Vec3(5, 5, 5) + Vec3(1, 2, 3) -- Add the two vectors together
local unrealVector = vector.ToStruct() -- This is now an FVector

No Safety

Unfortunately since we're working with UE objects directly, there is no expectation of safety when calling functions, modifying properties, etc. If something happens that isn't supposed to happen, or you pass in invalid data to a function, etc., then your client may crash in the worst case.


Performance

Lua can be slow depending on what has been written in it. Do remember that the more code in your lua script, the more processing that has to be done. For example, onRender is called every frame. If your logic doesn't need to be executed each frame, consider a timer or moving it to another event.

Last updated