Pathfinder Reference

C++ docs of the plugin. The GDScript names are right below each function. These are the ones which can be used in Godot.

Properties

bool

debug_mode

false

int

turn_costs

1

bool

rotate

false

int

collision_layer

2147483647

Descriptions

class PathFinder : public Reference

Pathfinder lets you search paths with A* algorithm, with anti Zig-Zag improvements

Public Functions

void Initialize(TileMap *tm, bool diag)

GDScript: initialize

Must be called from godot, with a reference to the tilemap

Parameters
  • tm: tile_map object. If Pathfinder is owned by a tilemap, just write "self"

  • diag: include diagonal pathfinding

PoolVector2Array SetTarget(bool isStart, Vector2 position)

GDScript: set_target

Sets the target of start- or end position. This can be set independently from each other. Pathfinder stores the information and starts calculating a path as soon as both (start and end) were set.

Return

An array of pathpoints from tile to tile, from start to end position

Parameters
  • isStart: true for start position, false for end position

  • position: the corrisponding position of start or end

PoolVector2Array SetPath(Vector2 startPosition, Vector2 endPosition)

GDScript: set_path

An simplified function to calculate a path. Under the hood this just calls SetTarget twice, for start- and end positions.

Return

An array of pathpoints from tile to tile, from start to end position

Parameters
  • startPosition: Start of path calculation

  • endPosition: End of path calculation

Vector2 Follow(Node2D *node, float speed)

GDScript: follow

Moves a node along the path. The path must be calculated before calling this function. Either by

SetTarget or SetPath. A good place for this function wouyld be in _process(delta) and use also the delta value for determining the speed
Return

Normalized direction in which node was moved

Parameters
  • node: The node that will be moved along the path

  • speed: How fast it moves from pathpoint to pathpoint. A value of 1 here would exactly jump from point to point and nothing in between.

float GetPositionOnPath()

GDScript: get_path_position

Returns current position on path, where 0 = start and end = 1.

Return

floating number between 0 and 1

Public Members

bool debugMode = false

GDScript: debug_mode

if true: visual helpers are displayed at path calculation. This includes:

  • tile-costs on each tile, which was considered by the algorithm

  • the path will be drawn as a red line

  • collision shapes will be drawn for tiles - ocupied by obstacles.

    It should be set BEFORE

    Initialize

int turnCosts = 1

GDScript: turn_costs

Adds costs for turns on the path. The pathfinding algorithm searches for the path with the lowests costs. A higher value here means turns are more expensive - thus, turns are decreased. This will result in more straight lines and less "ZIG-ZAG"

bool rotateToPath = false

GDScript: rotate

Rotate the node along the path during

Follow. This affects global rotation.

int64_t obstacleCollisionLayer = 2147483647

GDScript: collision_layer

Layer Mask for testing collisions while calculating the path. By default, all 32 layers are included (0 - 31). The result is this number. 2^31 = 2147483647