Metric Panda Games

One pixel at a time.

Embedding Lua for Modding

Rival Fortress Update #8

The engine for Rival Fortress is coming along nicely, and in a few weeks I’ll probably start working on game code.

This past week I integrated LuaJIT in the engine. LuaJIT is a just in time interpreter for the Lua language that is lightweight and very fast.

Integrating LuaJIT

I’m statically linking the LuaJIT runtime as I intend to make some modifications to the implementation, especially to the memory allocation routines.

Other than that integration was quick an painless. The only tedious part was converting the Makefile based build system that LuaJIT uses into my CMake toolchain. If you are looking to do something similar I suggest starting from the CMakeLists.txt in the luajit-rocks repo.

Exposing an API to Lua

In the engine I’m using Lua to expose an API that can be consumed by scripts and used in modding.

The API consists of a global Lua table that contains various functions that the modder can use to initialize the mod and register callbacks to specific events in the game lifetime. The API is still quite bare bones, as I intend to expose functionality as I need it while working on the actual game code. The idea is that most of the game specific logic will be implemented in Lua within the constraints of the engine.

Using reflection to expose functionality

I’m using the reflection system I talked about the previous week to expose functionality to Lua. I do so by annotating structs and functions and the reflection system generates the boilerplate code needed to glue C and Lua together.

This is particularly useful for handling Lua-to-C type conversion and Lua Stack management.