A variable is a memory cell which holds a value. You must always declare a variable before you use it. You can assign a variable a value either at the declaration or later in the script.
WME script is a loosely typed language. It means you do not have to specify a data type of a variable while declaring it. The variable’s data type depends on the value you store in the variable.
Internally, the WME script uses the following data types:
Boolean type variables can only contain a logical value: true or false.
A string value is a chain of zero or more characters (letters, digits and punctuation marks) strung together. String literals can be included in your scripts by enclosing them in matching pairs of double quotation marks.
Integer values can be positive whole numbers, negative whole numbers, and 0.
Floating-point values can be whole numbers with a decimal portion.
The null data type has only one value in WME script: null. A variable that contains null contains "no value" or "no object." You can erase the contents of a variable by assigning it the null value.
There are three types of variables in the WME script: local, global and constants.
The local variables are declared using the var keyword and they are only visible in the block of code in which they are declared.
var MyVariable; var MyInitializedVariable = 10;
The global variables are declared using the global keyword and they are visible in all scripts. The values of the global variables are shared in between all scripts. Therefore the global variables are used for storing the persistent game state values.
global MyGlobal; global MyInitializedGlobal = 10;
The constant variables are declared using the const keyword and they are visible in all scripts. Constant variables behave exactly the same as global variables with one exception: their value is not cleared after calling the Game.Reset() method. They are suitable for predefined constants, rarely changing their value.
const DI_UP = 0;
There are two special variables, Game and this. Those two variables are always declared and accessible by your scripts.
The Game variable contains a reference to the main game object. You can call its methods and set or query its attributes.
The this variable contains a reference to the object which owns the currently running script (the scripts in WME are always assigned to an object).
All the variables are actually objects, it means you can either assign a simple value to the variable, or you can set values of its properties. You can name the properties whatever you want. All the following examples are valid in WME script:
var MyVariable; MyVariable.SomeProperty = 10; MyVariable.AnotherProperty = "a string value"; MyVariable.LogProperty = true; MyVariable = 20; MyVariable = 30; MyVariable["YetAnotherProperty"] = 40;
Note the MyVariable notation. This way you can easily use the arrays (know from other languages) without declaring them.
Remember that instead of tens of global variables it’s better to take advantage of the object nature of the variables and to group coherent variables into one “object” variable. For example: You will probably end up having many state variables for individual scenes of your game. Such states as “is the window open in scene XXX?”, “did the player visit scene YYY yet?”. It’s recommended that you define one global variable per scene and store all scene related states in this variable.
global StateMyScene; StateMyScene.VisitedByPlayer = false; StateMyScene.IsWindowOpen = true;
These conventions are demonstrated in the demo game and they are supported by the standard templates, which are part of the WME Development Kit distribution.
Besides the normal variables, you will often use special variables containing reference to game objects (such as scenes, actors, entities, windows etc.). Setting properties of these object variables will have an impact on the game’s appearance or behavior. For example setting an “X” attribute of an actor variable will make the actor to change his position on screen. These special variables also allow you to call their methods. For example calling actor’s “Talk” method will make the actor talk.
var actor = Game.LoadActor("actors\molly\molly.actor"); actor.SetPosition(100, 200); actor.Talk("Blah blah."); actor.Active = false;
The above example does the following:
1) it declares a local variable “actor”
2) it invokes a “LoadActor” method of the Game object and stores the resulting actor reference in the “actor” variable
3) it invokes a “SetPosition” method of the newly created actor to change his position on screen
4) it invokes a “Talk” method of the actor to make him say “Blah blah”
5) it sets actor’s “Active” property to hide the actor
Note: A complete list of game objects, their methods and attributes can be found in the reference section of this chapter.