Arrays:How to use?

Zanderist

New Member
Reaction score
5
What can you use the array function on variables to do?

Have multiple variables of the same name?
 

Shura

New Member
Reaction score
45
An array will allow a variable to store multiple values in it, by making it require an index. For example if you had an integer array:
int[1]=4
int[2]=5
int[3]=7
 

DeeVee

New Member
Reaction score
4
Yes it is basically multiple variables of the same name. . . I have only used arrays for integer variables but I'm sure there is other uses for them.
 

PurgeandFire

zxcvmkgdfg
Reaction score
509
What can you use the array function on variables to do?

Have multiple variables of the same name?

http://world-editor-tutorials.thehelper.net/cat_usersubmit.php?view=20023

Yeah, kind of sort of.

Arrays are like several variables with the same name but a different way to refer to them. Like, if there were a pack of people named "John" all alike in every way, but each person had a different last name. The last name is kind of like an array index.

variableName [ array# ] = value
Ex:
Hello[0] = "Hello."
Hello[1] = "Hey."
Hello[2] = "Sup."
Hello[3] = "Yo."

If you displayed Hello[2] in chat, it'd say "Sup."
Arrays also allow you to take advantage of loops.
Trigger:
  • For each (Integer A) from 1 to 10, do (Actions)
    • Loop - Actions
      • Set IntegerVar[(Integer A)] = ((Player((Integer A))) Current gold)


For example, that will keep track of each Player's gold from Player 1 - Red to Player 10. You can refer to that variable by IntegerVar[Index of Player].
 

Bogrim

y hello thar
Reaction score
154
There's no short explanation for the usage of arrays, depending on the structure of your trigger. The key to understanding arrays is the integer value you use to access them, which allows you to systemize the index for flexible use. I recommend you just experiment and learn from other triggers until you get the logic behind arrays.
 

Zanderist

New Member
Reaction score
5
Okay let's say if I use a unit variable and array it.

I have now 5 units under the same variable (call this variable Y) under the array.

Now I have an X unit.

I want to know which out of the 5 "Y" units is closest to the X unit?

(keep in mind that every time I build a unit type "Y" it adds 1 to an integer variable which is for the later use in the Integer A, and another trigger that takes 1 away if one dies)

Would I use a loop integer"For each integer A" that would check through all of the 5 units to determine which unit was the closest?
 

Bogrim

y hello thar
Reaction score
154
That's not how you use an array. You're using the array as a replacement for a unit group. Just add the units to a group and run a unit group action checking the distance between points, then store the unit in a variable. For example:

Trigger:
  • Actions
    • Set Temp_Point = (Position of X)
    • Set Temp_Unit = No unit
    • Unit Group - Pick every unit in Unit Group and do (Actions)
      • Loop - Actions
        • Set Temp_Point2 = (Position of (Picked unit))
        • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
          • If - Conditions
            • Temp_Unit Equal to No unit
          • Then - Actions
            • Set Temp_Unit = (Picked unit)
            • Set Temp_Real = (Distance between Temp_Point and Temp_Point2)
          • Else - Actions
            • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
              • If - Conditions
                • (Distance between Temp_Point and Temp_Point2) Less than Temp_Real
              • Then - Actions
                • Set Temp_Unit = (Picked unit)
                • Set Temp_Real = (Distance between Temp_Point and Temp_Point2)
              • Else - Actions
        • Custom script: call RemoveLocation( udg_Temp_Point2 )
    • Custom script: call RemoveLocation( udg_Temp_Point )

After running this loop, Temp_Unit will be the unit closest to your position.
 

jig7c

Stop reading me...-statement
Reaction score
123
Unit Group - Pick every unit in Unit Group and do (Actions)

* Loop - Actions
o Set Temp_Point2 = (Position of (Picked unit))
o If (All Conditions are True) then do (Then Actions) else do (Else Actions)
+ If - Conditions
# (Integer A) Equal to 1
+ Then - Actions


where the hell is Integer A coming from?
you are running a unit loop, not a integer loop!
 

LmmcDarkY

New Member
Reaction score
1
One more question... what if i set array to default 1 or 0 idk that means i can store any number in array ?
 

IronTrout

New Member
Reaction score
20
Yes and no.

Generally i leave my arrays at default. But once in a blue moon you'll have errors where a second array isn't being made (this has happened to me once ever). You just need to know when setting the array that "Yes, it can happen, so never rule out the possibility that it may be the number of array's you have set that is screwing up your code"
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top