How to make objects follow a tilemap

Godot Version

4.4

Question

I’m trying to make a railway system for my game and now I need to make wagons move around the railway path.


The rails work using a tilemap with terrain autotiling and I place them by code. How could I make the wagons follow the path?

Wagon only needs to know what tile it’s on and which tiles are valid connections. Then move the wagon between connections based on whatever direction logic you provide (e.g. move forward when possible for a very easy example) and change the rotation/animation based on the start tile and the target tile (so 4 right turns; 4 left turns + up down, down up, left right, right left).

You can figure out valid connections at runtime whenever your wagon is on a new tile or store all rail data in a dictionary whenever you instance a wagon. No idea what the best approach is here tbh and it depends on your game. First option is lightweight for a single wagon and a lot of tracks, second option is better when you’re moving a lot of wagons over the same track. For the example you show it’s not going to impact performance either way.