I made a 2D list, although it’s more like a list of lists
So is this like an Array?
[1,2,3,4,5
6,7,8,9,10
11,12,etc]
It seems a little large just for that
yes, except it’s multiple nested within another.
like:
{{1,2,3,4}
{4,3,2,1}
{6,9,6,9}}
I make tables in my game by using the modulo operator (%) to create the width of the table,
and using math to locate or changes those values in the table.
Table X = (Index-1) % Width + 1
Table Y = Math.ceil (Index / Width)
Index = Table X + (Table Y - 1) * Width
(I had to double check in Pack-It)
For example:
If I had a list with 40 index, and I’ve turned it into a table that is 10 wide.
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | |
11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | |
21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | |
31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 |
I can locate or change the variables by using the expressions above.
So let’s change (6,3) to 82.
Index = 6 + (3 - 1) * 10
Index = 26
Change Index 26 into 82
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | |
11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | |
21 | 22 | 23 | 24 | 25 | 82 | 27 | 28 | 29 | 30 | |
31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 |
More Table Manipulation
And we can actually do this one step further if you want to find an index from another index. But also we have to make sure we are still inside the list, so the expressions below are true if results do not equal 0.
Name | Expression | True | False
---------------------------------------------------------------------
Row Up = Index - Width > 0 ? Index - Width : 0
Row Down = Index + Width <= List Size ? Index + Width : 0
Column Right = Table X + 1 <= Width ? Index + 1 : 0
Column Left = Table X -1 > 0 ? Index - 1 : 0
Let’s change the index that is a Row Down and a Column Left of (4,2) into 43.
Index = 4 + (2 - 1) * 10
Index = 14
Row Down check: 14 + 10 < 40?
Is 24 less than 40? Yes
Column Left check: [(14-1) % 10 + 1] - 1 > 0?
Column Left check: [4] - 1 > 0?
Is 3 greater than 0? Yes
Down Left of 14 = 14 + 10 - 1
Down Left of 14 = 23
Change Index 23 into 43
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | |
11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | |
21 | 22 | 43 | 24 | 25 | 82 | 27 | 28 | 29 | 30 | |
31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 |
(And this is how my Advance Tilemap Example works)