Translations of this page:

This is an old revision of the document!
—-

2. Survivor's guide to WME scripting

So we’ve managed to get over the first couple of WME tests, we’ve mastered the developers environment and now you’re surely eager to know how to advance! As you’ve probably guessed, the core of all WME operations lies in the scripts. But what IS the script?

Let’s start from the very beginning. Every software is in its core “action / reaction” based. This means that something happens and the program somehow responds to it. The action can be anything ranging from player’s mouse clicks and ending with, for example, time which passes. So back to my question – what is a script? It’s a program, which defines what actions to take care of and how to respond to them.

Now your typical WME game will be constructed from many scripts. Let’s say that you click with your mouse on a door. You’d need a script which would make your game avatar say “The door is locked.” I hope you get my point here. As we dive deeper into this chapter, you see a lot of WME magic, but let’s start right at the beginning.

The core of the WME programming lies in commands. Command tells WME to perform an action. We can have plenty of different commands ranging from simple mathematic operation (addition , substraction , etc) to complex commands like for example

Game.ChangeScene("some_scene.scene");

which makes WME to change actual scene. But let’s look at the very basics of scripting. We’d start with something which is called variable. What is a variable? It’s a container in which you can store information. In WME (unlike for example in C++) you can store any type of information into your variable (string, object, number).

Let’s look at an example:

var myvar = "Hello World";

The keyword var declares a local variable called myvar which has been filled by the text Hello World. Later on we can reference the text in the variable for example with:

Game.Msg(myvar);

This line would print Hello World on the game screen.

Syntax rule no.1
As you can see, every command ends up with a semicolon ;
Also WME is case sensitive! hello is not equal to Hello. Keep this in mind!

Very important rule is that in each script we declare variable only once. Then we reference it without keyword var.

Now let’s have a look at some basic work with variables:

var myvar = "Hello World"; 
Game.Msg(myvar); 
 
myvar = 1;		// we store number 1 in our variable 
myvar = myvar + 1;  // we add 1 to the stored number 
Game.Msg(myvar);	// we print result (number 2) 
 
myvar = myvar * 2; // multiple by 2 
myvar = myvar / 4; // Divide by 4 
 
myvar = "The result is " + myvar + "!"; 
Game.Msg(myvar); //prints out The result is 1!

Syntax rule no.2

If we want to comment out something, we use // for single line comments  
or /* */ for block comments Commented blocks or commands won’t be executed. 
 
Example: 
 
var a = 1; // we store 1 into variable a 
 
/*  This block  
   var a=1; 
   a = a +1; 
   a = 0; 
   will never get executed */

TIP: Always name your variable in an intelligent way so you find out later what is being stored inside. It’s of course funny to name a variable littleRedDwarf but if you look into your code 5 months later, you’ll be desperately trying to figure out why is your funny variable in there. Another common praxis is using lowerCamelCase or UpperCamelCase which is just a way how you capitalize letters in your variable name. So for example variable which should hold number of ingredients added to a kettle can be called for example kettleIngredients.

As you read, we’ve been speaking about something called local variable. The local means, that the variable is valid only in the script it's declared in. So imagine that we have two scripts which gets executed as test1.script followed by test2.script:

file test1.script

var a = 10; 
Game.Msg(a); //prints 10

file test2.script

var a = a + 1;  
Game.Msg(a); //prints 1, because the variable is valid for the current script only so default value is 0

But what if we want to share some value from one script to another? For that we have another kind of variable called global variable. This variable is declared using keyword global and is valid for the whole game!

Syntax rule no.3
In every script in which we want to use a global variable, we have to declare this variable through the keyword global.

And now for some arithmetic time:

  var a = 1 + 3;  // a = 4 
  a = 3 - 3;  // a = 0 
  a = 3 * 3; // a = 9 (* multiplies by) 
  a = 3 / 3; // a = 1 (/ divides by)

Let’s look at our previous example again assuming that first test1.script got executed and later on test2.script:

file test1.script

  global a = 10; 
  Game.Msg(a); //prints 10

file test1.script

   global a = a + 1;  
   Game.Msg(a); //prints 11, because the variable is valid for the whole game.

As the Real life example let's say that you have a button in some scene and if you press this button, you want some door in completely different scene to open. You simply declare a global variable and set it to true if the button was pressed. In the scene, where you find the door, you simply test this global variable and if it's set to true, you open the door. Just to clarify the term "true", there are two special values which you can set to variable - true and false. More about those in the conditions.

As we see, we’ve successfully uncovered variables, but wait - there’s much more to them. We’re just at the beginning.

2.1 Magical Arrays

Before we start with talking about arrays, I’ll give you an example of the real game problem. Imagine that you have a door protected by a number code pad. You have to punch in 4 correct numbers and then the door opens. You have a couple of different ways how to do it. Let’s start with the worst one:

terrible choice: From the previous chapter you’d probably create 4 different variables (for example code1, code2, code3, code4) and set the punched numbers into them. Then you’d again check them one by one for a match.

overly complicated choice: you have one variable and by using multiplication and mathematics, you construct one number which will you then compare with the resulting number.

smart choice: you use an array.

You can imagine an array as a set of variables. You reference those variables through indexing the array variable. This index is a number which starts with 0. I bet the example is worth thousands of words. We’ll define a variable myArray and set 3 array fields.

var myArray; 
 
myArray[0] = "Hello World";  
myArray[1] = "I’m your first array"; 
myArray[2] = "WHEEEEE!";

And of course we can do:

Game.Msg(myArray[1]);

or even

var a = 1; 
Game.Msg(myArray[a]);

which prints out the same text because variable a contains number 1.

Syntax rule no.4
We tell WME that we want to access array by using item index enclosed in square brackets.

If we got back to our real life example with a door code, we can easily set the whole code in one variable.

var doorCode; 
 
doorCode[0] = 5;  
doorCode[1] = 2; 
doorCode[2] = 3; 
doorCode[3] = 7;

Okay. Now the inquisitive coders already wonder why we don’t use something like:

var doorCode = “5”; 
 
doorCode = doorCode + “2”; 
doorCode = doorCode + “3”; 
doorCode = doorCode + “7”; // the result being 5237 in one variable.

Well, we could, but then – what if you have more complex door code and wanted to change for example 3rd number when all 4 were set? It would make things much more complicated and also - we’re now demonstrating arrays so get over it.

As you will get used to scripting, you’ll find a lot of use for arrays. The critical strength of arrays lies in the index. You can access the index value programmatically and that’s why we’ll look now in another real life example.

You surely saw how better designed games use more different responses for one action so it won’t be boring. Let’s see how it can be done and let’s use an array for it. We’ll also incorporate a new command called Random.

Random is a function which returns a random number in specified range. We’ll speak about functions and how they work later, so for now take it as something which works and don’t wonder why.

var answerArray; 
answerArray[0] = "It’s stuck!"; 
answerArray[1] = "I can’t move it!"; 
answerArray[2] = "No chance. It won’t budge!"; 
 
var reply = Random(0,2); // we set the variable reply with a random number between 0 and 2 
 
Game.Msg(answerArray[reply]); //So we can easily print a random response from our set

I think you’re getting a bit tired of this variable talk so we’ll focus now on different things and return to the big strength called objects a tad bit later on.

2.2 Conditions

Usually we need to know what’s going on. If we spoke before about the example with a door and a button, we spoke about testing if the button was pressed. But how do we actually do this test? We have a condition command called if which does the work for us. Again let’s start with some code:

var a = 1; 
 
if (a == 1) Game.Msg(“A equals to one”); // this line can be read as: if a equals to 1 execute the following command

This completely unusable example gives you the first impression what’s going on with conditions. As testing is syntactically more complicated we’ll now focus on the basics of our tests.

As you can see, the condition is itself enclosed in parentheses.

if (insert some condition here) do something;

Testing itself is done by the following logic:

  1. If we want to test that something equals to something else we use
    ==

    Example:

    if (a == 1)
  2. If we want to test that something isn't equal to something else we use
    !=

    Example:

    if (a != 1)
  3. If we want to test that something is greater than something else we use
    >

    Example:

    if (a > 1)
  4. If we want to test that something is lesser than something else we use
    <

    Example:

    if (a < 1)
  5. If we want to test that something is lesser or equal to something else we use
    <=

    Example:

    if (a <= 1)
  6. If we want to test that something is greater or equal to something else we use
    >=

    Example:

    if (a >= 1)

For variables (they're called boolean by the way) which are true or false we can use a shortcut:

var a = true; 
if (a) Game.Msg("It's true!"); 
if (!a) Game.Msg("It's false!");


But the fun doesn’t certainly end here! We can make much more complicated tests but to understand that, we have to look at another thing.
Let’s get back to the beginning:

Let's take our "if (a == 1)" and if we set variable a to 1 beforehand, we can read the command also as if 1 equals to 1. This statement is true.

If we set on the other hand variable a to 2, we can read the condition as if 2 equals to 1. This statement is false.

This trivial logic is the very elementary of the computing in general. Computers are filled to the brim with true/false logic and we have to get a grip of those.

WME reads the condition and makes an evaluation. If the statement was true, it executes the command, if the statement was false, WME doesn’t and goes on. Let’s look at this:

If we return about our real life example with button opening our door, we could use one script handling the event when the button was clicked like this:

global buttonClicked = true;

and then in the other script checking the door like this:

global buttonClicked; 
if (buttonClicked) do something which opens the door;

So now when we have the basics of conditions, we can look at how to test more than one thing at the same time.

  1. We use operator && if we want both conditions true. if (a > 1 && a < 10) means that a must be bigger than 1 and smaller than 10
  2. We use operator || if we want at least one of the conditions to be true. if (a == "John" || b == 1) means that a must contain the word John or variable b must be equal to 1.

If we want group some conditions we uses again parentheses:

if ( (a>1 && a<10) || (b =="John") ) is one of the more complicated tests. It will execute the following command if the variable a is bigger than 1 AND smaller than 10 OR if variable b contains the word John.

Syntax rule no.4
If you want to execute multiple commands in the condition, you use curly brackets to enclose the commands which will then take place.
Example:

if (a == 2) { 
  Game.Msg("Hi!"); 
  Game.Msg("Hi again!"); 
}

Both commands will be executed if variable a equals to 2


 
wmebook/ch2.1196934623.txt.gz · Last modified: 2007/12/06 10:50 by metamorphium
Recent changes RSS feed Creative Commons License Driven by DokuWiki