Contents Up Previous Next

Script language keywords

Data types
Operators
Constants
if, else statements
while
function
enum
import
export


Data types

Type Description
char Single byte data type, can store a single character or number 0 to 255
short 16-bit integer, can store numbers from –32,768 to 32,767
int 32-bit integer, can store from –2,147,483,648 to 2,147,483,647
string Stores a string of up to 200 characters
float A 32-bit floating point number. Accuracy normally about 6 decimal places, but varies depending on the size of the number being stored.

You will normally only need to use the int and string data types. The smaller types are only useful for conserving memory if you are creating a very large number of variables.

To declare a variable, write the type followed by the variable name, then a semicolon. For example:

int my_variable;

declares a new 32-bit integer called my_variable

WARNING: When using the float data type, you may find that the == and != operators don't seem to work properly. For example:

float result = 2.0 * 3.0;
if (result == 6.0) {
  Display("Result is 6!");
}
may not always work. This is due to the nature of floating point variables, and the solution is to code like this:
float result = 2.0 * 3.0;
if ((result > 5.99) && (result < 6.01)) {
  Display("Result is 6!");
}
The way floating point numbers are stored means that 6 might actually be stored as 6.000001 or 5.999999; this is a common gotcha to all programming languages so just be aware of it if you use any floating point arithmetic.


Operators

The AGS scripting engine supports the following operators in expressions. They are listed in order of precedence, with the most tightly bound at the top of the list.

WARNING: When using operators of equal precedence, AGS by default evaluates them right-to-left. So, the expression a = 5 - 4 - 2; evaluates as a = 5 - (4 - 2); which is not what you might expect. Always use parenthesis to make it clear what you want.
The "Left-to-right operator precedence" option on the General Settings pane allows you to control this behaviour.


Operator  Description              Example

  !    NOT                        if (!a) 
  *    Multiply                   a = b * c;
  /    Divide                     a = b / c;
  %    Remainder                  a = b % c;
  +    Add                        a = b + c;
  -    Subtract                   a = b - c;
  <<   Bitwise Left Shift         a = b << c;
       (advanced users only) 
  >>   Bitwise Right Shift        a = b >> c;
       (advanced users only) 
  &    Bitwise AND                a = b & c;
       (advanced users only) 
  |    Bitwise OR                 a = b | c;
       (advanced users only) 
  ^    Bitwise XOR                a = b ^ c;
       (advanced users only) 
  ==   Is equal to                if (a == b)
  !=   Is not equal to            if (a != b)
  >    Is greater than            if (a > b)
  <    Is less than               if (a < b)
  >=   Is greater than or equal   if (a >= b)
  <=   Is less than or equal      if (a <= b)
  &&   Logical AND                if (a && b)
  ||   Logical OR                 if (a || b)
This order of precedence allows expressions such as the following to evaluate as expected:

if (!a && b < 4)

which will execute the 'if' block if a is 0 and b is less than 4.

However, it is always good practice to use parenthesis to group expressions. It's much more readable to script the above expression like this:

if ((!a) && (b < 4))


Constants

The following predefined macros are available in your scripts:

Name Description
DEBUG Defined if the game is being compiled in debug mode, not defined otherwise
STRICT Defined if "Enforce Object Based Scripting" is ticked, not defined otherwise
LRPRECEDENCE Defined if "Left-to-right operator precedence" is ticked, not defined otherwise
AGS_MAX_CHARACTERS The maximum number of characters
AGS_MAX_INV_ITEMS The maximum number of inventory items
AGS_MAX_GUIS The maximum number of GUIs
AGS_MAX_VIEWS The maximum number of views
AGS_MAX_LOOPS_PER_VIEW The maximum number of loops in a view
AGS_MAX_FRAMES_PER_LOOP The maximum number of frames in a loop
AGS_MAX_OBJECTS The maximum objects per room
AGS_MAX_HOTSPOTS The maximum hotspots per room
AGS_MAX_REGIONS The maximum regions per room

You can check for whether a macro is defined or not by using the #ifdef and #ifndef keywords:

#ifndef STRICT
  // only compile the MoveCharacter command if not using object-based scripting
  MoveCharacter(EGO, 30, 40);
#endif
#ifdef DEBUG
  // only display this when the game is compiled in debug mode
  Display("Debugging information");
#endif
The other constants (AGS_MAX_*) are useful if you are writing some script code that you want to be portable to different versions of AGS, and to pick up the limits from the user's AGS version. For example, if you wanted to store some extra information on all the characters, you could do:
int charJibbles[AGS_MAX_CHARACTERS];
To get the actual number of things in the game rather than the AGS limit, use the GetGameParameter function.


if, else statements

if ( expression ) {
statements1
}
[ else {
statements2
} ]

If expression is true, then statements1 are run.

If expression is not true, and there is an else clause present, then statements2 are run instead.

For example:

if (GetGlobalInt(5) == 10) {
  Display("Globalint 5 is 10.");
}
else {
  Display("Globalint 5 is not 10.");
}
In this example, the first message will be displayed if the return value from GetGlobalInt(5) is 10, and the second message will be displayed if it is not.

if statements can be nested inside else statements to produce an "else if" effect. For example:

if (GetGlobalInt(5) == 1) {
  Display("Globalint 5 is 1.");
}
else if (GetGlobalInt(5) == 2) {
  Display("Globalint 5 is 2.");
}
else {
  Display("Globalint 5 is not 1 or 2.");
}

while

while ( expression ) {
statements
}

Runs statements continuously, while expression is true.

For example:

while (character[EGO].Moving) {
  Wait(1);
}
will run the script Wait(1); repeatedly, as long as character[EGO].Moving is not zero. Once it is zero, the while statement will exit at the end of the loop.


function

function name ( [type1 param1, type2 param2, ... ] )

Declares a custom function in your script. A function is a way in which you can seperate out commonly used code into its own place, and thus avoid duplicating code.

For example, suppose that you quite often want to play a sound and add an inventory item at the same time. You could write both commands each time, or you could define a custom function:

function AddInvAndPlaySound(int inventoryItem) {
  player.AddInventory(inventory[inventoryItem]);
  PlaySound(5);
}
then, elsewhere in your code you can simply call:
AddInvAndPlaySound(8);
to add inventory item 8 and play the sound.

Generally, you place your functions in your global script. You then need to add an import line to your script header to allow the function to be called from room scripts.


enum

Recommended for advanced users only

enum name {
option1 [ = value1 ],
option2 [ = value2 ],
...
};

Declares an enumeration type. An enumeration allows you to group together a set of related options, where only one will be true at any one time -- a bit like the contents of a list box.

For example, if you have a script function, doStuff, that can perform 3 different operations, you could do this:

function doStuff(int param) {
  if (param == 1) {
    // do something
  }
  else if (param == 2) {
    // do something else
  }
  // etc
}
but it's hard to read, and when calling the function from elsewhere in your script, it's not clear what 1 or 2 means. That's where enums come in:

enum DoStuffOption {
  BakeCake,
  DoLaundry
};

function doStuff(DoStuffOption param) {
  if (param == BakeCake) {
    // do something
  }
  else if (param == DoLaundry) {
    // do something else
  }
  // etc
}
and then the calling code looks like:
doStuff(BakeCake);
thus making it perfectly clear what the command will do.

Normally, you would put the enum definition into the script header.

In summary, enums are not an essential part of scripting and you can get away perfectly well without using them, but in some specific situations they're very handy.


import

import declaration ;

Declares declaration as a variable or function which is external to the current script, but that the script needs access to it. You use this to provide your room scripts with access to parts of your global script.

For example:

import int counter;
import function add_numbers (int, int);
This imports an integer variable counter and the function add_numbers from the global script to enable the current script to call them. You normally place import statements into the script header so that all rooms can benefit from them.

In order to import the variable, it must have been exported from the global script with the export keyword.

NOTE: You MUST import external variables with the correct type. If counter was declared as a short in the global script, you MUST import it as a short, otherwise your game may crash.

NOTE: You cannot currently import string variables. Instead, you should use a global string.


export

export variable [, variable ... ] ;

Declares that variable can be exported and accessed by other scripts. You must place this at the end of your global script. You can export many variables with one export line.

For example:

export my_variable;
export counter, strength;
This exports three variables - my_variable, counter and strength.

Browser Based Help. Published by chm2web software.
1