lua-users home
lua-l archive

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


I'm sure this must be a pretty common situation, but I can't seem to find a solution anywhere.  Pointers to an existing solution would be greatly appreciated.

I have implemented a callback system that lets users register Lua functions as handlers/listeners for events received by objects like this:

local myListener = function(event)

  -- do something here

end 


someObject:addEventListener("myEvent", myListener)



Internally, my C code calls luaL_ref to get a reference to the callback function and then stores that reference in an array of callbacks associated with the event, in this case "myEvent".  These references are then used to call the handlers when the event occurs.

This works fine, but I want to be able to remove the listener using the same syntax:

someObject:removeEventListener("myEvent", myListener)

The problem with this is that I can't think of a way to associate the function on the stack at this point (myListener), with the reference I stored earlier.  Calling luaL_ref again with the same function simply generates a new reference.  So apparently I need to store something else to identify the listener in addition to the reference, but I'm not sure what.

Any suggestions would be greatly appreciated.

Thanks,

James
-- 
James Norton
Sent with Sparrow