Clan UFW

Warcraft 3 USEast Clan for Fate / Another
HomeCalendarFAQSearchMemberlistUsergroupsRegisterLog in

Share | 

 [Tutorial | Triggering Basics #2] How to use the variables

View previous topic View next topic Go down 
Fate Another Developer

Posts : 1673
Join date : 2009-08-01
Age : 28
Location : Laramie

PostSubject: [Tutorial | Triggering Basics #2] How to use the variables   Sat Aug 01, 2009 4:37 pm

This tutorial assumes that you are familiar with the architecture of World Editor (Abbriviated WE) to a degree and you have at least a minor background in writing triggers

L46kok's Tutorial #2 : How to use the variables

Variable is your bread and butter in practically all of the programming languages out there. In a sense, writing triggers on WE is much similar to writing a block of code with a programming language hence, it is imperative that you understand its usage and advantage to write an efficient trigger.

Variable in computer terminology is a data structure that holds information which is changable. This means that once you make a variable, you can store a certain type of information in it, use that information somewhere else and even change that information. For instance, if you need to keep track of how many kills a certain player had, variable is the way to go.

When discussing about variables, we must also talk about its data types, which means what kind of information the variable can store. Here are some examples of data types in Warcraft 3:

  • Integer
  • Real
  • Boolean
  • String
  • Unit
  • Unit-Type
  • Player
  • Item
  • Item-Type
  • Quest
  • etc..

Of the data types I talked about, the first four: Integer, Real(Also known as Floating Point), Boolean and String are what's known as Primitive Data Types since they are the basic ones used for any operations and all the other datatypes are inherently derived from the primitive ones.

Now for some brief explanation about the data types-

Integer : Stores integers (negative or positive numbers without the decimal point). Note : Storing a decimal number in it does NOT round it up (I.E : If you try to store 2.999 into an integer variable, it will only store 2)

Real : Stores real numbers (negative or positive numbers including the decimal point).

Boolean : Stores either TRUE or FALSE case.

String : Stores a character-array (A text)

Unit : Stores a unit with an existing unit-ID (A Particular unit that has spawned on map, for example)

Unit-Type : Stores a unit type. (Unit classes such as Footman, Wisp, Acolyte)

Player : Stores a particular player in play

Item : Stores a unit with an existing item-ID (A particular item that has spawned on map, for example)

Item-Type : Stores an item type.

Quest : Stores a quest

Now that we covered the basics, let's go straight to the example.

Example 1 : You want to make a map where each player is given a certain amount of gold based on how many people are in play.
Let's say that a person gets 50 gold X total amount of people.

First, we have to make a variable and that's simpler than you can imagine.

1.Hit F4 or go to Modules->Trigger Editor

2.Click on the Variables icon or press Ctrl+B

3.Click on the New Variable icon or press Ctrl+N

4.Choose Integer from the Variable Type. Name the variable "TotalGoldAmt" then press OK.

*Note : You'll notice that the integer contains an initial value of 0. In our case, we'll leave it as it is since it is going to be changed

And that's it! You just made yourself an integer variable. Now let's use that to make the trigger written as above. You should have a trigger similar to the below:

For those who are not too familiar with triggers, let me briefly explain line by line

Events - Map Initialization

Events describe the situation where and when this trigger will be performed. In this particular case, this trigger will be run when the game starts.

Actions - Set TotalGoldAmt = 50 X number of players in All players

This is where the variable TotalGoldAmt is being set to the amount of gold which each player will receive.

Actions - Player Group - Pick every player in (All players) and do (Actions)

This is a loop statement where the system picks each and individual player in the game and performs the command under the Loop -Actions. We will cover more about loops later, so don't worry if you don't completely get what it does.

Loop - Actions : Player - Add TotalGoldAmt to (Picked player) current gold

This is where the player receives the amount of gold set in the variable TotalGoldAmt. Since this is a loop statement, each and every player will receive the gold.

Notice how writing the code with variables makes the code look tidier and easier to read!

You might be thinking "But wait, couldn't you optimize the code by doing the arithmetic calculation in the loop action instead of having to write a separate statement for variable?" why yes, you certanily could. But what if you had to change the arithmetic? Suppose you added another line of code shown as below:

If you wanted to change the multiplier from 50 to 60, all you have to do is modify one line of code, which is the line where you set the variable whereas if you didn't utilize the variables, you would have modify the code for each line the variable took in place. See how using variables could make your life so much easier? (Imagine if you had more lines of code dependent on the variable declaration. Brrrr)

Example 2 : You are given a barracks and an archmage to start off with. After 60 seconds have elapsed, you want the archmage and all footmen on the map to die.

This is a good example to demonstrate the difference between the datatypes of unit and unit type. We need a way to differentiate between certain units, considering if they are the same unit type. For instance, if you had 2 archmages and you wanted a particular archmage to be removed from the game, this is where the variable type unit comes in handy whereas if you wanted to get rid of any archmages from the game, you could instead use a unit type.

For the answer to the example, you should have a code similar to the following

This code isn't optimized at all but I'm utilizing variables as much as possible for the sake of explanation. Note that

Set Archmage_Unit = Archmage 0000
Set Footman_UnitType = Footman

has a fundamental difference of where the variable Archmage_Unit is given its Unit ID, specifying which unit it is on the map and the variable Footman_UnitType is not given its unit ID, indicating that it's storing the general unit class of Footman.

Rest is self explanatory - The function calls the trigger to kill the particular archmage on map and goes through a loop to pick every unit on the map matching the type of Footman_UnitType, then kills the picked unit.

I hope from this tutorial you have learned to appriciate the powerful tool of variables and that you can maximize its utiliization by practicing. Please leave and questions or comments so I can further improve this tutorial.
Back to top Go down
View user profile


Posts : 153
Join date : 2009-08-18
Age : 21
Location : Somewhere~

PostSubject: Re: [Tutorial | Triggering Basics #2] How to use the variables   Wed Sep 16, 2009 6:56 pm

So a variable stores stuff that can be used later? I have another question can you stack multiple variables into 1 trigger? Like say I wanted to make a scoreboard that can keep track of not kills but points and when a condition is met use multiple variables to change the score depending on the condition if I use multiple conditions?
Back to top Go down
View user profile


Posts : 673
Join date : 2009-08-01

PostSubject: Re: [Tutorial | Triggering Basics #2] How to use the variables   Thu Sep 17, 2009 2:17 am

You can modify as many variables as you want in a trigger >_>
Back to top Go down
View user profile
Sponsored content

PostSubject: Re: [Tutorial | Triggering Basics #2] How to use the variables   

Back to top Go down
[Tutorial | Triggering Basics #2] How to use the variables
View previous topic View next topic Back to top 
Page 1 of 1
 Similar topics
» Unity 5 Basics Tutorial
» Great Tank and Weathering tutorial
» Power Sword Tutorial (with more to follow)
» Gaming basics and our responsibility
» Instant Mold Tutorial

Permissions in this forum:You cannot reply to topics in this forum
Clan UFW :: Warcraft 3 :: Map Making-
Jump to: