Unreal
For all Unreal Engine based games, you can use the Unreal
namespace to access any Unreal objects the engine exports and take your scripts to a next level.
UObject
UObject
type represents any Unreal Engine object. There are three functions that return Unreal Object instances.
-- Gets the global UEngine instance.
--
UObject Unreal.GetEngine()
-- Gets the Unreal Object and of any entity from the entity list.
--
UObject Unreal.From(Entity entity)
-- Gets the default instance of a class type.
--
UObject? Unreal.DefaultInstance(UClass|string class)
-- Gets and sets the value of a field.
--
any UObject:__index(string|int k)
void UObject:__newindex(string|int k, any v)
You can transparently read (and write for numeric/vector fields) any public field by their name, convert the objects into string, and compare them.
local world = Unreal.GetEngine().GameViewport.World
local levels = world.Levels
local persistentLevel = world.PersistentLevel
for i=1,#levels do
local level = levels[i]
-- Prints 'Level #1 = 'Level @ 000001ED264FE200' (Persistent)'
print(string.format("Level #%d = '%s' %s", i, level, level == persistentLevel ? "(Persistent)" : ""))
end
Note that FVector
and FRotator
instances will be automatically converted to vec3
and will be transformed into our coordinate system.
UClass
UClass
type represents the metadata for all UObject
types. You can get the UClass
object either by name or from an already existing UObject
instance.
-- Gets a class instance by name.
--
UClass? Unreal.StaticClass(string name)
-- Gets the class of an object.
--
UClass? Unreal.GetClass(UObject object)
-- Gets an iterator for all classes, if an UClass is given, filters to classes inherting from it.
--
Iterable<UClass> Unreal.Classes(UClass? super)
-- Gets an array containing all super classes in order.
--
UClass[] Unreal.Hierarchy(UClass class)
-- Returns the number of fields.
--
uint UClass:__len()
-- Gets the Nth field's name.
--
string? UClass:__index(uint i)
The UClass
type can be enumerated like an array to get information about the fields.
local function printFields(object)
local class = Unreal.GetClass(object)
local fieldCount = #class
for i=1,fieldCount do
if i == 11 then
print(string.format("... (%d more)", fieldCount - 10))
break
else
local fieldName = class[i]
print(string.format(" .%s = %s", fieldName, object[fieldName]))
end
end
end
printFields(Unreal.GetEngine())
-- Prints:
-- .TinyFont = Font @ 000001ED39495000
-- .TinyFontName = SoftObjectPath @ 000001ED23035BF8
-- .SmallFont = Font @ 000001ED39495200
-- .SmallFontName = SoftObjectPath @ 000001ED23035C18
-- .MediumFont = Font @ 000001ED39495200
-- .MediumFontName = SoftObjectPath @ 000001ED23035C38
-- .LargeFont = Font @ 000001ED39495200
-- .LargeFontName = SoftObjectPath @ 000001ED23035C58
-- .SubtitleFont = Font @ 000001ED39495200
-- .SubtitleFontName = SoftObjectPath @ 000001ED23035C78
--... (217 more)
You can also check if an UObject
instance is of the given class type or not by using the IsA
function.
-- Returns true if object isn instance of the given class.
--
bool Unreal.IsA(UObject object, UClass class)
local worldClass = Unreal.StaticClass("World")
local world = Unreal.GetEngine().GameViewport.World
print(Unreal.IsA(world, worldClass)) -- Prints 'true'