• 2D
  • Design for grid based game

Hi, After doing a research on grid based games in godot i found two approaches: -first one by GDQuest (

he's created grid array which contains informations about entity that is on a tile(empty, player, enemy, obstacle) and for example if player wants to move to specific tile he checks if it is empty and if it is player moves -second by KidsCanCode (https://kidscancode.org/godot_recipes/2d/grid_movement/), he's using CollisionsShapes2D and RayCast2D Could anyone elaborate on pros and cons of these two?

Based on how you describe(too lazy to watch the video) 1. You need to declare large 2d array (even more worse if you make 3d game, 3d array) which most elements basically do nothing, heavy memory sink. But it is fast to access the data and easier to code. 2. Memory allocate just base on how many objects at giving time. I don't know much about this, but I think using physics will loop and check every possible collider objects (even if you implement the check function without physic yourself, unless you know very well how you would handle the data, you still need to loop all the objects) which effect the performance. Yes, if you compare to the first one, this one is much harder to code, not as simple as grid[x][y].

I present you this, which is hybrid of 1 and 2

var grid = {}

func read(x, y):
	if !grid.has(x) || !grid[x].has(y):
		return null
	return grid[x][y]

func write(x, y, d):
	if !grid.has(x):
		grid[x] = {}
	grid[x][y] = d

Explain - {} is Dictionary object, of course dictionary is take a bit more memory than array if it is about same size; but in this case, dictionary would use far less memory than array in (1) because it will contain far less data. - Dictionary.has() is the function that loop dictionary object; if it is found the key that match, it will break the loop and return true, overwise return false. - These 2 functions mostly loop x axis of grid object, which the number of object that store in grid object is not much, so it have better performance than (2). - At line 4 if !grid.has(x) return true, it will ignore !grid[x].has(y), so it is just loop only the x axis of grid object. - Even this can rewrite data, I didn't provide how to remove data properly. You can't just simply write null. If you use as is, it may be a lot of garbage data leftover. Do some research and implement it yourself.