UnLua基础使用
Lua 调用 UE4
调用被覆写的方法(Overridden)
function BP_Actor1_C:OverriddenPrintTest()
-- self.Overridden.PrintTest(self)
self.Overridden:PrintTest()
end
- 注意一定要传 self 进去,不然 Unlua 调用的时候执行的参数检查会 Crash,在 UnLua 中调用 UE 函数有点类似于拿到成员函数的原生指针,必须要手动传 this 进去
调用 UE 的 C++ 函数
UE4.UKismetSystemLibrary.PrintString(self,"HelloWorld")
覆写多返回值的函数
- c++ 或 蓝图的多值返回使用 lua的多值形式获取
local ret1, ret2, ret3 = Function(...)
非原始类型的返回
UFUNCTION()
static void GetStruct(FNameStruct& NameStruct) const;
- 直接返回
local NameStruct = Object1.GetStruct()
- 引用返回
local HitResult = FHitResult()
self:GetHitResult(HitResult)
- 直接返回更直观,引用返回性能更高(在循环中)
调用成员函数
静态方法
- 静态方法使用 “.”
UE4.UObject1.GetInstance()
实例方法
- 实例方法使用 “:”
local Instance = UE4.UObject1.GetInstance()
-- Instance.Hello1(self)
Instance:Hello1()
LoadObject
- 把资源加载到内存
local Object = LoadObject("/Game/Core/Blueprints/AI/BT_Enemy")
对应的UE4代码
UObject *Object = LoadObject<UObject>(nullptr, *ObjectPath);
UClass.Load
- 加载UClass
UE4.UClass.Load("Blueprint-Reference")
SpawnActor
- 生成actor
local World = self:GetWorld()
local WeaponClass = UE4.UClass.Load("/Game/Core/Blueprints/Weapon/BP_DefaultWeapon.BP_DefaultWeapon")
local NewWeapon = World:SpawnActor(WeaponClass, self:GetTransform(), UE4.ESpawnActorCollisionHandlingMethod.AlwaysSpawn, self, self, "Weapon.BP_DefaultWeapon_C")
NewObject
- UnLua 中的 NewObject 可以接收四个参数,依次是:创建的 UClass、Outer、Name,以及动态绑定的 Lua 脚本。
local ProxyObj = NewObject(ObjClass, self, nil, "Objects.ProxyObject")
UMG
local UMG_C = UE4.UClass.Load("/Game/Test/BPUI_TestMain.BPUI_TestMain_C")
local UMG_TestMain_Ins = UE4.UWidgetBlueprintLibrary.Create(self,UMG_C)
UMG_TestMain_Ins:AddToViewport()
Delay
使用coroutine + Delay 实现延时调用
function BP_Actor1_C:DelayFunc(Induration)
coroutine.resume(coroutine.create(
function(WorldContectObject,duration)
-- 蓝图的Delay节点
UE4.UKismetSystemLibrary.Delay(WorldContectObject,duration)
UE4.UKismetSystemLibrary.PrintString(WorldContectObject,"Helloworld")
end
),
self,Induration)
end
调用代码
function LoadingMap_C:ReceiveBeginPlay()
-- 5s后输出HelloWorld
self:DelayFunc(5.0)
end
绑定代理
Unlua只能绑定动态代理
单播绑定
DECLARE_DYNAMIC_DELEGATE(FNoParamDynamicDelegate); UPROPERTY() FNoParamDynamicDelegate TestNoParamDynamicDelegate;
local Object1 -- 赋值对象实例 Object1.TestNoParamDynamicDelegate:Bind(self, function() print("callback")) end --使用冒号且需要使用self Object1.TestNoParamDynamicDelegate:Execute() Object1.TestNoParamDynamicDelegate:Unbind()
多播绑定
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FNoParamMDynamicMulticastDelegate); UPROPERTY() FNoParamMDynamicMulticastDelegate TestNoParamMDynamicMulticastDelegate;
local Object1 -- 赋值对象实例 Object1.TestNoParamMDynamicMulticastDelegate:Add(self, function() print("callback1")) end) Object1.TestNoParamMDynamicMulticastDelegate:Add(self, function() print("callback2")) end) Object1.TestNoParamMDynamicMulticastDelegate:Broadcast()
TArray
index从1开始
local Indices = UE4.TArray(0)
Indices:Add(100)
Indices:Add(30)
print(Indices:Length())
print(Indices:Get(1))
Struct
local Struct_1 = UE4.FStruct_1()
Struct_1.id = 2
Struct_1.name = 'icewolf2'
Struct_1.age = 200
print(Struct_1.id .. ":" .. Struct_1.name .. ":" .. Struct_1.age)
UE4 调用Lua
其他
Lua 代码提示
Commandlet的调用方法
UE4Editor-cmd.exe PROJECT_PATH.uproject -run=COMMANDLET
Unlua的调用方法
D:\UnrealEngine\Epic\UE_4.23\Engine\Binaries\Win64\UE4Editor-cmd.exe C:\Users\imzlp\Documents\UnrealProjectSSD\MicroEnd_423\MicroEnd_423.uproject -run=UnLuaIntelliSense