我想知道下面的设置是否适用于小游戏:
假设我在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=<5“传递给lua状态引擎,在那里它将调用c++函数getCondition(),然后查看它是否为=<5并返回true或false?luaL_doString(L,"setAction(7,4,6)“)也是如此吗?
此外,这是不是一种合适的方式来创建计时器,只访问lua一次(创建它们),让c++处理其余的事情,只通过lua调用c++函数,让lua只处理逻辑?
提前谢谢。
发布于 2010-09-22 12:00:23
您可能希望将条件字符串更改为"return getCondition()<=5",否则字符串块将无法编译或运行。然后,当luaL_doString()成功返回时,检查堆栈上的布尔返回值。如下所示:
// 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) {
lua_settop(L, 0);
if(luaL_doString(L, *it->condition) == 0 && lua_toboolean(1)){
luaL_doString(L, *it->action);
}
}
}发布于 2010-09-23 00:40:09
您不能在Lua运行时中断它。您可以做的最好的事情是设置一个标志,然后在安全的时间处理中断。独立解释器使用这种技术来处理用户中断(control-C)。这项技术也用在我的lalarm库中,它可以用来实现计时器回调,尽管不是在您想要的高级别。
https://stackoverflow.com/questions/3765678
复制相似问题