我已经在LuabDin中嵌入了我的C++应用程序中的Lua。我需要在多个运行中保持变量,运行相同文件名的其他对象无法访问这些变量。在
例如:假设我有一个名为NPC
的类。NPC
包含一个字符串,即它们运行的脚本的名称。创建NPC
时,将创建一个名为Health
的变量。当一个NPC
被击中时,他们失去5点生命值。在Lua中,脚本应该是这样的:
local health = 10
function onHit()
health = health - 5
end
我遇到的问题是每个运行此脚本的NPC
没有自己的运行状况实例。例如,假设我创建NPC A
,并从其运行状况中减去5。然后,我创建NPC B
。因为它将health重置回10,如果我告诉NPC A
打印health,它会返回10,即使应该是5。在
如果每个对象都有一个不同的Lua实例,那么它就可以这样工作了,但是在这种情况下,我一次只能得到几百个实例,这不是一件好事。在
在Lua中有没有一种方法可以让变量像这样工作?如果没有,有没有一种脚本语言可以像这样高效地工作?在
作为参考,下面是我正在测试的代码:
卢阿:
^{pr2}$C++:
class NPC
{
public:
NPC(lua_State* inState);
void onHit();
const char* behavior;
lua_State* luaState;
};
NPC::NPC(lua_State* inState)
{
luaState = inState;
behavior = "testBehavior.lua";
luaL_dofile(luaState, behavior);
}
void NPC::onHit()
{
luaL_loadfile(luaState, behavior);
luabind::call_function<int>(luaState, "onHit");
}
void print_out(int number) {
cout << "Health : " << number << endl;
}
int main()
{
lua_State *myLuaState = luaL_newstate();
luabind::open(myLuaState);
luabind::module(myLuaState) [
luabind::def("print_out", print_out)
];
NPC test(myLuaState);
NPC test2(myLuaState);
test.onHit();
test2.onHit();
test.onHit();
lua_close(myLuaState);
}
这里有很多错误。你一直在运行Lua脚本;这就是为什么它总是被重置的原因。另一个问题是您的脚本正在创建一个全局函数,因此每次运行它时,都会得到一个新的全局函数,它使用一个new
local
变量。在停止使用全局变量。每个NPC都是一个独立的对象。因此,它需要有对象特定的数据。在
这不会创建任何特定于对象的数据。它只是运行一个Lua脚本并完全丢弃所有返回值。除非该脚本实际全局存储特定于对象的内容,否则不会创建任何特定于对象的数据。在
Lua脚本需要做的是返回一个表,该表包含脚本需要的对象特定数据。脚本应该如下所示:
^{pr2}$ <>你的C++代码需要在NPC类中使用EM>Stury您的类中没有
lua_State* luaState
,而是在类中保留一个luabind::object luaData
。如果您需要lua_State
,您可以随时从luabind::object
获得一个。在要调用Lua
onHit
函数,只需使用luabind::object
接口:请注意,不要重新运行Lua脚本。这就是你的问题所在。你只是在调用一个Lua脚本已经定义的函数。在
现在,您似乎希望使用局部变量而不是表内存。这很好,它将阻止C++代码直接访问^ {< CD8}}(不是没有欺骗)。{{cd6>我们必须简化代码。你可以在Lua中用这个:
{NPC>不需要改变构造函数。在
如果你一意孤行地使用全局变量,你可以和环境玩游戏,但这相当复杂。它将在各个脚本调用之间提供有保证的隔离。在
你可以看看卢娅的closures。它看起来像这样:
结果是每个人大都有自己的功能。每次命中它们时,只需调用
^{pr2}$onHit
函数。你应该这样使用它:您可以添加如下参数:
我觉得值得一试。在
我不知道卢娅的事,所以我就不谈这个了。这个问题也标记为python。为此,我认为可以序列化所有“重要”的局部变量。在
或者,您可以查看我的技巧,在这里保存一个名为PyDON的字典:http://code.activestate.com/recipes/577508-pydon-an-alternative-to-xml/
同时检查一下这个,我认为这会更有帮助:How do I serialize a Python dictionary into a string, and then back to a dictionary?
相关问题 更多 >
编程相关推荐