How can I correctly recan the pathfinder after updating colliders?

Hi,

I’m trying to get simple programatic 2D room generation working with pathfinding.

I have the pathfinder object set up for 2D grid with 2D physics collision detection (circle collider) as shown in tutorials. (I also have the AI and target set up and these work and pathfinding correctly).

I can create a room in the editor with floor and wall tiles, press ‘scan’ and it displays the walkable / non walkable areas ands connections as expected with the floor walkable and some no walkable areas around the walls.

However When I generate the same room programatically (also resizing it as needed) I have to manually call tell each node if its walkable or not and this results in a slightly different result to clicking ‘scan’. (If I click scan after programatically generating the room the graph will change).

I build the room with prefabs laid out on a grid, with each prefab having a collider and GraphUpdateScene script. I then use the code below to try to update the pathfinder.


AstarData data = AstarPath.active.data;
var gg = AstarPath.active.data.gridGraph;

gg.SetDimensions( _width, _depth, _nodeSize);
gg.center = new Vector3(_width / 2 , -(_depth / 2), 0);

AstarPath.active.AddWorkItem(new AstarWorkItem(ctx =>
{
	gg.GetNodes(node =>
		{
			// get the gameobject tile at the node position 
			var tile = _lm.GetTileAt((Vector3)node.position);
			
			// see if the tile was  set to walkable when created in its data (floor tile = walkable)
			node.Walkable = tile != null && tile.GetComponent<Tile>().Walkable;
		   
			gg.CalculateConnections((GridNodeBase) node);
		});
	}));

// Scans all graphs
AstarPath.active.Scan();

But I know this isnt right as it doesnt seem to be using colliders to check whats walkable any more and isnt giving the same results as scan does with its connections. Also if I try creating a new grid it always ends up 90 degrees out in the editor even with 2D set so I ditched that approached.

Can you show me what I’m doing wrong please? I really simple code snipet is what I’m after.

Thanks
Phil

Hi

What is the desired behavior? Do you want to set the walkability of the nodes manually or do you want to use the result that the grid graph scan gives you? Right now you seem to be trying to do both and I’m not sure what you actually want.

Hi Aron,

For now I’d like just a quick and easy ‘just what the grid graph scan gives me’ as i’m just prototyping really. I am in interested to know what was wrong with the manual method though or if there is a simple example somewhere in the docs for setting manually.

Thanks

If you just want to scan the graph then what you are doing by calling AstarPath.active.Scan should work fine (without your work item). Is something not working with that?

If you want to use a work item then you should calculate the connections after you have set all the walkability of the nodes. See https://arongranberg.com/astar/docs/graph-updates.php#scripting

Scan doesnt seem to work as I would expect.
I’ve made up a sample scene to demonstrate.

Here is what the floor and wall prefabs look

This is how it looks when I make the scen with above prefab in the editor and click scan.

This is a scene where I generate the prefabs at runtime then run the code below after that in an Update.

This is that same (runtime) scene if I click the scan button on the pathfinder.

This is the code that does the scan with work item removed.

 public void Regenerate2()
    {
        AstarData data = AstarPath.active.data;
        var gg = AstarPath.active.data.gridGraph;

        gg.SetDimensions(_width, _depth, _nodeSize);
        gg.center = new Vector3(_width / 2, -(_depth / 2), 0);

        AstarPath.active.Scan();
    }

Thanks

edit: I just noticed that the floor tiles are in a seperate layer to the walls i.e. they are not included in the detection.

So is this a bug? There seems a definate discrepancy between the editor and programatically rescanning, assuming I’m not doing something stupid.

Hi

Are you sure the tilemap has been updated when you scan the graph?
I think it’s possible that tilemaps do some kind of deferred update. Try to wait for 1 frame before you scan the graph.

Also. Regarding the 1 node border around your walls. You can reduce your Grid Graph -> Collision Testing -> Diameter setting to 0.99 or something which will ensure that the circle that is used for collision testing doesn’t touch the walls when it shouldn’t. Alternatively you can change the Collider Type field to Point.

Hi,

The problem seems to be creating the tiles in the same update as the rescan. If I wait until the next update it draws it as I was expecting.

Good call.
Cheers

1 Like