Varaibles are simply values that have names. All variables in AGAST are used to store
32-bit signed-integer values (any number included in the range from negative 2,147,483,648
to positive 2,147,483,647).
Here is an exhaustive example of all variable declarations. (The meanings of static and automatic are explained below.)
var a; // static variable
int b; // static variable
static var c; // static variable
static int d; // static variable
static e; // static variable
script anyOldScript
{
var a; // automatic variable
int b; // automatic variable
static var c; // static variable
static int d; // static variable
static e; // static variable
}
Game and Scene Scope
Depending on the context in which the variable is declared, it has game scope,
scene scope, or script scope. This determines from where the variable
can be "seen" and therefore accessed or modified.
Variables in game scope can be seen by any script in the game without restriction. It
is advisable to avoid declaring variables at game scope whenever possible, because it may lead
to interdependencies among scenes, which may convolute the design and maintainence of the game.
Only give game scope to a variable that must be accessed in more than one scene,
by declaring them in any script file in the game directory (outide of a script).
Variables that are only required by the scripts within a single scene should be given scene
scope, just by declaring them in any script file in the directory of their scene. If the name
of a variable is the same as one with game scope, the latter will be hidden from the scripts in
the same scene.
Variables declared inside scripts always have script scope, and can therefore only be
accessed or modified from within their surrounding script. If a script variable has the
same name a variable in game or scene scope (or both), the script variable will hide it
(or them).
Static and Automatic Script Variables
Variables at game and scene scope (sometimes called global variables) are said to be
static, because they never move; they exist for the duration of the game.
Script variables, on the other hand, are created automatically whenever their script is called
and destroyed automatically whenever it returns.
But local variables can be declared static too, if they are expected to retain their values
from call to call, or share the same value if several events call the same script at the same
time. The benefit of declaring them inside scripts is that the programmer will never be able
to modify them accidentally from within some other script.
Initialization
Variables can be initialized explicitly, otherwise they are initialized implicitly to zero.
var x = 10; // initialized explicity to 10
var j; // initialized implicity to 0
All static variables (that is, all variables declared outside of a script, and script variables
declared as static) are initialized when the game is started, before any events occur, and never
again. Automatic variables, in contrast, can be initialized every time their script is called,
just as if they were an ordinary assignment-statement.
script somethingIsWrong
{
var x = 10;
static var y = 10;
print "x is %d and y is also %d", x, y;
x = 20;
y = 20;
}
The first time somethingIsWrong is called, it will print
"x is 10 and y is also 10", but the second time it will print "x is
10 and y is also 20". That is because y has a static lifetime,
and is is only initialized once, at the beginning of the game, while
x is initialized every time this script is called.
Variable Arrays
int anArray[10];
Arrays of variables may be declared. Each element in the array is a static
variable. The number of elements given in the declaration must be a positive
constant or literal value, and are indexed from 0 to N minus 1.
Logical Values
Often it is useful for a variable to represent the logical value true or false
to track whether something has occurred, such as if the the player has talked to the crazy
old hermit, or has tried the beef jerky treats for dogs. A variable whose value is zero is
considered to be false, while a variable whose value is not zero is true.
Here is a common (mis)use of this fact. Most people would wonder what "while num" means, and
they should, because it is not readily intuitive. But it makes perfect sense to the compiler,
and to millions of programmers. It beeps 100 times at random intervals of 50 to 100 cycles,
stopping when num reaches zero.
script beVeryAnnoying
{
var num = 100;
while num {
say("Beep.");
delay randomRange(50, 100);
num--;
}
}
See Also...