AStar

    Category: Core

    AStar class representation that uses vectors as edges.

    A* (A star) is a computer algorithm that is widely used in pathfinding and graph traversal, the process of plotting an efficiently directed path between multiple points. It enjoys widespread use due to its performance and accuracy. Godot’s A* implementation make use of vectors as points.

    You must add points manually with and create segments manually with connect_points. So you can test if there is a path between two points with the function, get the list of existing ids in the found path with get_id_path, or the points list with .

    • float _compute_cost ( from_id, int to_id ) virtual

    Called when computing the cost between two connected points.


    • _estimate_cost ( int from_id, to_id ) virtual

    Called when estimating the cost between a point and the path’s ending point.


    • void add_point ( int id, position, float weight_scale=1.0 )

    Adds a new point at the given position with the given identifier. The algorithm prefers points with lower to form a path. The id must be 0 or larger, and the weight_scale must be 1 or larger.

    If there already exists a point for the given id, its position and weight scale are updated to the given values.


    • are_points_connected ( int id, to_id ) const

    Returns whether there is a connection/segment between the given points.



    • void connect_points ( int id, to_id, bool bidirectional=true )

    Creates a segment between the given points. If bidirectional is , only movement from id to to_id is allowed, not the reverse direction.


    • void disconnect_points ( id, int to_id )

    Deletes the segment between the given points.


    • get_available_point_id ( ) const

    Returns the next available point id with no point associated to it.


    • int get_closest_point ( to_position ) const

    Returns the id of the closest point to to_position. Returns -1 if there are no points in the points pool.


    • Vector3 get_closest_position_in_segment ( to_position ) const

    Returns the closest position to that resides inside a segment between two connected points.

    The result is in the segment that goes from y = 0 to y = 5. It’s the closest position in the segment to the given point.


    Returns an array with the ids of the points that form the path found by AStar between the given points. The array is ordered from the starting point to the ending point of the path.

    If you change the 2nd point’s weight to 3, then the result will be [1, 4, 3] instead, because now even though the distance is longer, it’s “easier” to get through point 4 than through point 2.


    • get_point_connections ( int id )

    Returns an array with the ids of the points that form the connect with the given point.


    • get_point_path ( int from_id, to_id )

    Returns the position of the point associated with the given id.


    • float get_point_weight_scale ( id ) const

    Returns the weight scale of the point associated with the given id.


    Returns an array of all points.


    • has_point ( int id ) const

    Returns whether a point associated with the given id exists.


    • is_point_disabled ( int id ) const

    Returns whether a point is disabled or not for pathfinding. By default, all points are enabled.


    • void remove_point ( id )

    Removes the point associated with the given id from the points pool.


    • void set_point_disabled ( int id, disabled=true )

    Disables or enables the specified point for pathfinding. Useful for making a temporary obstacle.


    • void set_point_position ( int id, position )

    Sets the position for the point with the given id.


    • void set_point_weight_scale ( int id, weight_scale )

    Sets the for the point with the given id.