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.

IntegerStores a whole number, and does not accept deciaml points.
RealStores a floating point number, or a number with decimal points.
StringStores a group of characters, or line of text.
AbilityStores a type of ability, like Windwalk or Bloodlust.
BooleanStores a true or false statement.
BuffStores an type of buff, like slow or poision.
Camera ObjectStores a pre-placed camera object on your map.
DestructableStores a particular destructable, either pre-placed or created.
Destructable TypeStores a type of destructable.
Defeat ConditionCan store the last defeat condition that you created.
DialogCan store the last dialog box that you created.
Dialog ButtonCan store the last dialog button that you created.
Floating TextCan store the last floating text that you created.
Game CacheCan store the last came cache that you created.
Game SpeedStores a game speed value, either the current speed or another value.
ItemStores a particular item that already exists in the map.
Item ClassStores the class of an item.
Item TypeStores the type of an item.
LeaderboardCan store the last leaderboard you created.
MultiboardCan store the last multiboard you created.
OrderStores a unit order, like Attack or Hide.
PlayerReferences a particular player.
Player ColorStores a player color value, like red.
Player GroupStores a group of players to be used again.
PointStores a X,Y coordinate on the map.
QuestStores the last quest you created.
Quest RequirementStores the last quest requirement you created.
RaceStores a race, like Human or Night Elf.
RegionReferences a particular region on the map.
SoundReferences a sound that you have used or imported, in the sound manager.
Special EffectStores the last special effect you created.
Tech TypeStores a type of research or upgrade.
Terrain DeformationStores the last terrain deformation you created.
TimerUsed for creating timers. Every timer has a timer variable.
Timer WindowStores the last timer window you created.
TriggerReferences a particular trigger in your map.
UnitStores a particular, single unit on your map.
Unit GroupStores a group of units.
Unit TypeStores a particular type of unit, like Footman or Sorceress.
Visibility ModifierStores the last visibiliy modifier you created.
Weather EffectStores the last weather effect you created.
 
 
Blizzard Entertainment, Inc.
Silkroad Online Forums
Team Griffonrawl Trains Muay Thai and MMA fighters in Ohio.
Apex Steel Pipe - Buys and sells Steel Pipe.