How to edit 2D array?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Vododovoodvod

Hi everyone,

I have data stored in 2D array, this would be example:

var stage_tiles_matrix = [[1, 1, 1], [1, 1, 1], [1, 1, 1]]

I would like to access certain number and change it. Something like this:

stage_tiles_matrix [1][2] = 0

resulting in:

stage_tiles_matrix = [[1, 1, 1], [1, 1, 0], [1, 1, 1]]

However, when I type something like that I get error:

Expected ']'

How can I access certain value in such data structure and modify if?

When I ran your code, it worked. Did you have a syntax error somewhere?

exuin | 2020-09-15 21:59

I just ran it again in neutral environment and it indeed worked.
I will double check it when I get home. Maybe issue lies in stage_tiles_matrix variable since I don’t declare it like here in example, but I read it from json file.
Eliminating stage_tiles_matrix [1][comment1-2] = 0 line as source of problem is still step forward.

I probably phrased data wrong, maybe engine treats it like string at the moment?

Anyway thanks!
I will post results when I get home and check variable more thoroughly.

Vododovoodvod | 2020-09-16 06:22

:bust_in_silhouette: Reply From: Magso

The stage_tiles_matrix[1] will get the first Vector3 which is a struct not an array so stage_tiles_matrix[1][2] doesn’t make sense to it. In the example you’re getting the 2nd index of the array and altering the z axis so it would be


as in Array[index] and Vector3.z.

I don’t think [1, 1, 1] is a Vector3. It looks like an Array to me.

exuin | 2020-09-15 22:54

Oh my bad they are arrays :/, try typing it

var stage_tiles_matrix : Array = [[1, 1, 1], [1, 1, 1], [1, 1, 1]]

Magso | 2020-09-15 23:29

Thanks for the answer, I checked line again and looks like issue lies in data type of variable.
I didn’t declare it like here in example, but read it from json file so probably Godot treats it like string at the moment.

I will post results when I get back home and test it in project environment.

Vododovoodvod | 2020-09-16 06:27

:bust_in_silhouette: Reply From: Vododovoodvod

So, it was typo after all.

If anyone with similar problem is looking for solution - this is good syntax:

stage_tiles_matrix [1][2] = 0

I made a mistake and wrote it like this:

stage_tiles_matrix [1,2] = 0

Don’t code tired.