Advanced RMS: Part 2 – Variables

By Matei of Woad Creations
created 9/10/03

Variables are locations in memory where your script can store information. They are very useful in any program because the computer often has to store intermediate results and then use them later. In AoM RMS, there are four types of variables:

  • int – Stores an integer, such as 0, 15, or -2. Can be operated upon by the +, -, /, *, and % operators (the % is modulus, the remainder of division).
  • float – Stores a real number, such as 0.2, 3.1415926, or -5.0. Can be operated upon by the +, -, /, and * operators, and by the built-in sqrt() function.
  • bool – Stores a true/false logical value, either true or false; named after mathematician Georgre Boole. There isn’t much you can do with these, but the ! operator (placed in front of a bool variable) will get its opposite (for example, !true is false).
  • string – Stores text, such as “hello”. You cannot define a really long string constant (such as “aaaaa……” with, say, 50 a’s), but you can get as long a string as you want by using the + operator and adding smaller strings to the end of a big one. You can also add values of variables of any type to a string, as long as the thing before the + sign is a string variable (so for example, if x is a float variable, you can write “hello”+x but not x+”hello”). Note that you can use the escape sequence, \ in a string for a “newline character”; use \\ (double back slash) if you want to print a backslash.

All variables must be declared before they are used. The declaration is of form <variableType> <name> = <initialValue>;. You must always assign an initial value to each variable, even if it’s 0 or a blank string (“”). The following example shows you how to define one variable of each type:

int numTownCenters = 4; 
float playerArea = 0.13; 
bool useWater = true; 
string baseTerrainName = "sandA";

Variables can be either global or local. Global variables are defined outside any function, at the very top of your script, and can be used by any function. They are usually things like environment information, such as the name of your base terrain type, that the entire script uses. Local variables are defined at the top if a specific function and can only be used by it. While you can put them lower in the function, it’s easier to have them at the top. They are used to store temporary results that only that function needs to “worry” about. Here is an example showing two functions, someFunction() and someOtherFunction(), and two variables, globalVar and localVar, as well as several lines trying to use them:

int globalVar = 0;

// ...

void function1(void) {
    string localVar = "";
    localVar = "abc";   // ok, valid statement
    globalVar = 2;      // ok
    // ...
}

void function2(void) {
    localVar = "abc";   // bad! localVar only exists in function1()
    globalVar = 2;      // ok
    // ...
}

Finally, you might want some variables to be constants. Constant variables cannot be changed at all from their initial value. They are declared by writing const <variableType> name = <initialValue>. They are useful if you don’t want to change the value of something by mistake. In general, however, a non-constant variable can do the same things as a constant one.

Back to the RMS Section