UE4 Unlua源码解析5 - FLuaContext内重要方法逐行解释
Posted 珞珈大胖强TURBO
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UE4 Unlua源码解析5 - FLuaContext内重要方法逐行解释相关的知识,希望对你有一定的参考价值。
FLuaContext内重要方法逐行解释
1 FindExportedReflectedClass
通过名字寻找静态导出的反射类
2 NotifyUObjectCreated
749 存储新Object
770 调用关键函数TryToBindLua,绑定UE4 Object到Lua
3 TryToBindLua
总结: 绑定Lua模块到UObject的函数
bool FLuaContext::TryToBindLua(UObjectBaseUtility* Object)
###1
if (!bEnable || !IsUObjectValid(Object))
return false;
if (!Object->HasAnyFlags(RF_ClassDefaultObject | RF_ArchetypeObject)) // filter out CDO and ArchetypeObjects
UClass* Class = Object->GetClass();
if (Class->IsChildOf<UPackage>() || Class->IsChildOf<UClass>()) // filter out UPackage and UClass
return false;
###1
###2
static UClass* InterfaceClass = UUnLuaInterface::StaticClass();
//!!!Fix!!!
//all bind operation should be in gamethread,include dynamic bind
if (Class->ImplementsInterface(InterfaceClass)) // static binding
// fliter some object in bp nest case
// skip objects during asset loding
if (Object->HasAnyFlags(RF_NeedLoad) && Object->HasAnyFlags(RF_Load) && Object->GetFName().GetNumber() < 1 && Object->GetClass()->GetName().Contains(Object->GetName()))
UE_LOG(LogUnLua, Log, TEXT("%s : Skip internal object (%s,%p,%s) during asset loading"), ANSI_TO_TCHAR(__FUNCTION__), *Object->GetFullName(), Object, *Class->GetName());
return false;
###2
###3
UFunction* Func = Class->FindFunctionByName(FName("GetModuleName")); // find UFunction 'GetModuleName'. hard coded!!!
if (Func)
if (IsInGameThread())
FString ModuleName;
UObject* DefaultObject = Class->GetDefaultObject(); // get CDO
DefaultObject->UObject::ProcessEvent(Func, &ModuleName); // force to invoke UObject::ProcessEvent(...)
if (ModuleName.Len() < 1)
return false;
if (!Object->HasAllFlags(RF_NeedPostLoad | RF_NeedInitialization))
return Manager->Bind(Object, Class, *ModuleName, GLuaDynamicBinding.InitializerTableRef); // bind!!!
else
// PostLoadObjects.Add((UObject*)Object);
OnDelayBindObject((UObject*)Object);
else
if (IsAsyncLoading())
// check FAsyncLoadingThread::IsMultithreaded()?
FScopeLock Lock(&Async2MainCS);
Candidates.Add((UObject*)Object); // mark the UObject as a candidate
###3
else if (GLuaDynamicBinding.IsValid(Class)) // dynamic binding
return Manager->Bind(Object, Class, *GLuaDynamicBinding.ModuleName, GLuaDynamicBinding.InitializerTableRef);
return false;
###1 判断Object类型,屏蔽CDO,Archetype,UPackage,UClass对象,不对他们进行绑定
###2 加载中的资源类屏蔽
###3通过ProcessEvent拿到实现了GetModuleName方法的返回值,即lua模块的路径,然后调用到 Manager->Bind方法
以上是关于UE4 Unlua源码解析5 - FLuaContext内重要方法逐行解释的主要内容,如果未能解决你的问题,请参考以下文章
UE4 Unlua源码解析11 - 非UE4反射支持的静态类导出给Lua使用原理
UE4 Unlua源码解析12 - Lua与UE4的混合GC