Tiny Town

Pathfinding component

This tutorial will show you how to implement and use the pathfinder component in your blueprints. The pathfinder uses the grid to find the shortest path between two tiles. This means that in order to work, the component needs a TT_GridManager object in the scene.

Step 1: Initialising the pathfinder

First, we need to add the TT_Pathfinder component to our blueprint. There are two ways of doing so:

- Adding it in-editor via the Add Component button. 
- Adding at runtime via the Add Component node.

If added via the Add Component, make sure to create a variable out of the Return Value so that we can access the component later on. From that component reference you can then access the functions discussed in the following steps.

Step 2: Getting TileIDs

In order to get the pathfinding nodes to work, we'll need to get the tile IDs of the tile we want to find a path to. (If you don't fully understand what a tile ID or a tile zone, see here). There are multiple ways of retrieving TileIDs:

You can get the tile ID under the mouse cursor with GetTileUnderMouse() in TT_PlayerGridCamera. It will return the currently hovered tile (is = -1 if no tile is hovered), and the last tile to be hovered.

You can get the tile IDs occupied by block from TT_Block itself with GetOccupiedTileIDs(). It will return the tile at the center of the building as well as an array of all the tiles the block is occupying.


Finally, you might need to get a reference to a zone (a rectangular zone of tiles). Use GetzoneTileIDsFromZoneParameters() to get all the tile IDs of the zone delimited by two tiles. There are four main functions to help you calculate tile zones. For more help, see here.

Step 3: Pathfinding functions

There are two algorithms included, A* and Dijkstra. Both behave slightly differently, Dijkstra tends to output straighter paths (with the least turns) where A* will go straight towards the goal.

The A* algorithm is usually the prefered one as it is more efficient. However, the current Dijkstra implementation is slightly different, it requires a zone to work in. The output path if existent is guaranteed to be in the input zone.

AllowDiagonalPaths will allow the algorithms to search for diagonal paths instead of only looking for paths that goes up, down left or right. The resulting path will be shorter.

Finally, BlockIDToIgnore will make the pathfinder ignore specific blocks placed on the grid instead of finding a way around them.

Step 4: Using TileIDs

Once you have the shortest path from a tile to another, there are multiple functions available to use these TileIDs. 

Probably the most important will be GetTileLocation() from TT_GridManager. It will simple return the world location of a specific tile.

IsTileUsed() will return true if  a specific tile is currently being occupied by a block. 

GetDistanceBetweenTiles() will return a rounded value of the distance between two tiles.

GetTileNeighbours() returns the tile IDs corresponding to tile neighbouring a specific tile. By default it will return the top, right, bottom and left tile, toggling AllowDiagonalPaths will make it return the diagonals too (top right, bottom right etc ...).

And that's it you should now all be set to use the grid pathfinding component, in your blueprints!

Last updated: August 2019
  • GitHub
  • Sketchfab
  • LinkedIn
  • Twitter