Hi all! Carrying over some code from a Columns puzzle clone. I'm trying to produce the gdscript equivalent of:

``````typedef struct TILE
{
int x,y,type;
} TILE;
TILE grid;

//example
grid.type = 3

//example
int x = 0;
int y = 0;
for(a = 0; a < 16; a++)
{
for(b = 0; b < 16; b++)
{
grid[a][b].x = x;
grid[a][b].y = y;
y += 16;
}
x += 16;
y = 0;
}
``````

So just a struct to a 2D array, giving the tiles as many properties as desired. Would anyone know how all this might be written in gdscript? Been stumbling on it for a while! Thanks!!

in Engine

+1 vote

You can use Dictionary:

``````var grid = null

func get_default_tile(): -> Dictionary
var tile = {}
tile.x = 0
tile.y = 0
tile.type = 0
return tile

func get_tile_grid(width, height): -> Array
grid = []
for x in range(width):
grid [x]=[]
for y in range(height):
grid [x][y]= get_default_tile()
return grid

grid = get_tile_grid(16, 16)
# change tile 0, 1:
grid.x = 100
grid.y = 200
grid.type = 10
``````
by (189 points)
selected by

Thanks! Got it working borrowing a couple changes from a similar Q&A on here for 2D arrays, only now with the dictionary. Going to roll with this and see how it goes!

``````var grid = null

func get_default_tile() -> Dictionary:
var tile = {}
tile.x = 0
tile.y = 0
tile.type = 0
return tile

func get_tile_grid(width, height) -> Array:
grid = []
for x in range(width):
grid.append([]) #changed from grid [x]=[]
for y in range(height):