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.

1 Like

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!

2 Likes

Following this page:
https://arongranberg.com/astar/docs/saveloadgraphs.html
It heavily implies that SerializeGraphs() also includes node data by default because you have an explicit example to turn it off. I was about to ask this exact question, but found this thread instead.

Can you update the documentation on the above page with more detailed code examples, specifically how to write the saved file to a given location and include node data? Something like the following code would be excellent:

using Pathfinding.Serialization;

public TextAsset graphData;

// Create a byte array that contains all settings and node data
byte[] bytes = AstarPath.active.data.SerializeGraphs(new SerializeSettings {nodes=true});
// Save the file to your Unity directory
Pathfinding.Serialization.AstarSerializer.SaveToFile(Application.dataPath + "/MyCustomFolder/MyGraphName.bytes", bytes);

// Then when you need to access the saved graph, deserialize it (make sure that your public TextAsset variable has the desired file dragged in the inspector)
if(graphData == null) {
  Debug.LogWarning("Saved pathfinding file not found!");
} else {
  AstarPath.active.data.DeserializeGraphs(graphData.bytes);
}