- 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]
-
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
-
Loop Actions -
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]
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
Last edited by a moderator: