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

Chapter 6
To Script, or Not to Script,
That Is the Question
S
cripting languages are rapidly gaining popularity with game develop-
ers. You only have to listen to the buzz at developer conferences or see
the amount of discussion regarding scripting on the Internet developer
forums to know what a hot topic they have become. Several big-name
developers have started to use scripting extensively in their titles. Epic
Games’ Unreal Tournament series, BioWare’s Neverwinter Nights, and
Crytek’s Far Cry all utilize scripting languages.
To appreciate what all the fuss is about you need to know what a scripting
language is before you understand how a game can benefit from using one.
Just What Is a Scripting Language?
As projects grow in size, the time required to compile the source code
increases. As we all know, this can be a real pain in the backside. Changing
just a couple of constants can result in a lengthy rebuild. For this reason,
249
Screenshot 6.1. Unreal Tournament 2003
Ó Epic Games, Inc.
Content removed due to copyright restrictions
it’s common practice to place many of the constants in a separate initializa-
tion file and create code to read and parse that file. This way, if you want to
change some values, you don’t need to recompile the project; you only
have to change the values in the initialization/configuration file — usually
a simple text file. You may be surprised to know the use of an initialization
file like this is a rudimentary form of scripting and the text the initialization
file contains is a very basic scripting language.
A more advanced scripting language increases the interaction between
the script and the executable, enabling you to not only initialize variables
but to create game logic or even game objects, all from one or more script
files. These script files are run from within your program by something
called a virtual machine, or VM for short. I’m not going to go into the
details of virtual machines in this book — it’s too low level and I don’t
think it’s appropriate — but it helps to think of a VM as an emulated CPU
sitting snugly inside your game’s executable (your web browser, for exam-
ple, uses a virtual machine to run Java code). You write functions in the
syntax of the scripting language, which are read and then run inside the
virtual machine. The beauty of scripting is that the virtual machine can
communicate with the language inside which it resides (in our case C++),
enabling data to easily be passed back and forth.
Scripts can either be interpreted or compiled. An interpreted script exists
in the same format in which it is written — the human readable scripting
language itself — and is read, parsed, and executed line by line by some-
thing called an interpreter. As this can be a slow process to do on-the-fly,
some interpreted scripting languages automatically compile the script
before it’s executed. Another problem with interpreted scripts is that they
can easily be understood and edited by game players who like nothing
better than to give themselves an unfair advantage.
250 | Chapter 6
Just What Is a Scripting Language?
Screenshot 6.2. Black & White
Ó Lionhead Studios Limited
Content removed due to copyright restrictions

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