Difference between revisions of "WML for Complete Beginners: Chapter 7"
(remove "clearing variables". it's not needed for a "complete beginner")
|Line 42:||Line 42:|
Revision as of 02:52, 18 April 2017
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"
You may have a counter variable to count the number of times an event happens. This variable will start at zero and go up by one every time the event happens. So how would you perform that basic math? It would look like this:
[set_variable] name=counter add=1 [/set_variable]
Next Chapter: WML for Complete Beginners: Chapter 8
Previous Chapter: WML for Complete Beginners: Chapter 6
Return to Main Index: WML for Complete Beginners