MCPcopy Create free account
hub / github.com/Tencent/UnLua / Bind

Method Bind

Plugins/UnLua/Source/UnLua/Private/UnLuaManager.cpp:56–117  ·  view source on GitHub ↗

* Bind a Lua module for a UObject */

Source from the content-addressed store, hash-verified

54 * Bind a Lua module for a UObject
55 */
56bool UUnLuaManager::Bind(UObject *Object, const TCHAR *InModuleName, int32 InitializerTableRef)
57{
58 check(Object);
59
60 const auto Class = Object->IsA<UClass>() ? static_cast<UClass*>(Object) : Object->GetClass();
61 lua_State *L = Env->GetMainState();
62
63 if (!Env->GetClassRegistry()->Register(Class))
64 return false;
65
66 // try bind lua if not bind or use a copyed table
67 UnLua::FLuaRetValues RetValues = UnLua::Call(L, "require", TCHAR_TO_UTF8(InModuleName));
68 FString Error;
69 if (!RetValues.IsValid() || RetValues.Num() == 0)
70 {
71 Error = "invalid return value of require()";
72 }
73 else if (RetValues[0].GetType() != LUA_TTABLE)
74 {
75 Error = FString("table needed but got ");
76 if(RetValues[0].GetType() == LUA_TSTRING)
77 Error += UTF8_TO_TCHAR(RetValues[0].Value<const char*>());
78 else
79 Error += UTF8_TO_TCHAR(lua_typename(L, RetValues[0].GetType()));
80 }
81 else
82 {
83 BindClass(Class, InModuleName, Error);
84 }
85
86 if (!Error.IsEmpty())
87 {
88 UE_LOG(LogUnLua, Warning, TEXT("Failed to attach %s module for object %s,%p!\n%s"), InModuleName, *Object->GetName(), Object, *Error);
89 return false;
90 }
91
92 // create a Lua instance for this UObject
93 Env->GetObjectRegistry()->Bind(Class);
94 Env->GetObjectRegistry()->Bind(Object);
95
96 // try call user first user function handler
97 int32 FunctionRef = PushFunction(L, Object, "Initialize"); // push hard coded Lua function 'Initialize'
98 if (FunctionRef != LUA_NOREF)
99 {
100 if (InitializerTableRef != LUA_NOREF)
101 {
102 lua_rawgeti(L, LUA_REGISTRYINDEX, InitializerTableRef); // push a initializer table if necessary
103 }
104 else
105 {
106 lua_pushnil(L);
107 }
108 bool bResult = ::CallFunction(L, 2, 0); // call 'Initialize'
109 if (!bResult)
110 {
111 UE_LOG(LogUnLua, Warning, TEXT("Failed to call 'Initialize' function!"));
112 }
113 luaL_unref(L, LUA_REGISTRYINDEX, FunctionRef);

Callers

nothing calls this directly

Calls 13

checkFunction · 0.85
CallFunction · 0.85
lua_typenameFunction · 0.85
PushFunctionFunction · 0.85
lua_rawgetiFunction · 0.85
lua_pushnilFunction · 0.85
CallFunctionFunction · 0.85
luaL_unrefFunction · 0.85
RegisterMethod · 0.45
IsValidMethod · 0.45
NumMethod · 0.45
GetTypeMethod · 0.45

Tested by

no test coverage detected