The Separation of Church and State

Or, the Separation of LUA and C++ code.

Texas is created in LUA and C++. The idea was to write as much of the game engine as possible in LUA, with the speed-fancy parts (graphics, sound, etc.) in C++. For the most part, this has gone smashingly and I will continue to develop games this way, only moreso (i.e., MORE in LUA and less in C++)

Sometimes, the LUA code has to call C++. This is a natural situation, for instance if the game engine (LUA)  needs to add a visual element, there is a function call add_sprite () that it can make. The C++ engine maintains state of sprites and other renderables, but it doesn't know what they are supposed to do. The higher level game logic is in LUA, and the C++ engine just draws and moves stuff around based on what it's told (by LUA).

Other times, the C++ code has to call LUA. For instance, the main game loop is in C++ and it makes a function call to update (). Or, when a mouse button is pressed this is first detected in the game engine (it's a low level event so it makes sense) but the mouse click is passed on to LUA for handling (the handling of it is high level).

So there is some back-and-forth between the two layers, with C++ handling the low level stuff and LUA handling the high level stuff.

Two layers: C++ for low level, LUA for high level. Ay, there's the rub!

There aren't two layers, there are three! But I didn't realize it until today and because I didn't realize it I was creating some unnecessary complexity for myself by violating a certain obvious hierarchy:

[C++ control logic (i.e., the main game loop, event handling, window management)]



[LUA game logic (i.e., menus, enemy AI, moving from area to area)]



[C++ game engine (i.e., renderables, sound effect state, that kind of thing)]

It's a basic computer science FACT that tree structures (well ordered sets) are simpler than graphs. Graphs can be extremely complex. So from a design perspective, if you can keep your game engine structured in a hierarchy as above, rather than think of it in terms of a graph with back-and-forth calls, you will be much happier.

Because I didn't see that there were really three layers instead of two, I was treating the interaction of LUA and C++ elements as a graph, with arrows back and forth:

[C++ low level code (hardware, rendering, etc.)]




[LUA high level code (enemy behaviour)]

This resulted in some messiness. Which I will now seek to remedy, as it's now causing me some real grief.



◀ Back