Data types
Operators
if, else statements
while
import
export
| 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 |
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 operators of equal precedence, AGS 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.
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))
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.");
}
Runs statements continuously, while expression is true.
For example:
while (character[EGO].walking != 0) {
Wait(1);
}
will run the script Wait(1); repeatedly, as long as character[EGO].walking is
not zero. Once it is zero, the while statement will exit at the end of the loop.
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.
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.