我想做的是执行一个createEvent
方法,该方法的第一个参数是在特定延迟(第二个参数)之后应在C ++中执行的回调:
createEvent(function() print("Event has been fired.") end, 10)
事件结束时,永远不会执行回调。
这是我在调用lua_pcall
之前所拥有的内容:
nil, boolean, userdata
该如何正确完成?
这是我到目前为止尝试过的:
struct LuaCallbackEvent {
int32_t delay;
int32_t ref;
LuaCallbackEvent() : delay(0), ref(0) { }
};
int32_t _lua_createEvent(lua_State* L) {
// Callback delay
auto delay = lua_tonumber(L, 2);
// Lua callback reference
auto lua_cb = luaL_ref(L, LUA_REGISTRYINDEX);
LuaCallbackEvent cb;
cb.delay = delay;
cb.ref = lua_cb;
EventManager::instance().CreateLuaCallback(cb);
return 0;
}
// Gets called when specfic time has elapsed
void ExecuteLuaCallback(LuaCallbackEvent* ev) {
if (!L) {
LERR << "No state";
return;
}
// Retrieve callback by the reference
lua_rawgeti(L, LUA_REGISTRYINDEX, ev->ref);
// Execute callback
lua_pcall(L, 1, LUA_MULTRET, 0);
// Unset reference
luaL_unref(L, LUA_REGISTRYINDEX, ev->ref);
}