• 🏆 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!

Lemme see if I understand Arrays

Status
Not open for further replies.

hdm

hdm

Level 9
Joined
Nov 19, 2011
Messages
384
If I have, for example, PositionUnit Array, and create multiple positions with this PositionUnit, but set the [numberhere] to anything different for each other it will work ? For example. In one trigger I have Set PositionUnit [1] = there, and other trigger i have Set PoistionUnit [2] = here (the same variable but with other number). Will it work ? No crash ? No bug ? (except for the leak that I can remove) Or do I have to create other variable ? Like in one trigger Set PositionUnit and in the other Set OtherPositionUnit ?
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
that is exactly what array is actually. Array basically represents collection of variables that have the same name and are only differentiated by integer value. This has several applications, like easy iterating compared to having 10 variables with different names.

However, when it comes to Location variable, if you dont use Wait, Polled Wait or timer expire events, you dont really need to array it, but you still can
 
Level 22
Joined
Sep 24, 2005
Messages
4,821
Think of arrays as your fingers, each finger can hold rings (data). They're all fingers but the rings are placed on a specific finger.

let f be the group of fingers (on your hand) and the numbers be the individual finger;

f[1]=no ring,f[2]=no ring,f[3]=no ring,f[4]=no ring,f[5]=no ring;

now, if you wanted to put the "ruby ring" on the fifth finger on your hand, you do this;

set f[5]=ruby ring

That's about it. Simple ain't it?
 
Level 26
Joined
Aug 18, 2009
Messages
4,097
Arrays are a type of data structure. Data structure means it's a compound of multiple elements organized in a certain way. Arrays are a very simple case where the elements have to be of the same type and you store/read the individual element by presenting an integer index, for one-dimensional arrays at least. Imagine the table in Excel, just use one column, then you have a number of rows each with a certain position (a vector), those are the slots, the position is the index. A two-dimensional array would be multiple columns and rows (a matrix), so you provide two indizes. A three-dimensional one would be like multiple Excel tables and so on. However, Wc3 natively only supports 1d arrays. The index range is from 0 to 8190 statically (8191 is kinda bugged).

The major advantage of data structures is that you can write the code more dynamically. For example, if you wanted to count the kills of multiple units separately, it would require multiple integer variables and you would have to pick the right variable for the unit. This would result in IF/THEN/ELSE branching or more triggers, one solution for each unit. The problem arising becomes even more evident when more units spawn during runtime. You won't have code prepared for them, at maximum have reserves, but that would be a big mess and quite limited. So a proper solution would be to give each unit a unique index for an array (at least until it gets removed again) and dynamically address the memory space meant for the specific unit by presenting its index to the array. It's similar as if you were creating new variables during runtime.
 
It seems like you get the concept.

Basically:

castLocation = Location of Triggering Unit
targetLocation = Target point of ability being cast

>

location[1] = Location of Triggering Unit
location[2] = Target point of ability being cast

[x] each number is a unique variable.
This. I suggest instead of creating ~5 temp point variables, use TempPoints[] instead (with the 's' so it is distinguished between maps).
 

Dr Super Good

Spell Reviewer
Level 63
Joined
Jan 18, 2005
Messages
27,195
This. I suggest instead of creating ~5 temp point variables, use TempPoints[] instead (with the 's' so it is distinguished between maps).
Actually that is not the recommend approach. In C/C++ you can get away with it because the static index compiles to a constant offset (similar to reading struct/class members) however in JASS it is a fully parsed argument which requires another stage of execution. As such you should use arrays as a dynamic storage space and instead use non-arrays for static storage.

It's better to use dedicated variables though because else you have to pay attention to not call events. You cannot have triggers carelessly running in parallel when using superglobals.
The memory model of WC3 is such that only 1 thread is ever accessing it at any time and that all changes it makes are immediately consistent with the next running thread. Since this implies that 2 threads can never run simultaneously it is impossible for triggers to be "running in parallel".

The problem with global registers (more appropriate name than "superglobals" since they act more like processor registers) is that like all registers they can only be assigned to store one value at a time for a job. If the job generates another jobs that needs to use the same global register then the value of the register has to be stored somewhere. By the time the original job resumes the value in the global register has to be restored again so as to prevent a loss of data. This is your basic function/interrupt call procedure where it dumps registers to stack at a time defined by architecture and call convention.
 
Status
Not open for further replies.
Top