code snippet: luatables simple access to lua tables from C/C++

June 5, 2012

The last two gamejams definitely convinced me that Lua is a very elegant, fast and easy to learn scripting language. Combined with the amazing LÖVE engine really cool things can happen.

When I created on my simple skeletal animation tool MeshUp, I thought that using JSON as a fileformat would be way better than XML as it is not as verbose and therefore easier to read. Also there is a nice C++ library for reading and writing JSON files.

However the syntax of JSON is actually fairly similar to Lua so I thought it would be a nice thing to have scripting powers in the model description files as it empowers you to even … script repetitive tasks!!!

The only problem was that the interface to get values from Lua into C can be a little counter intuitive as one has to interact with Lua through its stack. I do have to not here that the Lua stack is – just as the language itself – a very elegant way for passing data in and out of Lua’s virtual machine. But as said before it is not as intuitive when one wants to do simple things.

For this I hacked together a small set of functions, called luatables, that should simplify using Lua as a fileformat. There are of course various libraries already available, but I wanted a) get to know Lua a bit better and b) have a lightweight solution that does not add bloat or dependencies.

I published it under the (very permissive) zlib open-source license. You can grab it from:

Enjoy!