lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I'm new to Lua so I apologize for any horrible mistakes I'm making
here. I have a timer that I want to run two pieces of code in Lua
periodically. Here's my attempt so far:

-- Lua code
delay = 10
t = Timer(delay, function() [code] end, function() [code] end)

//C++ code
Timer::Timer(int x, int y, ??? luaFunction1, ??? luaFunction2)// I
don't know what object they'd be
{
//is any of this right?
lua_pushvalue( L, -1 ); // this is meant to push the function onto the stack
m_timerFunction2 = luaL_ref( L, LUA_REGISTRYINDEX ); //which I then
move to the registry
lua_pushvalue( L, -1 );
m_timerFunction1 = luaL_ref( L, LUA_REGISTRYINDEX );
}

Timer::ExecuteTimer1() //just making up a simple name for the example
{
lua_rawgeti( L, LUA_REGISTRYINDEX, m_timerFunction1 );
lua_call( L, 0, 0 ); //is this right?
}

//something like this?
Timer::~Timer()
{
luaL_unref( L, LUA_REGISTRYINDEX, m_timerFunction1 );
luaL_unref( L, LUA_REGISTRYINDEX, m_timerFunction2 );
}
}