• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Array Tutorial

Level 6
Joined
Feb 15, 2005
Messages
183
Array tutorial

What exactly is a Variable Array?
A variable array is a when a variable has multiple layers. Each layer stores data seperatly from eachother and changing the data for one level of an array does not influence other levels of the array.​

What is the point of an array?
Arrays are there to make the handeling and organizing of data easier so as to allow you to make more complex systems and spells easily that are more efficent and less closed (allows for greater variation of data). When making a complex map you can be insured you will use atleast a few arrays somewhere, as many do.​

All this sounds good but how can I make a basic array?
You make basic global arrays by enhancing a global variable into an array. To do this simply go to variable manager and choose the variable you want to make/enhance. Then there as an available "tick" option labeled "Array" which you must activate. Finaly choose the size of the array and click ok to finish the enhancing of the global variable.​

In global arrays, what does the "size" mean?
The size is how many layers the array has. The total number of data items able to be stored in a global array is "size+1". This is due to layer 0 counting as a valid storage location. If you want to store 1 data item per player for 12 players in a single global variable, then an array size of 11 is needed.​

Great, now I have an array, but how exactly do I use it?
Well, you use it exactly like a normal variable, but it requires a level integer (whole number) inorder to be used. This is located after the variable name and before the value it is to be set to in square brackets. You can preform any integer culculation to get the level to store something as / retreive from. Common ones are setting/retreiving data by getting the player slot number or storing items/text for a save/load system in a logical system.​

Can I randomize things, like hero spawns, units spawned or even items using arrays?
Yes, they are the easiest way and most efficent way to randomize something. To do this you need to develop a logical system and formula that uses the random integer action/function to output a integer that can be used to refer to a layer to retrieve data for another action/function to use. Common uses of this are in item drop tabels in RPGs (Role Playing Games) or in random hero modes in games like footman frenzy. More advanced uses are in games like Dugeon of Doom where they used random array systems to make rooms spawn seemingly random yet level advancing creeps.​

Can I make an array with an infinate size, for some really advanced system?
WC3 game engine does not support infinatly large arrays. You can have big arrays with well over 1000 layers but if it was infinatly big, an infinat ammount of RAM (Random Axcess Memory) would be required which is impossiable. For a more efficent storage system to store large ammounts of data with variable layers, try using a Game Chache based system.​

I already know about global arrays, what about local arrays or do they even exist?

CONTENT BELLOW IS NOT REWRITEN AND THUS MIGHTNOT BE HELPFULL.

Loops
Now, Lets pretend we have a Footman, that we want to move to a region, and then to the next region, and then to the next region, etcetera. You could make an action look something like this:

  • Order Footman to move to center of WayPointRegion[Integer A]
Now you may be asking, what the heck is Integer A? If you look under actions you will notice a function called “For each Integer A do multiple actions”. This is essentially a “loop” function. What 'Integer A" signifies, is the number of times the function has looped. Lets see what a complete Trigger with this loop would look like:

  • For Each Integer from 1 to 4 do multiple actions:
    • Loop Actions -
      • Order Footman to move to center of WayPointRegion[Integer A]
      • Wait 5.00 seconds
Lets look at this example from top to bottom. The first line tells us how many times this function will loop. It says it will loop from 1 to 4, so that is essentially 4 times that the loop actions will happen. Now, the next action is a unit action. We are telling the footman to move to WayPointRegion[Integer A]. So from we know about "Integer A" we know that the first time the loop goes through, Integer A = 1. So it will grab the 1st variable in the WayPointRegion Array, which we set as “Region01” earlier. After the order is given, it will wait for 5 seconds, and then continue the loop. On that second loop, Integer A = 2. So then the index will be 2 and the order will be in the center of "Region02." It will continue until it gets to the 4th loop, and then the loop will stop.

That is all for now, this is only a very small portion of what you can do with Arrays, these are only a few common ones that you may find useful. Experiment and see how else you can use Arrays, I am sure you will find it a very useful tool.

Multi-Instance
Many probably consider the use of array's in order to make a spell able to be cast by multiple units at once to be sloppy and inefficient. This may be true, but before you learn how to use JASS and custom scripts, this is the only means you have available to you. The most common way to use Array's to multiinstance is to use the Player Index value. (note: this only allows each player ONE instance of the spell, but it allows the spell to be cast by multiple players at once)

Let's say you make a unit array with 12 variables called "CasterUnit" and another called "TargetedUnit" (one variable for each player). And you have a Fire Ball spell that you want to be cast by each player. You would make a trigger that looks something like this.

  • Events
    • Unit finishes casting a spell
  • Conditions
    • Spell being cast = Fire Ball
  • Actions
    • set Casting_Unit = CasterUnit[index number of owner of Triggering Unit]
    • set Target Unit of ability being cast = TargetedUnit[index number of owner of Triggeirng Unit]
    • Wait 3.00 seconds
    • Unit - Pause TargetedUnit[index number of owner of Triggering Unit]
    • Unit - Order CasterUnit[index number of owner of Triggering Unit] to damage TargetedUnit[index number of owner of Triggering Unit] for 500 using attack type Spells and damage type Fire.
    • Wait 1.00 seconds
    • Unit - Unpause TargetedUnit[index number of owner of Triggering Unit]
Now if we look at the actions step by step, we can see how this works. Lets pretend that we are Player 3 and we just cast a spell on another unit. The first 2 actions of the trigger set the unit that is casting the spell to CasterUnit[3] and the target of the spell to TargetedUnit[3]. Since we indexed the array by using our player number, it will save the units in that Unit Array in the "3rd slot". After those 2 variables have been set. There is a wait for 3 seconds. After the wait we give the command to Pause the unit that was saved earlier as TargetedUnit[3] and then we order CasterUnit[3] to deal 500 damage to TargetedUnit[3]. There is another wait, and then a Unpause action to unpause TargetedUnit[3].

You may be asking, what is the difference with using just normal global variables and arrays here? The difference is, if you used single global variables to save the "TargetedUnit" and "CasterUnit" they could be changed to totally different units if the spell was cast by someone else before your spell resolved! What this type of array setup insures, is that each player will always have their specific variables that cannot be overwritten by other player's casting the same spell. This is because the only way to access CasterUnit[3] and TargetedUnit[3] is if Player 3 owned the triggering unit. the same goes for Player accessing CasterUnit[4] and TargetedUnit[4]. I hope this has helped!
 

Attachments

  • oldarraytutorial.txt
    7.4 KB · Views: 992
Last edited by a moderator:
Top