GODOT 4.2 AStarGrid2D - dont work =(

Godot Version

GODOT 4.2 AStarGrid2D - Why dont work ?
Tuturial https://www.youtube.com/watch?v=DkAmGxRuCk4

Question

print(id_path) # print EMPTY " " WHY ???

<< extends Node2D

u/onready var tile_map = $“…/TileMap”

var astar_grid: AStarGrid2D

Called when the node enters the scene tree for the first time.

func _ready():
astar_grid = AStarGrid2D.new()
astar_grid.region = tile_map.get_used_rect()
astar_grid.cell_size = Vector2(100,100)
astar_grid.diagonal_mode = AStarGrid2D.DIAGONAL_MODE_NEVER
astar_grid.update()

func _physics_process(delta):
if Input.is_action_pressed(“click”):
> var mousX = get_global_mouse_position().x
var id_path = astar_grid.get_id_path(
tile_map.local_to_map(global_position),
tile_map.local_to_map(get_global_mouse_position())
)
print(id_path) # print EMPTY " " WHY ???
print(int(mousX))>

Please format your code into code blocks by selecting it all and pressing Ctrl+e. This makes it much easier to debug code, particularly as GDScript cares a lot about whitespace.

I recreated the project and used your code, it works fine.
image

Do you get any warnings or errors?
Could you post a screenshot of the nodes in your scene?

i don’t know the issue off the top of my head but i have examples of AStarGrid2D working in GitHub and example code you can see here: Pathfinding in Godot with AStarGrid2D – baylor does game stuff. It looks pretty similar to your code though:

Setup:

var astar_grid = AStarGrid2D.new()
astar_grid.cell_size = tilemap.tile_set.tile_size
astar_grid.region = tilemap.get_used_rect()
astar_grid.update()

Are your tiles really 100x100? If so, cool, i’m just verifying.

Use:

var start_coord_map = terrain_tilemap.local_to_map(to_local(start_coord_global))
var goal_coord_map = terrain_tilemap.local_to_map(to_local(goal_coord_global))
var path_in_map_coords = astar_grid.get_id_path(start_coord_map, goal_coord_map)

You pass a global position to one expecting local. That could cause problems but global and local are often the same so it often doesn’t matter.

It mostly looks okay so here’s my recommendation: Don’t inline the conversion of variables (in fact, never do that, ever). Print them. See if they make sense. Also print the grid info. If you ask for (500,600) which local_to_map turns into (5,6) and your grid reports its bounds as (-5,4) then you’re trying to find a path that starts or ends not on the map, which will give you an empty id_path. i got a lot of those when i first setup my unit conversions.

So your code looks fine but i recommend getting specific, concrete coordinates and making sure they match what you think you’re passing.

https://ibb.co/LZGkML9

You’ve got 39 errors at the bottom. Are they relevant to the issue you are experiencing?

1 Like

Write error:_physics_process(): Can’t get id path. Point (0, 0) out of bounds [P: (1, 1), S: (5, 5)].

Thx i m understand :star_struck: My TileMap didn’t start from the center. Now all work

Object out TileMap
https://ibb.co/n3JkkJL
And it seems to me that this may be due to a mismatch between the global and local coordinates of the cursor

How inject set_cell (any_Tile …) in Path ? How draw_line(last_point, current_point, DRAW_COLOR, BASE_LINE_WIDTH, true) change in Tiles ?

i don’t understand the question but would a working example help?

Here are several path finding demos: Godot Pathfinding Demo by baylorw

Here’s the code for those demos: GitHub - baylorw/Godot-PathFinding-Feature-Demo: Feature demos of pathfinding in Godot.

That should have modifying tiles and drawing lines. i’ve got demos for drawing lines with graphics primitives, Line2D and a tile map just for path lines (lots of different ways to draw).

i am try rewrite tutorial godot 3.5 BTStartegy img in godot 4.2
Просмотр вложения - Форум программистов и сисадминов Киберфорум I couldn’t change it and no one on that forum can help me

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.