lua-users home
lua-l archive

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


2009/8/6 Colm Sloan <colmsloan@gmail.com>:
> 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?
> }

Unless you properly built Lua with C++ exceptions support, and Lua is
already on the call stack, you should use lua_pcall to make sure your
callback won't throw an error that is not caught.