Support Forum

Help with saving grid graph

Hi,

I’ve been trying to save my grid graphs at runtime and reload these again as part of my save game system.

As part of my game logic, I place buildings that cut the grid graph and make some nodes unwalkable.

I read the documents and am trying to achieve this using SerializeGraphs() and DeserializeGraphs().

My code is:

graphBytes = AstarPath.active.astarData.SerializeGraphs();
AstarPath.active.astarData.DeserializeGraphs(graphBytes);
AstarPath.active.Scan();

I’ve done some testing and the bytes file is successfully saved and loaded and by tweaking a setting on the graph (max climb) and reloading I can see that the value is reloaded so it seems to be passing the data back into the pathfinding system on load; however, as you can see from the screenshots the unwalkable nodes are no longer saved and instead seem to have some random grid nodes that have become unwalkable.

On save:

After load:

Is there something more specific that needs to be done to save this data for grid graphs?

Many thanks,

Stephen.

Hi

SerializeGraphs by default only serializes graph settings. When you load it you can, as you do right now, call AstarPath.active.Scan to scan the graph.
However, if you have made modifications to the graph that are not represented in the world (e.g. you used a GraphUpdateObject to manually set a few nodes to be unwalkable) this will not be saved.
You can instead save all the node data by passing a settings object:

var bytes = AstarPath.active.data.SerializeGraphs(new SerializeSettings {
    nodes = true
});

then when you load this data you should not scan the graph again. Everything will be just as you left it.

Ah ok that makes sense. I think I got a bit confused with the documentation part here:

There is an easy to use API for saving and loading files.
This will serialize graph settings to a byte array. By default node info is included (assuming the graphs have been scanned first).

I read that as though the node information was there just from the normal call without passing the settings object.

Thanks for the help Aron!

Hmmm, I’m not at a computer where I can check the code right now… I thought it was excluding node settings by default, but if the documentation says it does then I might not be remembering correctly.
In any case, if it does include node data then you should not scan the graph after you load it as that will recalculate everything.

Hi Aron,

Thanks for looking at this. I’ve not had chance until just now to test this I was going to report back which way it worked.

It does not seem to be automatically including the node data as I’ve tried just doing the normal call and without scanning but this does not work (I also get an error that the graph is not scanned so can’t update the area) when trying to then use pathfinding after loading.

Is there another namespace I need to be referencing for saving the settings? I get the normal unity warning that I am missing one when trying to use SerializeSettings . I have using Pathfinding; and from the docs it would seem that is all I need.

Ok found it needs using Pathfinding.Serialization;

Also I just tested and it works perfectly passing the settings object.

Thanks again!

1 Like