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.

C++ Tutorial – Sorting STL Containers

Welcome to the first C++ tutorial ever written in here. Our tutorials are typically centered around C# and Flex, however lots of people in the world (including me) program in C++ every day – and the standard template library is an integral part of any modern C++ project. This tutorial is going to demonstrate how to use the built-in algorithms to perform custom sorting on standard template library collections.

The STL object I’m going to be using as my example will be a vector, which is one of several containers offered by the library. To use a vector, you’ll have to include <vector>. All of the algorithms, like sort, are located in <algorithm>. And of course, everything inside the standard template library is located in the std namespace.

Let’s start by using the simplest form of sort.

using namespace std;

//create a vector
vector<int> v;

//populate the vector
v.push_back(2);
v.push_back(3);
v.push_back(1);
v.push_back(5);
v.push_back(4);

//sort the vector
sort(v.begin(), v.end());

//v = {1, 2, 3, 4, 5}

This use of the sort function will sort the elements by applying the < operator on each element. So if you were populating this vector with your custom objects, you’d have to define the < operator to sort them. The sort function takes two iterators, which are essentially pointers to objects inside the collection. The begin() and end() functions return iterators that point to the first element and the last element respectively. Since sort will sort the elements between these two iterators, you could sort just a portion of the vector if you wanted:

using namespace std;
 
 //create a vector
 vector<int> v;
 
 //populate the vector
 v.push_back(2);
 v.push_back(3);
 v.push_back(1);
 v.push_back(5);
 v.push_back(4);
 
 //sort the last two elements
 sort(v.begin() + 3, v.end());
 
 //v = {2, 3, 1, 4, 5}

All right, enough with iterators. Let’s look at another version of the sort function. One that takes a functor instead of using the < operator.

Using namespace std;
 
 //create a vector
 vector<int> v;
 
 //populate the vector
 v.push_back(2);
 v.push_back(3);
 v.push_back(1);
 v.push_back(5);
 v.push_back(4);
 
 struct reverseSort
 {
  bool operator()(int a, int b) { return b < a; }
 };
 
 //sort the elements using custom functor
 sort(v.begin(), v.end(), reverseSort());
 
 //v = {5, 4, 3, 2, 1}

Functors are typically just functions or function pointers, but they are also objects that define the () operator. Here I created a struct that defines the () operator and takes two integers as arguments. The sort function will now use this functor to compare objects in my collection. In my example, I reversed the default comparer, which will sort the vector in the reverse order – largest to smallest.

The functor object passed into sort must be a Binary Predicate – that is a function that takes two arguments and returns a boolean. The sort function is also not stable, meaning if two objects resolve to the same value, their orders in the list are not guaranteed to be the same as they were before sort was called. If stability is required, stable_sort will get the job done.

That’s pretty much it for sorting standard template library containers. The sort function uses the introsort algorithm, which has a complexity of O(N log(N)) and is a hybrid of QuickSort and HeapSort.