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
相关推荐
Maya动画技术19 小时前
ue5.5崩溃报gpu错误快速修复注册表命令方法
ue5·ue5.5崩溃报gpu错误
每天回答3个问题3 天前
AI数字人| Fay开源项目、UE5数字人、本地大模型
人工智能·python·ue5·开源·游戏引擎
子燕若水3 天前
UE5 蓝图项目转换为 C++项目
java·c++·ue5
努力的小钟3 天前
UE5 AssetManager类使用详解
ue5
努力的小钟3 天前
UE5中UBlueprintFunctionLibrary类详解
ue5
GentooEmacs5 天前
UnrealEngine UE5 可视化 从地球观察火星 金星 土星 运动轨迹
ue5
tkokof17 天前
崩溃(Crash)简记
数据结构·c++·ue5·ue4·crash
飞30010 天前
飞鱼科技游戏策划岗内推
游戏·ue5·业界资讯·游戏策划
Bluesonli11 天前
第 22 天:多线程开发,提高 UE5 性能!
学习·游戏·ue5·虚幻引擎·unreal engine
远离UE411 天前
UE5 Computer Shader学习笔记
笔记·学习·ue5