HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Varible Arrays,what do they do?

08-08-2004, 12:02 PM#1
Arkidas
The title says all!
08-08-2004, 01:53 PM#2
Anitarf
Variable arrays are a great upgrade of standard variables. They allow you to sistemise and organize variables and automatise their manipulation.

Simply put, a variable array is a single variable, which can hold multiple values. Normaly, you can store a single unit into a unit variable. Into an array, you can store as many as you want (well, in warcraft, the limit is somewhere around 8000). How can you do that? Simple. an array variable has, besides it's name, also an index. This index is the difference between different values a variable can hold. You can store different units under UnitVariable[1] and UnitVariable[2]...

The biggest advantage of this is, that the index is an integer; this allows for some great, no, grand code optimisations! Because for the index, you can put in any number, it doesn't need to be preset, it can be another dynamic variable. Lets look at a quick example of how to use this in combination with loops:

Code:
Without arrays:
  set UnitsGroup1 = (Units owned by (Player 1))
  set UnitsGroup2 = (Units owned by (Player 2))
  set UnitsGroup3 = (Units owned by (Player 3))
  ....
  set UnitsGroup12 = (Units owned by (Player 12))

With an array:
  For each integer A from 1 to 12, do  set UnitsGroup[(Integer A)] = (Units owned by (Player (Integer A)))

12 lines of code compressed into one! Neat, huh? That nearly always happens, when you do something with an array.

Code:
Without arrays, you need as many such triggers as there are players:
  Events:
    A unit dies
  Conditions:
    (owner of (killing unit)) equal to (Player 1)
  Actions:
    Set KillCountVariableForPlayer1 = KillCountVariableForPlayer1 + 1

With an array, one trigger gets the job done!
  Events:
    A unit dies
  Conditions:
  Actions:
    Set KillCountVariable[(Player number of(owner of (killing unit)))] = KillCountVariable[(Player number of(owner of (killing unit)))] + 1

The uses of arrays are so many, it's hard to give you a broad wiev of all that can be done. This is merely a glimpse of what arrays are capable of. In these examples, we only had a single array, just for the players. What if you had two dynamic things interacting? For example, what if the effect was dependant on which player interacted with which? That would be tons of if-then-else statements! Horrible! If something didn't work and you would post your code here, to get some help, triggerers would avert their eyes in terror and you wouldn't get anywhere (not to mention that even if you did find the problem, you would have to redo the entire triggers!).

Yea, it is true, arrays are a grand gift to the trigger people indeed!
08-08-2004, 01:57 PM#3
johnfn
A variable array is like a collection of variables. Each number can be accessed by its index. So for example to get the second number in an array you would type array[2], to get the sixth you would type in array[6]. Using For Loops with array's is very helpful because you can do array[integer a] to loop through each array element (means each number in the array)

One good use for arrays is if you would want to destroy a whole lot of special effects. For example:

for each integer a from 1 to 10
create special effect blah
set array[integer a] to (Last Created Special Effect)

When you want to destroy the special effects, you would do

for each integer a from 1 to 10
destroy special effect (array[integer a])

Whew, that was pretty long, hope you understood everything, as arrays are very important.

Edit: Lol. Anitarf, you posted as I was writing my post. Somehow I knew this would happen. Oh well, now he gets two descriptions! ;)
08-08-2004, 02:36 PM#4
Anitarf
And you thought your post was long. :) Well, I was afraid someone would post before me too, if it makes you feel any better :) There is nothing wrong with more descriptions (unless they contradict each other, that can get confusing), so, in order to not get too much off topic, here's another example:

Code:
Let's say any player can choose a unit type, and that unit will be his elite
unit (it will have critical strike, for example). First, let's envision a darker
world where arrays don't exist, and trigerer's souls are eternaly tormented
as they copy and paste paragraphs of code (If you really want a vision of
hell, imagine also that there is no GUI and all the code is in custom text):

Events:
  A unit owned by (Player 1) finishes training a unit
Conditions:
Actions:
  If UnitTypeVariableForPlayer1 = (unit type of (trained unit)) then (unit - add (critical strike) to (trained unit))

Repeat the trigger for as many players as you have.

But now, an Angel of light makes it's radiant appearance in this dim world,
and delivers a great gift to it's tormented people, and lo! the briliant gift in
the angels hands is none other than the Array, unsurpassed in purity and
fairness. And all that was once dark and shadowy is now illuminated by
this supreme light, and all the triggerers fall to their knees, awed and
humbled in the presence of such grandeour, and then with trembling
hands they receive their gift, and immediately use it to create a single
trigger, one to surpass all their previous spirit's tormentors:

Events:
  GENERIC UNIT EVENT! : A unit finishes training a unit
Conditions:
Actions:
  If UnitTypeVariableForPlayer[(owner of (trained unit))] = (unit type of (trained unit)) then (unit - add (critical strike) to (trained unit))
08-08-2004, 03:55 PM#5
Deathperception
To condense the explanations even further think of a variable without an array as a file cabinet without any folders in it and you can only put 1 piece of paper or piece of information in the drawer without being unorganized.

Think of an array now you can put folders in this file cabinet drawer and you can manage multiple pieces of paper now rather then just storing one.

Basically it allows you to store multiple pieces of information under one variable name or in one cabinet drawer. For example I want to make a banking system to store player 1 and 2's gold I make an integer (a variable that stores numbers only) called BankAccount[1] and BankAccount[2] .

Now player 1's money is stored in the integer BankAccount [1] and player 2's is stored in BankAccount[2].

P1's gold is in BankAccount array 1 and 2's is in BankAccount array 2.

If you didn't use an array to store the gold you would have to make 2 separate integers which is more work and eventually clutters the variable menu unnecessarily.

This should put arrays into perspective now if you've already read the excellent explanations above.

Hope This Helps :god_help_us:
08-08-2004, 05:41 PM#6
Arkidas
ahh,so you mean if I set variables 1st array as lets say footman unit and its 2th array as ghoul then I can do,create unit variable index 1 and then I get footman?
08-08-2004, 06:00 PM#7
Anitarf
Yes, exactly. This is useful for spawning creeps in TDs or Hero sieges, for example: Without arrays, you would need a seperate trigger for each wave of monsters (because each wave would use a different unit-type to spawn). With arrays, you can store all the wave monster types in an array first, and then use only one spawning trigger, which would, instead of using a fixed unit-type, use the unit-type array instead, each time with a different index.

In your example, you would set an integer variable to 1, and your spawning trigger, which would be using that variable as the index in the array, would start spawning footmen. Then, when you changed the integer variable to 2, the same spawning trigger would start spawning ghouls.