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
相关推荐
邪修king2 天前
UE5 零基础入门第二弹:让你的几何体 “活” 起来 ——Actor 基础与蓝图交互入门
c++·ue5·交互
Дерек的学习记录2 天前
Unreal Eangie 5:蓝图编程
开发语言·学习·ue5
吴梓穆2 天前
UE5 c++ 常用方法
java·c++·ue5
吴梓穆3 天前
UE5 无法修改Actor的图层
ue5
GentooEmacs3 天前
UnrealEngine(UE5)阿耳忒弥斯2号绕月飞行模拟
ue5·阿耳忒弥斯2号·绕月飞行·spice星历
吴梓穆4 天前
UE5 C++ 两种枚举
开发语言·c++·ue5
邪修king4 天前
【UE4/UE5 萌新向】有C++基础如何快速入门虚幻引擎?超详细图文全揭秘!
c++·ue5·ue4
Kang.Charles4 天前
UE游戏性能优化归结(基于UE5环境)
游戏·ue5
吴梓穆4 天前
UE5 Invideo 插件,拉取rtsp视频流
ue5
曼巴UE56 天前
Unlua 官方案例
c++·ue5·lua·ue