Tricks of the Windows Game Programming Gurus:Making Silicon Think with Artificial Intelligence

All ITKnowledge Source Code Search Tips Advanced Search |

| Tricks of the Windows Game Programming Gurus by Andre LaMothe Sams, Macmillan Computer Publishing ISBN: 0672313618 Pub Date: 09/27/99

Search this book:

Previous | Table of Contents | Next

#### Using the C/C++ Compiler

The nice thing about using an interpreted language is that the engine can read it and the game doesn’t need to be recompiled. If you don’t mind your game designers compiling (they should know how to anyway), you can use an old trick to make a crude game-scripting language: Use the C/C++ preprocessor to translate your scripting language for you. It takes nothing more than header files and the C/C++ source, which have nothing to do with compiler design.

The C/C++ preprocessor is really an amazing tool. It enables you to perform symbolic referencing, substitutions, comparisons, math, and a lot more. If you don’t mind using C/C++ as the root language and compiling your scripts, you might be able to write your scripting language by means of a clever design, a lot of text substitutions, a lot of canned functions, identifiers to refer to objects, and a good object-oriented design.

Of course, under it is going to be real C/C++, but you don’t have to tell your game designers that (if you have any). Or you can force them to use only the pseudo-language and not use all the real C/C++ functionality.

The best way to show you this is with a very simple example (that’s all I have time for). First, the scripting language will be compiled and each script will be run whenever the object it refers to is created. The script will be terminated when the object it refers to dies. The scripting language you’re going to write is based on C, so I’m not going to go over everything there. But I am going to use text substitutions for a lot of new keywords and data types.

A script consists of these parts:

  Globals section—This is where any global variables that are used in the script are defined. There are only two types of data type: `REAL` and `INTEGER`. `REAL` holds real numbers like the C-type `float`, and `INTEGER` is similar to the C-type `int`.
  Functions section—This section is composed of functions. Functions all have this syntax:

         data_type FUNCNAME(data_type parm1, data_type parm2…)

         BEGIN

         // code

         ENDFUNC

  Main section—The main of the program is where execution begins, and it will continue to loop here until the object is dead:

         BEGINMAIN

         // code

         ENDMAIN

As for variable assignment and comparison, only the following operators will be valid:

Assignment | `variable = expression;`
Equality | `(expression EQUALS expression)`
Not Equal | `(expression NOTEQUAL expression)`

Comparisons—Greater than, less than, greater than or equal to, and less than or equal to all use the same C standard, which follows:

    (expression > expression)

    (expression < expression)

    (expression >= expression)

    (expression <= expression)

Conditionals—The form of conditional statements is the same as C, except that the code that executes when the statement is `TRUE` must be contained within a `BEGIN ENDIF` block. Look at the following example:

    if (a EQUALS b)

       BEGIN

       // code

       ENDIF

    else

       BEGIN

       // code

       ENDELSE

Similarly, the `else` block must be contained within a `BEGIN ENDELSE` block as well as the `elseif`.

There are no switch statements in this language, and there’s only one kind of loop in the language, called a `WHILE` loop:

    WHILE(condition)

         BEGIN

         // code

         ENDWHILE

Next, there’s a `GOTO` keyword that jumps from one point in the code to another. The jump must be labeled with a name of the form:

    LBL_NAME:

where `NAME` can be any character string up to 16 characters in length. See the following example:

    LBL_DEAD:

    if (a EQUALS b) BEGIN

    GOTO LBL_DEAD;

    ENDIF

You probably get the point by now. Of course, you’d want to add dozens or even hundreds of high-level helper functions that could perform tests on objects. For example, for objects that have a health or life state, you could have a function called `HEALTH()`:

    if (HEALTH("alien1";) > 50)

        BEGIN

        // code

        ENDIF

Previous | Table of Contents | Next

Copyright © Macmillan Computer Publishing, Inc.

Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.