【发布时间】:2010-09-22 01:20:24
【问题描述】:
我想知道以下设置是否适用于小型游戏:
假设我在 Lua 中注册了以下函数,如下所示:
lua_register(L, "createTimer", createTimer);
lua_register(L, "getCondition", getCondition);
lua_register(L, "setAction", setAction);
在哪里:(留下类型检查)
int createTimer(lua_State* L){
string condition = lua_tostring(L, 1);
string action = lua_tostring(L, 2);
double timer = lua_tonumber(L, 3);
double expiration = lua_tonumber(L, 4);
addTimer(condition, action, timer, expiration); // adds the "timer" to a vector or something
return 1;
}
通过以下方式在 lua 中调用此函数:
createTimer("getCondition=<5", "setAction(7,4,6)", 5, 20);
然后我可以执行以下操作吗(?):
// this function is called in the game-loop to loop through all timers in the vector
void checkTimers(){
for(std::vector<T>::iterator it = v.begin(); it != v.end(); ++it) {
if(luaL_doString(L, *it->condition)){
luaL_doString(L, *it->action)
}
}
}
这行得通吗? luaL_doString 是否会将“getCondition=
此外,通过只访问 lua 一次(创建它们)并让 c++ 处理其余部分,只通过 lua 调用 c++ 函数并让 lua 只处理逻辑,这是否是一种合适的方法来创建计时器?
提前致谢。
【问题讨论】: