pioneerspacesim/pioneer — pull request #2161 by robn (2013-03-27 06:03:06+00:00)
Title: Object properties

The idea here is to allow objects to have properties that are referenced by name instead of directly (by pointer or reference), and looked up at runtime. Effectively its attaching a dictionary/hashtable to a C++ object.

(In fact, since the implementation is backed by a Lua table, that's exactly what it is).

This gets us some things:

* We can annotate objects with any arbitrary values we like at runtime.
* We can pass an object reference and an property name to something to look up later, like a kind of lazy evaluation or dynamic dispatch (not quite, but similar in concept).
* We can get notified when a property changes, since set/get requires code and not just flipping bits in memory.

That last two are interesting to me. I want UI widgets that can be updated when a value changes, but of course the UI is written in Lua. This would normally require some way of calling out to Lua when a value changes, which is expensive. With this (and some more stuff on the UI side, coming in another PR) we can tell an interested widget when a property changes, and it can then look up the property itself and make an appropriate update without Lua having to get involved in the update process at all. But because the property has a proper name, Lua can reference it during setup.

The downside to all this is that its slower to update properties than just setting a C++ field directly. For that reason I've kept the original fields where appropriate, and only update the property when it actually changes.

This PR is just groundwork - it does nothing by itself. If you want to test the feature itself, use the branch `robn/object-properties-lua-function`. This allows a Lua function to connect to a property. Try this in the console: `Game.player:Connect("fuel", function () print(Game.player.fuel) end)`. Of course, also make sure that the rest of Lua stuff works - this changes stuff in the dispatcher and in the existing attributes.