WML for Complete Beginners: Chapter 7
Chapter 7: Introduction to Variables
Now it's time to learn about “variables”. Variables contain things. They're a lot like boxes that you'd use when moving to a new home. You put things in the boxes, and then you label them so that you can find the right things when unpacking later.
Like attributes, every variable has a name and a value. The name of the variable is like the label on the box, and the variable's value is the thing that the variable (or box) contains.
For instance, you might put all of your dishes in a box and label it “dishes”. Similarly you might create a variable named “my_name” and put your name inside of it. Then if you wanted to find your name later, you could look in the variable called “my_name”.
To create a variable, the following syntax is used:
[set_variable] name=variable_name value=variable_value [/set_variable]
This creates a variable and assigns a name and value to it.
If you have ever taken basic algebra, you should know what substitution is. If you haven't, don't worry, I'll explain it.
Substitution basically allows you to substitute one thing for another thing, as long as both of those things are declared equal. For instance, let's say that x=7. Since they have been declared equal, if you were told to solve this problem:
what would you do? Well, since x is equal to seven, you can just replace x with 7, which gives you:
From there, it's easy to solve this problem.
What you just did was called substitution. You substituted 7 for x in the problem.
Returning the idea of the dishes stored in the box labeled "dishes". If your mother pointed at the box containing the dishes and said "get out the contents of the box labeled dishes", you understand that she wants you to get out the dishes from the box labeled "dishes", so you'd get out the dishes and give them to her. Now suppose you had a WML variable named "dishes_box" and the value of that variable was "dishes". If you tell the game that you want it to get the value of the variable named "dishes_box", it would give you the value "dishes". So what exactly do we need to do in order to tell the game to get the value of the "dishes_box" variable? This is where substitution comes in.
Suppose you wanted to have the narrator give a message telling the player the value of the variable "dishes_box". Here's how you would tell the game to do that in WML:
[message] speaker=narrator message= _ "The value of the dishes_box variable is: $dishes_box" [/message]
By preceding the name of a variable with a dollar sign "$" you are telling the game that you want to substitute the value of that variable. So in-game the narrator would say, "The value of the dishes_box variable is: dishes"
Suppose you decided change the value of the "dishes_box" variable to "empty". Now the narrator will say in-game, "The value of the dishes_box variable is: empty"
Whenever you know for sure that you won't need a variable any more, it's considered good programming practice to delete that variable, or clear it. This effectively tells the game that the variable in question isn't needed any more, so the game deletes that variable. If you don't clear variables once you no longer need them, they can accumulate over time and slow down the performance of both the game and the player's computer.
In order to clear a variable, use the following syntax:
#whatever the syntax is goes here, NOT THE MACRO, we want the user to be able to clear variables without relying on the macro (maybe mention the macro as a side note anyway?)
Next Chapter: WML for Complete Beginners: Chapter 8
Previous Chapter: WML for Complete Beginners: Chapter 6