Struggling with a random walk and Godot

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

Hi,
I’m testing a random walk algorithm, it seems to be correct, but when I try to test it instanciating tiles, things go wrong. The game world is made of squared tiles, let’s say 5x5 for testing:

[(-2, 0, -2), (-2, 0, -1), (-2, 0, 0), (-2, 0, 1), (-2, 0, 2), (-1, 0, -2), (-1, 0, -1), (-1, 0, 0), (-1, 0, 1), (-1, 0, 2), (0, 0, -2), (0, 0, -1), (0, 0, 0), (0, 0, 1), (0, 0, 2), (1, 0, -2), (1, 0, -1), (1, 0, 0), (1, 0, 1), (1, 0, 2), (2, 0, -2), (2, 0, -1), (2, 0, 0), (2, 0, 1), (2, 0, 2)]

A random walk of size 5 tiles has the following translations:

[(0, 0, 0), (-1, 0, 0), (-1, 0, -1), (-2, 0, -1), (-2, 0, -2)]

To see the result, I try to erase this translations from the previous array:

func erase_array_from_array(array : Array, erase : Array) -> Array:
	array = array.duplicate()
	for i in range(erase.size()):
		array.erase(erase[i])
	return array

So the tiles translations are:

[(-2, 0, 0), (-2, 0, 1), (-2, 0, 2), (-1, 0, -2), (-1, 0, 1), (-1, 0, 2), (0, 0, -2), (0, 0, -1), (0, 0, 1), (0, 0, 2), (1, 0, -2), (1, 0, -1), (1, 0, 0), (1, 0, 1), (1, 0, 2), (2, 0, -2), (2, 0, -1), (2, 0, 0), (2, 0, 1), (2, 0, 2)]

This seems to work. The problem is when instanciating the tiles:

var tile : Spatial
for i in range(tilesTranslations.size()):
	tile = ASSETS3.EmptyTile.instance()
	tile.translation = tilesTranslations[i]
	add_child(tile)

And the result is:

Obviously, the result is wrong, since the tile at (0, 0, 0) shoudn’t exists.

Full part of the code:

var tilesTranslations : Array = _generate_tiles_translations()

var walk = SECTOR_FUNCS.random_walk2(5, Vector3.ZERO, tilesTranslations)

print(tilesTranslations)
tilesTranslations = GEN_FUNCS.erase_array_from_array(tilesTranslations, walk)
print(tilesTranslations)
print(walk)


var tile : Spatial
for i in range(tilesTranslations.size()):
	tile = ASSETS3.EmptyTile.instance()
	tile.translation = tilesTranslations[i]
	add_child(tile)

Are you sure ASSETS3.EmptyTile only contains one tile? It is rather odd that on a designated 5x5 grid you get 6x6 tiles.

skysphr | 2021-09-10 15:28

Oh, I didn’t noticed! I checked in the past but it seems I changed something. Now it is fixed and everithing works!

abelgutierrez99 | 2021-09-10 16:20