From d126b77c6d7f20709f23530c92cc9a677ecdccb8 Mon Sep 17 00:00:00 2001 From: jh510574 Date: Thu, 17 Feb 2022 14:23:29 +0800 Subject: [PATCH] Update CCComponentLua.cpp each ComponentLua object should be stand alone, rather than change the same metatable. --- .../lua-bindings/manual/CCComponentLua.cpp | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/cocos/scripting/lua-bindings/manual/CCComponentLua.cpp b/cocos/scripting/lua-bindings/manual/CCComponentLua.cpp index b75911b52808..87e08b56ff15 100644 --- a/cocos/scripting/lua-bindings/manual/CCComponentLua.cpp +++ b/cocos/scripting/lua-bindings/manual/CCComponentLua.cpp @@ -232,18 +232,11 @@ void ComponentLua::storeLuaTable() lua_rawset(l, -3); // stack: table_return_from_lua table_of_component lua_pop(l, 1); // stack: table_return_from_lua - // add table's elements to userdata's metatable - object_to_luaval(l, "cc.ComponentLua", this); // stack: table_return_from_lua userdata - lua_getmetatable(l, -1); // stack: table_return_from_lua userdata mt - lua_remove(l, -2); // stack: table_return_from_lua mt - lua_pushnil(l); // stack: table_return_from_lua mt nil - while (lua_next(l, -3)) // stack: table_return_from_lua mt key value - { - lua_pushvalue(l, -2); // stack: table_return_from_lua mt key value key - lua_insert(l, -2); // stack: table_return_from_lua mt key key value - lua_rawset(l, -4); // stack: table_return_from_lua mt key - } - lua_pop(l, 2); + // set table_return_from_lua as the new environment for this + object_to_luaval(l, "cc.ComponentLua", this); // stack: table_return_from_lua/this + lua_pushvalue(l, -2); // stack: table_return_from_lua/this/table_return_from_lua + lua_setfenv(l, -2); // stack: table_return_from_lua/this + lua_pop(l, 1); // stack: table_return_from_lua } void ComponentLua::removeLuaTable()