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 actually 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 basic 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.
As usual here at SOTC, we are going to be using visual studio, and in this case 2008. Visual C++ is pretty close to normal 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 slightly special because we do not want pre-compiled headers for this project. By default, it 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 headers 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 really less than user friendly, so I will point you in the right direction. What you want to do is go to the Binaries Downloads and pickup the "Windows x86 DLL and Includes", which like it sounds, includes the C files we will need to get the job done. I have also included in the source files below, a class that was given to me that makes things even easier. I do not know the original source of the class, but the Script class is a must for simple actions between Lua and C++. If you are really feeling a little lazy, you can just 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 just copy them. Remember that the includes folder in the Lua package should not be copied, just the contents inside. So your project folder should look something like this:
Notice that all of the files are in one folder, there are no sub-folders. This makes it easier on everyone. It make look 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 basic of c++ here, I will just list the code:
Its really 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 just play with getting variables from Lua. But first, we need a Lua script to use. My script looks something like this:
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 in into c++. To do that, we need to use the
string sName = "script.lua";
if(script.loadScript(sName.c_str()) == false)
return 1;
As you can see, all you really 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 very easy methods called getGlobal. There is one method for each primitive data types, so we can use them with the following:
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 really 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 re-compiling 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 just 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. Just remember thought, when you need programming help, all you have to do is switch on the code.
Add Comment
[language] [/language]
Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]
See here for supported languages.
Javascript must be enabled to submit anonymous comments - or you can login.