Lua woes

Bad Lua!For all the great things Lua has done for my current game development endeavour, it is now proving to be a royal pain in the ass.

The problems began when I added a background loading system to load new scenes in a separate thread without interrupting play.  This all worked until it began loading Lua scripts.  At the time, there was a single Lua state (virtual machine) for all of the scripts.  This was awesome because communicating between scripts was extremely easy.  When the loading thread started adding new scripts to the Lua state which was currently in use by the main thread, however, things began to go horribly wrong.

This problem was easily solved by having one Lua state per script.  This also fixed problems with naming, as Lua has no notion of namespace.  Lua states are designed to execute in isolation, so communicating between them becomes extremely difficult.  Implementing game logic without entity communication is neigh on impossible.

There are a few potential solutions.  The first would be to construct some kind of message passing system on the C++ side.  When an actor wants to tell another actor something, it dispatches a message which is received by the actor on the next update and interpreted.  Another option would be to use Luabind’s call_function() template function.  This approach would require custom wrapper functions for each different function signature you’d need.

More on this later.