Implementation of custom pathfinding system for Godot Engine 4.3+, based on the A* algorithm (AStarGrid2D).
To add pathfinding functionality to an object of the Node2D class (or a class that inherits from it), you need to add a Pathfinder2D node to it as a child element. After that, you need to specify the map on which the route will be calculated (PathfindingMap) and the final destination for Pathfinder2D. If pathfinder receives all the necessary data in the specified order, it will find a route to the final destination, which can be obtained through special functions: get_path_coords(), get_path_positions(), get_next_path_position().
Example
Setting up PathfindingMap for Pathfinder2D of an enemy character from the game level code:
func _ready() -> void:
# Set the pathfinding map for Pathfinder2D.
$EnemyCharacter.pathfinder.pathdinding_map = PathfindingMap.new($TileMapLayers/GroundLayer, __get_impassable_cells())
returnSetting the final destination for Pathfinder2D (updated every physical frame) in the enemy character's code:
func _physics_process(_delta: float) -> void:
# Set the final destination for Pathfinder2D.
pathfinder.target_position = movement_target.position
# Move the character if the target is not reached.
if not pathfinder.is_target_reached():
velocity = __calculate_velocity()
move_and_slide()
return
func __calculate_velocity() -> Vector2:
# Get direction to the nearest point from the found path.
var direction : Vector2 = position.direction_to(pathfinder.get_next_path_position())
if direction.length() > 1.0:
direction = direction.normalized()
return direction * speedThis project uses custom node icons based on the open-source icon set from Phosphor Icons.
Phosphor is a flexible and open-source icon family for interfaces, diagrams, presentations — whatever, really. Licensed under the MIT License.
This project is licensed under the MIT License.
You are free to use, modify, and distribute it under the terms of that license.