O'Reilly logo

Programming Game AI by Example by Mat Buckland

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

n
Homeworld 2
n
Far Cry
Lua has gained this popularity because it’s extremely powerful and fast (for
a scripting language), yet lightweight and simple to use. The icing on the
cake is that it’s also very portable, free, and open-source.
It’s impossible within the space of one chapter to describe all the features
of Lua, but I can provide you with a decent introduction, certainly enough
to whet your appetite and to show you how Lua can be used effectively in
your own games.
Let’s get started then…
Setting Up Your Compiler to Work with Lua
The Lua headers and libraries can be found in the common/lua-5.0 folder
in the downloadable files. You must direct your compiler to search for the
Lua header files in the common/lua-5.0/include folder, and for the lua
libraries in the common/lua-5.0/lib folder. When you create a project, make
sure you add the Lua libraries: lua.lib, lualib.lib, and lauxlib.lib.
Getting Started
Before you learn how to interface your C/C++ programs with Lua, you
need to know how to program using the Lua programming language. With
this in mind, the next few pages will be spent giving you a tour of Lua to
familiarize you with the data types and syntax. Fortunately, Lua is very
easy to learn and it will only take you a short while before you feel compe-
tent enough to start writing your own scripts. Once you are familiar with
256 | Chapter 6
Scripting in Lua
Screenshot 6.6. Impossible Creatures
Ó Relic Entertainment, Inc.
Content removed due to copyright restrictions
the language, I’ll demonstrate how Lua variables and functions can be
accessed from within C++ and vice versa. We’ll then spend some time
examining how C++ classes can be exposed to Lua before moving on to a
little project that ties everything together.
Ü
NOTE Although this chapter will show you enough to get you started, it’s
impossible to do the language justice in just one chapter. Therefore, I highly rec
-
ommend you read the Lua documentation and visit the Lua user’s wiki online at
http://lua-users.org/wiki/.
Lua comes with an interactive interpreter (common/lua-5.0/bin/lua.exe)
you can use to try out little snippets of code by typing them directly into
the console at the command prompt, but for anything more than a couple of
lines you may find this tedious. I think the best way to start your journey
into Lua is by showing you how to run a script using the C/C++ Lua API.
This way you can use the familiar environment of your compilers IDE to
write and run scripts.
Let me talk you through the code required to run a Lua script from a
C/C++ program.
extern "C"
{
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}
First you have to include the relevant header files. Since Lua is a pure C
library, you must let the compiler know this explicitly or you’ll experience
problems. This is done by encasing the
#includes with extern “C”.
//include the lua libraries. If your compiler doesn't support this pragma
//then don't forget to add the libraries in your project settings!
#pragma comment(lib, "lua.lib")
#pragma comment(lib, "lualib.lib")
#include <iostream>
int main()
{
//create a lua state
lua_State* pL = lua_open();
Each script file you run will be executed in a dynamically allocated struc
-
ture called a
lua_State. Every function in the Lua library requires that a
pointer to a
lua_State be passed to it as a parameter. Therefore, before run
-
ning a script file you must create a Lua state by calling
lua_open.
//enable access to the standard libraries
luaopen_base(pL);
luaopen_string(pL);
luaopen_table(pL);
To Script, or Not to Script, That Is the Question
| 257
Scripting in Lua

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required