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
相关推荐
吴梓穆20 小时前
UE5 重新编译插件版本
ue5
HECUgauss20 小时前
UE5 使用过程遇到的问题
ue5
m0_5522008220 小时前
《UE5_C++多人TPS完整教程》学习笔记40 ——《P41 装备(武器)姿势(Equipped Pose)》
c++·游戏·ue5
漫游者Nova1 天前
虚幻引擎Unreal Engine5恐怖游戏设计制作教程,从入门到精通从零开始完整项目开发实战详细讲解中英字幕
ue5·游戏引擎·虚幻·游戏开发完整教程·恐怖游戏开发
ue星空3 天前
UE5音频技术
ue5
帅_shuai_14 天前
UE5 游戏模板 —— FirstShootGame
游戏·ue5
吴梓穆15 天前
UE5 手动停止Delay定时器
ue5
AgilityBaby16 天前
UE5蓝图按键输入绑定学习笔记
笔记·学习·ue5·蓝图
涟涟涟涟16 天前
UE5错误 Linux离线状态下错误 请求失败libcurl错误:6无法解析主机名
linux·ue5
蛋卷卷-16 天前
【UE5】如何开发安卓项目的udp客户端
android·ue5·udp