This is Part 1a of my Integrating Lua into C++ Tutorial series.

There are plenty of scripting languages to work with when binding with C++. Everyone have their personal favorite and mine is Lua.

There are plenty of great tutorials out there that teaches on how to integrate C++ with Lua but I feel that those tutorials are kind of outdated. I feel that there are better ways to do things in 2018. As such, this tutorial counts on utilizing more updated libraries compared to most tutorials out there.

Why use a scripting language?

Source codes are usually compiled at compile-time while scripting languages are mostly compiled at run-time. As a game gets bigger, one might notice that compiling the entire code-base would take a long time, which results to a huge productivity hit.

As such, being able to tweak game logic without the need to recompile the code-base would result in a significant productivity boost. There are always people out there complaining about the time it takes to compile a code-base.

Some scripting languages, such as Lua, is also easy to learn. This means that non-programmers would be able to provide gameplay behaviors without having access to the main source codes.

With having an environment for the non-programmers to code (e.g. your scripting language), the programmers can effectively constraint the working environment of the non-programmers, for better or for worse. This ensures that the non-programmers are not able to overstep some boundaries, and write game breaking code out of the context that they were supposed to work in.

Scripting languages also makes it super easy to add modding support to your game. You can easily allow your players to modify gameplay features by giving them limited access to your scripting platform. For example, look at Binding of Isaac.

Finally, being able to refresh your code at a press of a button during run-time is really really neat.

Why should I not use a scripting language?

Debugging for scripting languages is a big pain in the ass, unless you are going to make an IDE for your scripting language. It is sadly not as easy to debug as compared to using a host language (like c++), which have so many fancy tools (visual studio, etc.)

The major turn-off for most will be the performance. It is a main issue that scripting languages will always be slower compared to it’s source code equivalent. If speed is a critical must, then it’s better to run everything on the main source code.

Some prerequisites before we start.

Sol2 Lua Binding Library for C++

Sol2 is a C++ library binding to Lua. It is really easy to set up, and easy to use.

There are plenty of tutorials out there on the github page. If you would like to know more, I would recommend you to go to the github page and follow the tutorials there for more grasp on the topic. There are plenty of great tutorials there on how to use the library.

Here’s the link!

Lua Library

You’re going to need download the Lua library. You can go to the Lua website and download the library. There’s no need to compile the library. Just download the pre-compiled binaries.

Go to Download -> Binaries

and get the version you need.

Here’s the link!

Middleclass Library

This is an OOP library for Lua.  It is very lightweight, easy to use, and to set up.

The github page have really good example that shows everything you need to know about using the library. Take a look at the page to see how to use the library out of the scope of the tutorial.

Here’s the link!

Next (Part 1b: The Basics)