[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Metatable __newindex on registered table of C functions
- From: "John Dunn" <John_Dunn@...>
- Date: Wed, 13 Aug 2008 14:03:05 -0700
I'm registering some C functions in a table with the following code
static const struct luaL_reg timer_class_methods [] =
{
{ "Start", timer_start },
{ "Stop", timer_stop },
{ "AddEventHandler", timer_addEventHandler },
{NULL,NULL}
};
void script_timer_manager_t::init( lua_State* L )
{
luaL_register( L, lua_timer_class_name, timer_class_methods );
lua_pop(L, 1);
}
The user can then add a timer tick handler with code like
Timer.AddEventHandler( function() .... end )
What I'd like to be able to do is to have the user add the event handler
with a more Property like interface. Something like
Timer.EventHandler = function() ... end
I thought the correct way to go about this would be to create a new
table with __newindex method and then assign that as the metatable of my
Timer table.
function mt__newindex(k, v)
if k == 'EventHandler' then
Timer.AddEventHandler(v)
end
end
local mt = {}
mt.__newindex = mt__newindex
setmetatable(Timer,mt)
That doesn't appear to work, although accessing the __newindex directly
does
-- this works
mt = getmetatable(Timer)
mt.__newindex("EventHandler", TimerFunc)
-- this doesn't
Timer.EventHandler = TimerFunc