4 ms·
Is there a sane way to create an 2d array in GDScript yet?
by Joyfield 5y ago
Is there a sane way to create an 2d array in GDScript yet?
- ehnto 5y agoI remember having an epiphany over the ordeal so maybe I found something. I will update this comment when I get home and read the code.
- zubspace 5y agoNo, but like in every other programming language you can implement something like that yourself either by 1) nesting arrays (columns) within an array (rows) and access items them by map[x][y] or by using a single array and transform x and y into an index like y*width+x. I always take the second approach and wrap it in a class [1]. [1] https://github.com/ZuBsPaCe/godot-base-code/blob/main/Scripts/Tools/Map.gd https://github.com/ZuBsPaCe/godot-base-code/blob/main/Script...
- krapp 5y agoYou can also emulate a 2D array in 1D. For array[w][h] you would allocate array[w * h] and instead of indexing array[row][col] you would index array[w * row + col]. Wrap all of that in a nice API and for all intents and purposes it's the exact same thing. see https://stackoverflow.com/questions/2151084/map-a-2d-array-onto-a-1d-array https://stackoverflow.com/questions/2151084/map-a-2d-array-o...