Variables are locations in memory that can hold values.
Variables play an important role in scripting because they enable programmers to write flexible programs.
You can store almost any kind of data inside a variable.
Variables allow you to keep track of a certain unit, a winning player, a type of unit or even text strings.
Storing information in a variable allows you to use it later.
For example, you could store the number of units a player has killed in an integer variable.
You could also store the player that has killed the most units during the game using a player variable.
In World Editor, variables are typically created inside the Variable Editor.
If your using custom scripts, you may also create certain types of variables within a trigger.
Inside the Trigger Editor, you can click on Edit, Variables and load the variable editor.
Here you can add any type of variable you will need for your map.
All variables have a type, a name, and a value.
Every variable gets a unique name.
The name means nothing.
It is only a reference for you while you create your map.
You can almost call variables whatever you like, so as long as you remember what they are.
Use variable names that mean something. Single-letter variables are just hunky-dory. But index is better than i, count is better than c, and name is better than n. Short, descriptive variable names are best.
You should not begin a variable name with a number. They can contain numbers, but you begin it with a letter.
You may use underscores in a variable name, but not spaces. Using underscores is fine, though it's not recommended to begin a variable name with an underline.
Don't give names that are too similar to your variables.
Avoid naming your variables the same as keywords or functions. Don't name your integer variable int, for example, or your string variable string. This can also cause confusion during map making.
The type of a variable determines what kind of data it can store.
There are many different data types used in Warcraft III.
When creating a variable, you will need to determine what kind of data type to use.
For example, an integer variable can only store a number, and a unit variable can only reference a single exsiting unit.
Later, we will discuss the different types of variables and how they are used.
By default, the value of a new variable is 0, or none.
You can later change this during your game using trigger codes and custom scripts.
After creating a variable, you can always go back to the variable editor, and change the properties of the variable.
The value of a variable can be set any time during the game.
You can set important variables at the start of your map.
Later you can set lesser variables as they are needed, or when an event happens.
Variables are flexible.
The value of a variable can always be changed.
You can set a variable using the 'Set Variable' trigger.
An array is a variable that can store multiple values.
Any variable you create can be an array.
You can specify this inside the variable editor by checking the Array box.
Each of these values has an index, or number in the array, with the first index being 0.
The index is used to call up variables in the list.
It is a way to access the indivudal values stored inside the array.
For example, I could have a variable called Fruits, which is an array of string variables.
Inside my map initialization, I could set the different values of Fruits to what I will need later in my map.
Set Fruits = "Apples"
Set Fruits = "Oranges"
Set Fruits = "Pears"
Set Fruits = "Bananas"
Later in the game, If I wanted to tell a player to collect the pears, I could use Fruits to give them instructions.
Game - Display to (All players) the text: "The King wants you to collect an " + Fruits)
I could also use Fruits in a loop, and display the types of fruits to players in the game.
For each (Integer A) from 0 to 3, do (Actions)
Loop - Actions
Game - Display to (All players) the text: Fruits[(Integer A)]
Boolean logic is used to find out the overall answer to a number of conditional statements.
A Boolean value can be either true or false - nothing else.
An example statement would be 'I am ten feet tall or the tree has fallen'.
The statement will either be true or false.
If it was true, I could collect the pears for the king.
Local variables are different from global variables.
Local variables only exist inside the function they were declared in.
For example, if you had a trigger that fired every time a unit was killed, and you were using a standard, global variable, the triggeres could possibly by overwriting that variable as new ones fired.
Things wouldn't work correctly because the value of your variable was always changing.
Or, if you had a trigger that revived Heroes, and you stored the hero in a global unit variable.
If some other hero died before you revived the first, the hero unit would be overwritten with the new.
The original hero unit value would be lost.
To create local variables, you need to use JASS.
Insert a new action in your script, and pick 'Custom Script'.
Add the variable into your trigger as shown:
Custom script: local unit Revive_Hero
To set this new variable, you would also have to use a custom script.
Insert another line into your action list, using 'Custim Script':
Custom script: set Revive_Hero = GetTriggerUnit()
Then later, you would again need JASS to revive that hero: