Tile Based movement collision management while objects move concurrently

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

Hello, I manages to implement a tile based movement for my game and I am using RayCast2D in order to detect collisions. The code for collision management looks like this :

desired_pos = new_position
ray.target_position = desired_pos
if not ray.is_colliding():

In order to move one tile further, this code gets called multiple times (each time I interpolate the movement until I reach the next tile).
It works well when I only manage the player with this code, but things get a little buggy when there are also other moving NPC’s using this code.

enter image description here

For example, in this image if both the green sprite and black sprite go to the right side at the same time, the black sprite could cross the red sprite.

Does anyone know a better way to manage movements ?

:bust_in_silhouette: Reply From: PinkPenguin

I thought about storing an array representing the tilemap in which each sprite could reserve a tile before movement and keep it locked during the movement, but I don’t know about any easy way of implementing it as it requires conccurent access management in the code.

:bust_in_silhouette: Reply From: PinkPenguin

I managed to find a fix.
I put every moving objects into a specific group and attribute to every objects a dictionnary containing the free tiles.
And whenever an object is moving I check that the target tile is free and I lock it by adding this tile in the dictionnary for every elements inside the group (using call_group).
Once the movement is done, I remove the tile from the dictionnaries.

Unfortunatelry there isn’t sets in GDscript, so I could have some performance problems if there are too many moving objects.
In my case, it should be ok as I won’t have more than 20 or 30 moving objects in my maps. But, if anyone have got a better solution don’t hesitate to post.