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
相关推荐
ue星空1 天前
UE5音频技术
ue5
帅_shuai_12 天前
UE5 游戏模板 —— FirstShootGame
游戏·ue5
吴梓穆13 天前
UE5 手动停止Delay定时器
ue5
AgilityBaby14 天前
UE5蓝图按键输入绑定学习笔记
笔记·学习·ue5·蓝图
涟涟涟涟14 天前
UE5错误 Linux离线状态下错误 请求失败libcurl错误:6无法解析主机名
linux·ue5
蛋卷卷-14 天前
【UE5】如何开发安卓项目的udp客户端
android·ue5·udp
异次元的归来16 天前
UE5反射系统分析(一)generated.h
ue5·游戏引擎·unreal engine
道之所在16 天前
UE5.6源码安卓打包报错
android·ue5
AgilityBaby16 天前
UE5创建蒙太奇动画并播放和在动画蒙太奇中创建动画通知状态
笔记·学习·ue5·游戏引擎·蓝图·蒙太奇动画
北冥没有鱼啊16 天前
UE5 仿 鸣潮人物渲染
ue5·游戏开发·虚幻·材质