I'm following Calneon's guide from reddit:
It is easy to setup for a grid. Just make a new node with a script that inherits from AStar2D, then:
- For each cell on your grid, call add_point with an id (just increment an index), and position (in world space). Do this with a 2D loop (for x in size_x: for y in size_y).
- Do your loop again, and this time for each cell you added previously, call connect_points with the id of the current cell, and the cells above, below, left, and right of it (if they exist).
- Then simply call get_point_path or get_id_path with the start and end position, which will give you an array of positions, or an array of ids for the path.
So far, I've got this, the rect of the tilemap is looped through and added as points (I'm not worried about obstacles yet, just getting this much working) but I'm stuck with the second loop:
func astarloop():
var ID = 0
for y in range(tilemapsize.position.y, tilemapsize.end.y):
for x in range(tilemapsize.position.x, tilemapsize.end.x):
astar.add_point(ID, Vector2(x,y), 1.0)
ID += 1
ID = 0
for y in range(tilemapsize.position.y, tilemapsize.end.y):
for x in range(tilemapsize.position.x, tilemapsize.end.x):
var IDpos
IDpos = astar.get_point_position(ID)
var left = IDpos + Vector2(-1,0)
var right = IDpos + Vector2(1,0)
var up = IDpos + Vector2(0,-1)
var down = IDpos + Vector2(0,1)
astar.connect_points(?,?)
ID += 1
I honestly have no idea how to do this second part since there's no astar.get_ID(point_position)
. I could do everything at the same time in the first loop, but I can't connect points that don't exist and it would be a nightmare to try and add multiple points per loop whilst trying to keep track of the ID.