pioneerspacesim/pioneer — Issue #630: Lua event filters
Author: robn
Created: 2011-10-08 00:43:38+00:00
State: open
Labels: Feature request, To do

Right now some Lua events (notably the `onShip*` ones) are too broad. A script gets sent events for every event of that type even when its likely that most of the time its not interesting (not their ship). There's significant overhead in calling into Lua only to have it return. Ideally we would only call into Lua when we know that something is actually interested. I propose a new `EventFilter` object that works as follows:

-- register the handler with the queue, get a filter back
local filter = EventQueue.onShipCollided:Connect(function (ship, other) ... end)

-- add interesting objects we're interested in to the filter
filter:Add(ship, ship2, ...)

-- no longer interested in something
filter:Remove(ship2)

Notes, off the top of my head:

* Maybe it should not be an `EventFilter` but an `EventConnection`. The distinction isn't important but might save a rename later if we need to add more per-handler state. Also it means we could do `EventQueue.onShipCollided:Disconnect(connection)`.

* Remove should happen automatically when the object is deleted (ie catch the signal from the `DeleteEmitter`)

* We'll need some more template shenanigans on the C++ side to make sure Lua is trying to filter on the right types of things.

* I'm not sure if a filter with nothing added to it should match no events (ideal) or all events (backwards-compat). I'm inclined towards no events at this stage - we have very few scripts so the changes should be easy and it makes it harder to get it wrong by accident.

* If the default is "no events" then we might need a way to say "give me everything".

* Most of the time the filter can probably be by body, particularly for the `onShip*` events. Some events (`onGameStart` etc, maybe low-impact ones like `onEnterSystem`) probably don't need filters at all, but maybe should for consistency.

I only worked this out this morning after some discussion with @jaj22, so please pick it apart.