Lua & C++ – The Basics

With Lua, almost anything is possible. It is akin to JS, but in some ways, it is so much more powerful. With World of Warcraft incorporating Lua right into its core, you have to wonder how powerful you can make it. One of the things that make Lua so powerful is its easy integration into C++ and C. With this integration; you can accomplish just about anything. Today, we are going to go through the steps to get some essential interaction between C++ and Lua.

So you may be asking yourself, “What is the major advantage of using Lua with C++?” Well, the answer is as simple as the question. You don’t have to compile Lua; therefore, you can change the script on the fly, and get different outcomes in the program. Most of the time, you use Lua for user interfaces, but the possibilities are almost limitless.

We are going to be using visual studio. Visual C++ is pretty close to standard C++, so if you want, you should be able to adapt this any way you want. But, to start, we have to create a project.

This case is unique because we do not want pre-compiled headers for this project. By default, pre-compiled headers are turned on, but you can disable them during the project creation process. For today, we are going with a C++ console application. The only difference is the header’s checkbox, which you need to uncheck. I have provided a nifty screenshot to help you out.

Ok, now that we have a shiny new C++ project ready to go, we need to get Lua all straightened out, which honestly can be a pain. The Lua site is less than user-friendly so that I will point you in the right direction. What you want to do is go to the Binaries Downloads and pick up the “Windows x86 DLL and Includes”, which like it sounds, includes the C files we will need to get the job done. I do not know the source of the class, but the Script class is a must for simple actions between Lua and C++. If you are feeling a little lazy, you can download the VS Solution, and that has everything you need.

Once you have everything extracted, what you need to do with your files is simple, put them all into your VS project folder. You can add existing items or what I prefer to do is open up the folders and copy them. Remember that the includes folder in the Lua package should not be copied, just the contents inside.

Notice that all of the files are in one folder, there are no sub-folders. This makes it easier for everyone. It makes it looks like a lot of files, but using the files is not difficult at all. In fact, to implement the simple use of a Lua script, all we have to do is include our special Script class. In VS, right-click on your project, and goto add then existing item. Then find your project folder and add script.h and script.cpp.

Once those are added, now you have to include the script class into your c++ project. Since I am assuming you know the basics of c++ here, I will list the code:

#include "Script.h"

It’s that simple. Just add some files and include the class. But using this class is even simpler. The class itself has only a few methods, and today we are going to play with getting variables from Lua. But first, we need a Lua script to use. My script looks something like this:

PROGRAM_NAME = "C++ Lua Script Testing"
 PROGRAM_VERSION = "0.1A"
 SCREENWIDTH = 1920   
 SCREENHEIGHT = 1200
 COLORDEPTH = 32
 FULLSCREEN = false

Pretty basic, so much so that you don’t have to understand Lua to know that all we have is variables. We are going to use c++ to grab and display these variables. First, using the same process as above, you need to add your Lua file to your project. Once added, you can load it into c++. To do that, we need to use the

Script script;
 string sName = "script.lua";
 
 if(script.loadScript(sName.c_str()) == false)
 return 1;

As you can see, all you have to do is my a Script object, then call the loadScript method. It will return false on an error, so we want to test for that. Now, once the script is loaded, we can grab variables from the script with a set of effortless methods called getGlobal. There is one method for each primitive data types so that we can use them with the following:

string name = script.getGlobalString("PROGRAM_NAME");
 string version = script.getGlobalString("PROGRAM_VERSION");
 int width = (int)script.getGlobalNumber("SCREENWIDTH");
 int height = (int)script.getGlobalNumber("SCREENHEIGHT");
 int color = (int)script.getGlobalNumber("COLORDEPTH");
 bool fScreen = script.getGlobalBoolean("FULLSCREEN");

It is that easy, at least using our nifty script class. There are limitations, but this will give you a good start. Once the values are loaded, you can, of course, do whatever you want. But what makes it better, is that you can change the values in the script, without recompiling the program. So you can change true to false, 10 to 20, and so on.

So for this tutorial, this is where we wrap it up. Before I wrap up, I want to note that there are other ways to accomplish this, but this is what I was thought, so if you have a better way, please share.