pioneerspacesim/pioneer — pull request #1441 by robn (2012-08-17 23:59:00+00:00)
Title: Lua event queues

This moves the EventQueue system out into Lua. The core `LuaEventQueue` class is now a light wrapper that simply calls down into the proper Lua method. All the dispatch and queuing is done there.

Original motivation for this was @laarmen working on reimplementing `Ship.Jettison()` in Lua. That needs to fire the `onJettison` event. Taken to its logical conclusion there's no need for that even to even exist in the game core, so by pushing the event system out to Lua individual modules etc can setup their own events.

The code is simpler now too :)

Uncertainties at this point:
* This needs to load early, since the alphanumerically-first module, libs/Characters.lua wants to register event handlers. Too force the issue I've named this `00-EventQueue.lua`. Another reminder that we need some sort of dependency/require arrangement.
* I haven't gone to any great pains in the core to deal with a broken or missing Lua-side implementation. If the queue is not found or the EventQueue table is bogus, it will trip asserts. It could be made safer (try to trigger an event that doesn't exist could just be a no-op) but I don't know how important it really is.
* The `Queue`, `Signal`, `Emit` and `Clear` are now exposed to Lua. This may be a bad idea, or not close enough to where we need to be. `Signal` I've been wary of in the past, though I suppose there's much less concern about raciness if its triggered from Lua anyway. `Emit` is normally called at end of frame. We might need to add a hook or flag or something so a queue can request to be called once per frame too.
`Clear` is called at game end for the core events, so it may be valuable for the event system to register its own `onGameEnd` and flush out the queues to prevent events leaking into the next game. (Or Lua restart between games, but we know about this already). Anyway, because of my uncertainty about all of this I've marked the whole API "experimental" (for what that's worth).

This might be better built on top of `LuaTable` (#1438). I didn't want to depend on that yet though.

This may make #630 easier.

I feel like there's something else I wanted t bring up, but I'm getting really sleepy now and its not coming to me.