Clean integration of Lua and C

Ever since thinking about building my own amateur game engine, I’ve wanted to integrate scripting in it. Because I believe the advantages to outweigh the downsides.

Exemplary code, with redis

Whenever possible, I like to look at how well-written, modern projects handle this. In this case that’s easy, redis has lua scripting! Quite a lot of things in redis are small, efficient, self-contained and liberally licensed (MIT), which makes it excellent to study and learn from.

By the way, I think it’s good practice to look for ideas in other projects. The trick is to remember some projects that you can have look at, confident that the author knows what she is doing. For me, that project is redis.

The other side of the coin though is that it can stifle your creativity. So maybe a good middle ground is to only start looking at things if you can’t find a solution in 20 minutes or so.

Let’s take the redis scripting code as an example:

Sandboxing

This section is based on an answer from stack overflow, and if we look carefully we can see that the developers of redis took the same approach in scripting.c.

Some other tutorials if you still have questions:

Alternatives to Lua to consider, that share some of its advantages (easy integration, …):

Suggestions, comments? Feel free to send me a mail and if possible I’ll amend the article or just write a Part II.