在实际项目中,TEST_TABLE
会包含很多TEST_TABLE_NESTED
,每个都有自己的testVariable
和一堆testScript
. test
函数 fromtestScript
将在 C++ 代码中使用,并且TEST_TABLE_NESTED
表也会从 C++ 代码中自动添加。
TEST_TABLE =
{
TEST_TABLE_NESTED =
{
testVariable = 5,
testScript =
{
test = function()
print(testVariable, "hello") --How to access 'testVariable'?
end
}
}
}
编辑: 这是使用此脚本的实际场景:
GameObjectScriptTables =
{
GameObject_1 = --Container of scripts corresponding to some gameObject
{
gameObjectOwner = actual_object_passed_from_c++, --This is an actual object passed from c++
GameObjectScript_1 = --This is a script with update(dt) method which will be called somwhere in c++ code
{
update = function(dt)
--here I want to use some data from gameObjectOwner like position or velocity
end
}
}
GameObject_2 =
{
gameObjectOwner = actual_object_passed_from_c++,
GameObjectScript_1 =
{
update = function(dt)
--here I want to use some data from gameObjectOwner like position or velocity
end
},
GameObjectScript_2 =
{
update = function(dt)
--here I want to use some data from gameObjectOwner like position or velocity
end
}
}
--And so on
}
想法是存在一些testVariable
对象(从 C++ 传递),这些数据在TEST_TABLE_NESTED
. 对我来说,上面的例子对于这个任务看起来很自然,但它打印nil
而不是5
. 那么如何在不打印完整路径的情况下访问testVariable
from呢?testScript
TEST_TABLE.TEST_TABLE_NESTED.testVariable