In vJASS it is possible.Just as the title says is it possible to make a 2d array in the simple jass ?
For the first instance i need it to make a quest array for all the players, and secondly i need it for an item table Thanks in advance
function Set2D takes integer playerId, integer secondIndex, <type> value returns nothing
set udg_myVar[playerId + 16 * secondIndex] = value
endfunction
function Get2D takes integer playerId, integer secondIndex returns <type>
return udg_myVar[playerId + 16 * secondIndex]
endfunction
playerId | secondIndex | playerId + 16 * secondIndex |
0 | 0 | 0 |
0 | 1 | 16 |
0 | 2 | 32 |
0 | 3 | 48 |
0 | 4 | 64 |
0 | 5 | 80 |
0 | 6 | 96 |
0 | 7 | 112 |
0 | 8 | 128 |
0 | 9 | 144 |
0 | ... | ... |
1 | 0 | 1 |
1 | 1 | 17 |
1 | 2 | 33 |
1 | 3 | 49 |
1 | 4 | 65 |
1 | 5 | 81 |
1 | 6 | 97 |
1 | 7 | 113 |
1 | 8 | 129 |
1 | 9 | 145 |
1 | ... | ... |
2 | 0 | 2 |
2 | 1 | 18 |
2 | 2 | 34 |
2 | 3 | 50 |
2 | 4 | 66 |
2 | 5 | 82 |
2 | 6 | 98 |
2 | 7 | 114 |
2 | 8 | 130 |
2 | 9 | 146 |
2 | ... | ... |
... | ... | ... |
It is pretty simple. For example, if you want to have a 2d array where one index is a player's id, you can do:
Here the 16 comes from the fact that player ids range from 0 to 15 (to my recollection).JASS:function Set2D takes integer playerId, integer secondIndex, <type> value returns nothing set udg_myVar[playerId + 16 * secondIndex] = value endfunction function Get2D takes integer playerId, integer secondIndex returns <type> return udg_myVar[playerId + 16 * secondIndex] endfunction
If you want to understand why this works, you can stare at the results of the [playerId + 16 * secondIndex] function and see how it doesn't generate overlapping indexes i.e. numbers in the third column occur only once for the given values:
playerId secondIndex playerId + 16 * secondIndex 0 0 0 0 1 16 0 2 32 0 3 48 0 4 64 0 5 80 0 6 96 0 7 112 0 8 128 0 9 144 0 ... ... 1 0 1 1 1 17 1 2 33 1 3 49 1 4 65 1 5 81 1 6 97 1 7 113 1 8 129 1 9 145 1 ... ... 2 0 2 2 1 18 2 2 34 2 3 50 2 4 66 2 5 82 2 6 98 2 7 114 2 8 130 2 9 146 2 ... ... ... ... ...
How ? I don't understand. 2d arrays must have 2 indexes, its what I used in other programing language to make a matrix.looks like a 2d array too me, takes 2 integers. its a pew-newgen 2d array
How ? I don't understand. 2d arrays must have 2 indexes, its what I used in other programing language to make a matrix.
In the table you created an equation inside the [] that will result in different number when you change the 2 variables it consists of but it is a single array still.
And this applies to any array in any programing language or in JASS scripting only ?That's how a 2D array works under the surface.
You don't just tell your compiler to put something at [4][3] and it magically stores it somewhere.
A 2D array is a normal array just with some calculations (the above) that tell it where to store/get things.
I am pretty sure, though, they usually do it in blocks and not spread like that (as in i * Max + j).
This makes more sense as it allocates a block for each 'i'.
And in case you don't see how that makes a 2D array, say you want a 4x4 array, the first index is [0][0] which will turn into 0*4+0 = 0.
The second index is [0][1] which will turn into 0*4+1 = 1.
The last index is [3][3] which will turn into 3*4+3 = 15.
And so on and on.
The exact same thing happens for any dimension above one, but it gets more and more messy.
I can't say that I comletely understand but do you know of any site that can illustrate this more precisly.Yes.
In some low-level programming languages you get much more power over arrays. For example: in C arrays can be used as pointers. A pointer *points* to a memory adress in your RAM memory. Arrays effectively are pointers to an adress. The calculation is very simple:
Say "array" points to adress 1000. Say it's an array of "integers", this means every integer is 4 bytes large.
array[0] points to 1000 + 0*4 = 1000
array[1] points to 1000 + 1*4 = 1004
array[2] points to 1000 + 2*4 = 1008
etcetera.
If it would be an array of long integers (8 bytes - this type doesn't exist in warcraft), then it would be very similar:
array[0] points to 1000 + 0*8 = 1000
array[1] points to 1000 + 1*8 = 1008
A 2D array can be seen as an "array of arrays"". So if you have an array of long integers (8 bytes) of maximum size 4x4, you get:
array[0,0] points to 1000 + (0*4*8) + (0*8) = 1000
array[0,1] points to 1000 + (0*4*8) + (1*8) = 1008
array[0,2] points to 1000 + (0*4*8) + (2*8) = 1016
array[0,3] points to 1000 + (0*4*8) + (3*8) = 1024
array[1,0] points to 1000 + (1*4*8) + (0*8) = 1032
As you can see, the 2D array is just like the 1D array a pointer to a memory adress, where the index specifies an offset from that adress. In the case of multiple indices, each index specifies their own offset from the starting adress.
Normal integers like 1,2,3,4 .... they are as binary stored in those bytes ?You use the size of the data type you are storing in the array because Eleandor is talking about the actual bytes that the array is stored in.
A normal integer is 4 bytes long, so it makes sense that every index takes 4 bytes.
A short integer is 2 bytes long, so it makes sense that every index takes 2 bytes.
Same goes for any size.
Address: 1000 1001 1002 1003 1004 1005 1006
Offset: 00 01 02 03 04 05 06
Value: 00 01 02 03 04 05 06
Address: 1000 1004 1008 1012 1016 1020 1024
Offset: 00 04 08 0C 10 14 18
Value: 00 01 02 03 04 05 06
It always takes exactly one byte so it can be between -127 and 127 (forget unsigned integers for now)
01111111b
0x7F
127d
128 will be
10000000b
0x 80
128d
Isn't that supposed to count as 1 byte also ?