Refernce to AstarGrid2D

Hello,

I am trying to get a reference to the astargrid2d I created in my world object.
My world is a Node2d, had a child NavigationAgent2D

Inside it this is the code,

extends NavigationAgent2D
func _ready():
var world = get_node(“…”)
var astar_grid = AStarGrid2D.new()
astar_grid.region = Rect2i(0, 0, world.tiles_width, world.tiles_height)
astar_grid.cell_size = Vector2i(world.cell_width, world.cell_height)
astar_grid.update()

Then in my agent object, which is a child of the world object, I get a reference to the NavigationAgent2D using

navigation = get_tree().get_root().get_node(“World/NavigationAgent2D”)
I did a print ad this in fact returns the node

From my agent object, I am trying to do pathfinding but I am not sure of the syntax
I used this(below), I figured I would call my node, then get access to the variable but I am not having any luck.

navigation.astar_grid.get_id_path(position,target)

If you know how to fix this please let me know.

Thank you,

Since you declared astar_grid inside _ready its scope is limited to that function, i.e. outside of it, it doesn’t exist and can’t be used. So do this instead:

extends NavigationAgent2D

@onready var astar_grid = AStarGrid2D.new()

func _ready():
    var world = get_node("...")

    astar_grid.region = Rect2i(0, 0, world.tiles_width, world.tiles_height)
    astar_grid.cell_size = Vector2i(world.cell_width, world.cell_height)
    astar_grid.update()