0 votes

Hello,

I created a room out of sprites and randomly placed obstacles and enemies through an array. Here, the enemies use astar to chase the player for the shortest distance.

But sometimes PoolVector2Array will be null and it won't chase the player even if a point is captured.

I'll leave a link for a simple project. Thank you for your help.

link: https://drive.google.com/drive/folders/1W2FLBN4FF_Fj_8YzlBUweoOdC3VPAfcI?usp=sharing

Godot version 3.3.2
in Engine by (75 points)

1 Answer

0 votes
Best answer
extends Node2D


onready var astar_node = AStar.new()
var map = {"width": 3, "height": 3}

func _ready():
    var previous = null;
    for y in range(3):
        for x in range(3):
            var point = Vector2(x, y)
            var point_index = calculate_point_index(point)
            astar_node.add_point(point_index, Vector3(point.x, point.y, 0.0))
            if previous == null:
                previous = point
            else:
                astar_node.connect_points(calculate_point_index(previous), point_index, true)
                previous = point
            if point_index > map.get("width"):
                astar_node.connect_points(point_index - map.get("width"), point_index, true)

    var path = astar_node.get_point_path(0, 8)
    print("Are points connected? (5, 8) = ", astar_node.are_points_connected(5, 8))
    print(path, astar_node)


func calculate_point_index(point):
    return point.x + map.get("width") * point.y

It's a "dirty" code based on godot demo pathfind_astar.
I create a map 3x3 and connect vertically and horizontally and just find the path. Only Node2d is used

by (48 points)
selected by

Thank you! I'll test it.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.