Encapsulating grid graph to fit level at runtime?

I procedurally generate my level, so I’m unable to accurately predict how large it is or in what direction it will go in. Right now, I’m using an obscenely large grid graph to ensure that the entire level is pathed.

To somewhat lessen the performance impact of this, I’m trying to manually adjust width/depth of the grid graph found in AstarPath.astarData.gridGraph but it doesn’t seem to take effect. Is there somewhere else I’m supposed to be doing this? Is a grid graph even suitable for this?

Hi

Yes, that is possible.
See this discussion: http://arongranberg.com/vanillaforums/discussion/1180/rescale-grid-graph-during-run-time/p1

Thanks. I’ve got the graph size updating, but it’s now registering everything as unwalkable. Here’s the code I’m using:

`void SetupPathfinding() {
Bounds b = new Bounds();
foreach (Tile tile in dungeon.tiles) {
b.Encapsulate(tile.Placement.Bounds);
}

GridGraph gg = AstarPath.active.astarData.gridGraph;
gg.center = b.center;
gg.width = Mathf.CeilToInt(b.size.x);
gg.depth = Mathf.CeilToInt(b.size.z);
gg.UpdateSizeFromWidthDepth();
AstarPath.active.Scan();

}`

This is only called after the dungeon is generated. To test this to make sure it’s being called after the generation, I tried manually setting the width/depth like normal and then use this:

void SetupPathfinding() { AstarPath.active.Scan(); }

Which properly detected unwalkable/walkable areas, so I know that’s not the problem. Any idea what’s wrong?

I should also mention that I tried updating the graph directly with “gg.Scan();”, but this results in the following error:

Exception: Trying to initialize a node when it is not safe to initialize any nodes. Must be done during a graph update

Hi

Are you sure the center of the graph is below (or at least at the same height) as the ground. If the center is above the ground, it cannot detect it.

Also, the width and depth fields are the number of nodes, not the world size.

Yes, the GridGraph.Scan method is only used internally. In more recent versions it has been renamed ScanInternal.

Ah, it was the height of the center of the graph. Didn’t notice it. Got it working now.

Quick last question: It seems grid graphs ignore mesh colliders. Is there a way around this? While I wouldn’t use many, if any at all mesh colliders in the final build, when prototyping it’s much quicker to generate a quick mesh collider rather than parenting a bunch of box colliders to match the shape of the room.

Does it ignore mesh colliders??
That’s really odd. The system just uses the UnityEngine.Physics API to check for collisions, and that should work for all types of colliders.
Are you sure they are not in a layer that is excluded from collision/height testing?