|
Variables
By SD_Ryoko
What are Variables?
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.
Creating Variables
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.
Name
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.
Type
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.
Value
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.
Saving our image with the extrator tool.
Setting Variable Values
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.
Variable Arrays
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[0] = "Apples"
Set Fruits[1] = "Oranges"
Set Fruits[2] = "Pears"
Set Fruits[3] = "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[2])
|
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)]
|
Using Boolean Logic
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
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:
Custom script: call ReviveHeroLoc(Revive_Hero, GetRectCenter(GetPlayableMapRect()), true)
|
Remember, local variables will never show up in the standard interface.
This is why you have to use JASS to manipulate them.
The best way to do this is to create a rough draft of just the special actions using dummy variables.
Then convert it to custom text, change it, and insert it into your trigger.
Appendix of Variable Types
Below is a list of the variable types Warcraft III can use.
I have placed the most common variable types at the top of the list.
The rest are in alphabetical order.
Integer | Stores a whole number, and does not accept deciaml points. |
Real | Stores a floating point number, or a number with decimal points. |
String | Stores a group of characters, or line of text. |
Ability | Stores a type of ability, like Windwalk or Bloodlust. |
Boolean | Stores a true or false statement. |
Buff | Stores an type of buff, like slow or poision. |
Camera Object | Stores a pre-placed camera object on your map. |
Destructable | Stores a particular destructable, either pre-placed or created. |
Destructable Type | Stores a type of destructable. |
Defeat Condition | Can store the last defeat condition that you created. |
Dialog | Can store the last dialog box that you created. |
Dialog Button | Can store the last dialog button that you created. |
Floating Text | Can store the last floating text that you created. |
Game Cache | Can store the last came cache that you created. |
Game Speed | Stores a game speed value, either the current speed or another value. |
Item | Stores a particular item that already exists in the map. |
Item Class | Stores the class of an item. |
Item Type | Stores the type of an item. |
Leaderboard | Can store the last leaderboard you created. |
Multiboard | Can store the last multiboard you created. |
Order | Stores a unit order, like Attack or Hide. |
Player | References a particular player. |
Player Color | Stores a player color value, like red. |
Player Group | Stores a group of players to be used again. |
Point | Stores a X,Y coordinate on the map. |
Quest | Stores the last quest you created. |
Quest Requirement | Stores the last quest requirement you created. |
Race | Stores a race, like Human or Night Elf. |
Region | References a particular region on the map. |
Sound | References a sound that you have used or imported, in the sound manager. |
Special Effect | Stores the last special effect you created. |
Tech Type | Stores a type of research or upgrade. |
Terrain Deformation | Stores the last terrain deformation you created. |
Timer | Used for creating timers. Every timer has a timer variable. |
Timer Window | Stores the last timer window you created. |
Trigger | References a particular trigger in your map. |
Unit | Stores a particular, single unit on your map. |
Unit Group | Stores a group of units. |
Unit Type | Stores a particular type of unit, like Footman or Sorceress. |
Visibility Modifier | Stores the last visibiliy modifier you created. |
Weather Effect | Stores the last weather effect you created. |
|
|