UnLua扩展C++函数和蓝图自定义事件

一、通过BlueprintImplementableEvent标记扩展C++函数

1、 这个标记表示C++不需要实现,让蓝图/Lua重写。

2、首先在C++中将LuaImp函数标记为BlueprintImplementableEvent,不需要实现,然后再GetIndex中调用该函数。

MyBaseActor.h

cpp 复制代码
UFUNCTION(BlueprintImplementableEvent)
void LuaImp();
UFUNCTION(BlueprintCallable)
int GetIndex();

MyBaseActor.cpp

cpp 复制代码
int AMyBaseActor::GetIndex()
{
	LuaImp();
	return index++;
}

2、在Lua中重写LuaImp函数

BP_BaseActor.lua

lua 复制代码
 --实现C++中定义的BluprintImplementable方法
 function BP_BaseActor_C:LuaImp()
	print("this is a function implements C++")
 end

3、在Lua中调用GetIndex函数,即可执行到Lua中扩展的LuaImp函数

WBP_FirstLua.lua

lua 复制代码
Actor:GetIndex()

二、通过BlueprintNativeEvent标记扩展C++函数

1、BlueprintNativeEvent也是让蓝图/Lua重写扩展,但是C++端会提供一个默认的函数名为LuaNative_Implementation实现,如果蓝图/Lua没有重写,就执行默认的

2、首先在C++中将LuaNative函数标记为BlueprintNativeEvent,再实现一个LuaNative_Implementation函数。

MyBaseActor.h

cpp 复制代码
	UFUNCTION(BlueprintNativeEvent)
	void LuaNative();
	void LuaNative_Implementation();

3、在GetIndex函数中调用LuaNative,在LuaNative_Implementation中调用LuaImp。

MyBaseActor.cpp

cpp 复制代码
void AMyBaseActor::LuaNative_Implementation()
{
	LuaImp();
}
int AMyBaseActor::GetIndex()
{
	LuaNative();
	return index++;
}

4、在Lua中调用GetIndex

WBP_FirstLua.lua

lua 复制代码
Actor:GetIndex()

5、如果在Lua中没有重写LuaNative函数,则会调用C++中的LuaNative_Implementation中LuaImp函数,输出"this is a function implements C++"

6、如果在Lua中重写了LuaNative函数,就会调用Lua中的LuaNative函数,输出"Native Event"

BP_BaseActor.lua

lua 复制代码
  function BP_BaseActor_C:LuaNative()
	print("Native Event")
  end

三、Lua扩展蓝图自定义事件

1、在蓝图中新建自定义事件并在EventBeginPlay中调用

2、在Lua中重写BP_EventTest函数,需要注意的是Lua中把ReceiveBeginPlay函数注释掉,蓝图的EventBeginPlay才会生效。

BP_BaseActor.lua

lua 复制代码
-- function BP_BaseActor_C:ReceiveBeginPlay()
-- end

 function BP_BaseActor_C:BP_EventTest()
	print("BP_EventTest")
  end
相关推荐
zhangzhangkeji5 天前
UE5 C++(71):文件是否存在,文件夹是否存在,FPaths :: FileExists( const FString & InPath) ;
ue5
妙为5 天前
UE5角色穿过石头穿模
ue5·unreal engine5·角色穿越石头·穿模
技术策划Boring6 天前
2025年工作复盘:开放世界3A项目配置管线与性能监控的探索
游戏·ue5·虚幻·p4·perforce
zhangzhangkeji8 天前
UE5 C++(70-2):定义成员函数 getCleanDirectory(..) 和枚举类 EFileDirectoryType,来获得目录
ue5
avi91119 天前
UE4-UE5虚幻引擎-前置学习三,优化,基础CPP
ue5·ue4·游戏开发·虚幻·游戏优化·游戏代码
zhangzhangkeji9 天前
UE5线程进阶(3-2):任务图的相关源码整理。 FGraphEvent 与 TGraphTask 的区别和联系
ue5
zhangzhangkeji11 天前
UE5线程进阶(3-1):
ue5
zhangzhangkeji11 天前
UE5线程进阶(2-3):enum ENamedThreads命名空间 :: Type : int32 { RHIThread = 0 } 是渲染硬件接口线程
ue5
zhangzhangkeji12 天前
UE5线程进阶(2-1):枚举类EAsyncExecution,作业类TAsyncRunnable、TAsyncQueuedWork,及全局线程函数 Async(..),及线程调用的 4 种方法总结
ue5
zhangzhangkeji13 天前
UE5线程进阶(1):
ue5