General Anyone mind explaing Arrays
Reply
Anyone mind explaing Arrays
I have been trying to teach myself macros and these just surpass me.

/for i 1 to 10

This would change i to 1, next time i is used would it change it to 2? am i even using this right?


/declare MyArray[5] int

That would make 5 possible MyArrays? Or do i need to Define each?


Macro
More +

/Target ${NearestSpawn[1 NPC radius ${CR}]}
/delay 5
/if (${Target.ID} != ${TEMPID} && !${Target.Type.Equal[Corpse]} && ${SpawnCount[npc radius ${CR}]} > 1) {
/varset MyArray[i] ${Target.ID}
/delay 5
/Target ${NearestSpawn[2 NPC radius ${CR}]}
/delay 5
/if (${Target.ID} != ${TEMPID} && !${Target.Type.Equal[Corpse]} && ${SpawnCount[npc radius ${CR}]} > 1) {
/varset MyArray[i] ${Target.ID}
/delay 5
/Target ${NearestSpawn[3 NPC radius ${CR}]}
/delay 5
/if (${Target.ID} != ${TEMPID} && !${Target.Type.Equal[Corpse]} && ${SpawnCount[npc radius ${CR}]} > 1) {
/varset MyArray[i] ${Target.ID}
/delay 5
/Target ${NearestSpawn[4 NPC radius ${CR}]}
/delay 5
/if (${Target.ID} != ${TEMPID} && !${Target.Type.Equal[Corpse]} && ${SpawnCount[npc radius ${CR}]} > 1) {
/varset MyArray[i] ${Target.ID}
/delay 5
/Target ${NearestSpawn[5 NPC radius ${CR}]}
/delay 5
/if (${Target.ID} != ${TEMPID} && !${Target.Type.Equal[Corpse]} && ${SpawnCount[npc radius ${CR}]} > 1) {
/varset MyArray[i] ${Target.ID}
/delay 5





This is what i was messing around with to check and clear the Array also





Macro
More +


Sub Clear_Targets
:reclear
/for i 1 to 5
/target ${MyArray[i]}
/delay 1
/if (!${Target.Type.Equal[NPC]}) /varset MyArray[i] NULL
/delay 2
/if (i<5) /goto :reclear
/if (i=5) /return


Would anyone mind helping me refine my skills with these please ;)[/syntax]
Wed Jan 16, 2013 12:44 pm
i is a variable so you want brackets on it.
Also, i think the equals operator for macros is "=="
You want to group everything that is getting looped in brackets also, that way the macro knows when to loop back up to the /for

Macro
More +
Sub Clear_Targets
:reclear
/for i 1 to 5 {
   /target ${MyArray[${i}]}
   /delay 1
   /if (!${Target.Type.Equal[NPC]}) /varset MyArray[${i}] NULL
   /delay 2
   /if (${i}<5) /goto :reclear
   /if (${i}==5) /return
}
Wed Jan 16, 2013 3:43 pm
Project Lead
I'm so rusty, don't put brackets on your loop, end it with /next i


Macro
More +
Sub Clear_Targets
:reclear
/for i 1 to 5  
   /target ${MyArray[${i}]}
   /delay 1
   /if (!${Target.Type.Equal[NPC]}) /varset MyArray[${i}] NULL
   /delay 2
   /if (${i}<5) /goto :reclear
   /if (${i}==5) /return
/next i
Wed Jan 16, 2013 3:48 pm
Project Lead
I just noticed you were using /goto to finish the loop structure. /next is the last line that increments i and sends the flow back up to the /for.

Macro
More +
/for x 1 to 5
     /echo ${x}
/next x


Would output

1
2
3
4
5

Macro
More +
/for x 1 to 5
     /echo ${x}
     /varset x 1
/next x


Would infinitely loop and would output:
1
2
2
2
2
2
Etc

The first time through x is set to 1, so it prints a 1. Then varset sets it to 1, then next x sets it to 2 and sends it back to the top where it prints a 2, etc. it's because of this that you loop logic needs to be solid.
Wed Jan 16, 2013 9:03 pm
Project Lead
Macro
More +
/for x 1 to 5

     /echo ${x}

/next x


Cold be looked at as the following, the /for is just a shortcut sort of

Macro
More +
/varset x 1
:top
/if (${x}>5) /goto bottom

/echo ${x}

/varcalc x ${x} + 1
/goto :top
:bottom


The top 3 lines are the /for the bottom 3 are the /next

[/quote][/syntax]
Wed Jan 16, 2013 9:11 pm
Project Lead
/for i 1 to 10

This would change i to 1, next time i is used would it change it to 2? am i even using this right?


By itself no. The /next i command is necessary to create a loop and to increment i to 2, then 3, etc. See Maudigan's examples.


/declare MyArray[5] int

That would make 5 possible MyArrays? Or do i need to Define each?


That declaration creates a single array. However, that single array can hold 5 integers. You don't need to define each but you do need to /varset each if you want them to start with a number other than zero.

Macro
More +
Sub Clear_Targets
:reclear
/for i 1 to 5
/target ${MyArray[i]}
/delay 1
/if (!${Target.Type.Equal[NPC]}) /varset MyArray[i] NULL
/delay 2
/if (i<5) /goto :reclear
/if (i=5) /return


In the first code section, ${MyArray[${i}]} is an integer array populated with spawn ID's. If so, your /target commands in this quoted code section won't work. If you want to target by id, then you need the id parameter:
/target id ${MyArray[${i}]}

With that said, you don't need the target command at all:
Macro
More +
Sub Clear_Targets
   /declare i int local
   /for i 1 to ${MyArray.Size}
      /if (${Spawn[id ${MyArray[${i}]}].Type.NotEqual[NPC]}) /varset MyArray[i] NULL
   /next i
/return


Whenever you use temporary variables like the i in this case, you want to use local scope. That means you have to declare it in the same Sub.
Wed Jan 16, 2013 11:31 pm
Senior Project Member
Thanks alot for the guidance, helps alot!
Thu Jan 17, 2013 7:35 am
Macro
More +
Sub Main
/declare MyArray[5]    string outer NULL
:Start
/call Get_Targets
/call Clear_Targets
/goto :Start




Sub none
   /declare i int local
   /declare x int local
   /for i 1 to 5
   /for x 1 to 5
   /Target ${NearestSpawn[${x} NPC radius 60}
   /delay 5
   /if (!${Target.Type.Equal[Corpse]} && ${SpawnCount[${x} radius 60]} > 1) {
/varset MyArray[${i}] ${Target.ID}
/delay 5
/next i
 }
/return

Sub Get_Targets
/Target notid ${Target.ID} NPC Radius 60
/delay 5
/if (!${Target.Type.Equal[Corpse]} && ${Target.ID} != ${MyArray[1]} && ${Target.ID} != ${MyArray[2]} && ${Target.ID} != ${MyArray[3]} && ${Target.ID} != ${MyArray[4]} && ${Target.ID} != ${MyArray[5]}) {
/varset MyArray[1] ${Target.ID}
/delay 5
/echo ${i} ${Target.Name} Set to MyArray[1]
}
/Target notid ${Target.ID} NPC Radius 60
/delay 5
/if (!${Target.Type.Equal[Corpse]} && ${Target.ID} != ${MyArray[1]} && ${Target.ID} != ${MyArray[2]} && ${Target.ID} != ${MyArray[3]} && ${Target.ID} != ${MyArray[4]} && ${Target.ID} != ${MyArray[5]}) {
/varset MyArray[2] ${Target.ID}
/delay 5
/echo ${i} ${Target.Name} Set to MyArray[2]
}
/Target notid ${Target.ID} NPC Radius 60
/delay 5
/if (!${Target.Type.Equal[Corpse]} && ${Target.ID} != ${MyArray[1]} && ${Target.ID} != ${MyArray[2]} && ${Target.ID} != ${MyArray[3]} && ${Target.ID} != ${MyArray[4]} && ${Target.ID} != ${MyArray[5]}) {
/varset MyArray[3] ${Target.ID}
/delay 5
/echo ${i} ${Target.Name} Set to MyArray[3]
}
/Target notid ${Target.ID} NPC Radius 60
/delay 5
/if (!${Target.Type.Equal[Corpse]} && ${Target.ID} != ${MyArray[1]} && ${Target.ID} != ${MyArray[2]} && ${Target.ID} != ${MyArray[3]} && ${Target.ID} != ${MyArray[4]} && ${Target.ID} != ${MyArray[5]}) {
/varset MyArray[4] ${Target.ID}
/delay 5
/echo ${i} ${Target.Name} Set to MyArray[4]
}
/Target notid ${Target.ID} NPC Radius 60
/delay 5
/if (!${Target.Type.Equal[Corpse]} && ${Target.ID} != ${MyArray[1]} && ${Target.ID} != ${MyArray[2]} && ${Target.ID} != ${MyArray[3]} && ${Target.ID} != ${MyArray[4]} && ${Target.ID} != ${MyArray[5]}) {
/varset MyArray[5] ${Target.ID}
/delay 5
/echo ${i} ${Target.Name} Set to MyArray[5]
}
/return







Sub Clear_Targets
   /declare i int local
   /for i 1 to ${MyArray.Size}
      /if (${Spawn[id ${MyArray[${i}]}].Type.NotEqual[NPC]}) {
/varset MyArray[${i}] NULL
/echo MyArray[i] Target Cleared
}
        /if (${Spawn[id ${MyArray[${i}]}].Distance}>150) {
/varset MyArray[${i}] NULL
/echo MyArray[${i}] Target Cleared
}
   /next i
/return






Thanks i think this is going to work very nice, This is a test macro i threw together to see if it will work, It adds targets and takes them away Very nice
Going to Start adding the real subs in now and seeing if causes any problems, Once again Thx for the info
Thu Jan 17, 2013 10:27 am
Macro
More +

Sub Main
   /declare MyArray[5]    string outer NULL
   :Start
      /call Get_Targets
      /call Clear_Targets
   /goto :Start
|end all your subs with /return even|if it never gets used, just in case/return


|if something is logically contained within| another structure, indent it 3 spaces| this can include /for, Sub, goto (sometimes), /if.|blank lines between logically distinct portions| helps readability too, like between code and| variable definitions.
|in the following sub you had a nesting error| /for opens, /next closes. For an if, { opens, } closes| you had an opening (/for i) outside the /if, but| but the closing (/next i) is inside the /if| they are like russian nesting dolls. The top| and bottom of the outer doll are on the outside| you never have the top on the outside and the bottom| on the inside. Same thing here. You also don't have a| /next x anywhereSub none
   /declare i int local
   /declare x int local
   
   /for i 1 to 5
      /for x 1 to 5
         /target ${NearestSpawn[${x} NPC radius 60]}
         /delay 5
         /if (!${Target.Type.Equal[Corpse]} && ${SpawnCount[${x} radius 60]} > 1) {
            /varset MyArray[${i}] ${Target.ID}
            /delay 5
         }
      /next x
   /next i
/return

|this loop will also loop 25 times. The i| will loop from 1 to 5. When the i is 1| x will do a 1 through 5. When i is 2| x will go 1 through 5 again, etc.
Thu Jan 17, 2013 10:53 am
Project Lead
General Anyone mind explaing Arrays
Reply