Tencent / UnLua

A feature-rich, easy-learning and highly optimized Lua scripting plugin for UE.
Other
2.28k stars 619 forks source link

多次跳转关卡lua访问蓝图的父类函数会产生崩溃 #635

Open HSHSir opened 1 year ago

HSHSir commented 1 year ago

复现操作如下 环境: Unreal版本4.27(源码或发行版都可) Unlua版本:2.4.0 操作步骤: 1.新建一个C++工程,新建三张空地图,命名Map1,Map2,Map3 2.新建BP_Gamemode继承自GameModeBaes,无逻辑,新建BP_Controller继承自PlayerController,无逻辑 3.新建BP_ActorAA继承自Actor,并添加一个函数FuncAA,该函数仅打印 image 4.新建BP_ActorBB继承自BP_ActorAA,并添加一个函数FuncBB,该函数仅调用父类的FuncAA(证明蓝图可以访问父蓝图函数) image

最终项目文件目录: image

5. BP_Controller和BP_ActorBB绑定Lua脚本
---@type BP_Controller_C
local M = UnLua.Class()

function M:ReceiveBeginPlay()

    UE.UKismetSystemLibrary.K2_SetTimerDelegate({self,self.JumpToNewLevel},0.8,false,0,0)

    local ActorClass = UE.UClass.Load("/Game/BP_ActorBB.BP_ActorBB_C")
    local Transform = UE.FTransform()
    Transform.Translation = UE.FVector(0,0,1000)
    self:GetWorld():SpawnActor(ActorClass,Transform,UE.ESpawnActorCollisionHandlingMethod.AdjustIfPossibleButAlwaysSpawn, nil, nil)
end

---停止定时器自动跳转新关卡
function M:StopJumpLevel()
    UE.UKismetSystemLibrary.K2_ClearTimerDelegate({self,self.JumpToNewLevel})
end

function M:JumpToNewLevel()
    local LevelName = UE.UGameplayStatics.GetCurrentLevelName(self,true)
    if LevelName == "Map1" then
        UE.UGameplayStatics.OpenLevel(self,"Map2",true,"")
    elseif LevelName == "Map2" then
        UE.UGameplayStatics.OpenLevel(self,"Map3",true,"")
    elseif LevelName == "Map3" then
        UE.UGameplayStatics.OpenLevel(self,"Map1",true,"")
    end
end
return M

---@type BP_ActorBB_C
local M = UnLua.Class()
function M:ReceiveBeginPlay()
    print("ActorBB --> BeginPlay")
    self:FuncBB()
    if self.FuncAA then
        print("函数访问成功")
        self:FuncAA()
    else
        print("函数访问失败")
        UE.UKismetSystemLibrary.PrintString(self, "Lua 访问蓝图的父类函数失败", true, false, UE.FLinearColor(1,0,0,1), 200)
    end
end
return M

image image

打包后运行,跳转7 8次关卡后,程序就会崩溃

崩溃堆栈: image

输出: image

从输出日志可以看出,当程序执行BP_ActorBB绑定的lua脚本21行,访问self.FuncAA产生崩溃,FuncAA是父蓝图的一个函数 image

HSHSir commented 1 year ago

补充一下,三张地图的GameMode都设置为BP_GameMode,BP_GameMode的默认Controller设置为BP_Controller