[Solved] Saving and loading gridgraphs from files

Hello,

I am trying to save a gridgraph to a file when the player saves the game, and then load the gridgraph the next time the player chooses to load a saved game.

However, it seems like the “tags” on the nodes are not working. During the game I tag certain pathable areas to make them have no penalty – which makes them walkable paths. Then I save the gridgraph to a file, and when I reload it, the tags are not working for some reason.

But maybe I’m not saving and loading the gridgraph correctly, so I just wanted to ask if I’m doing something wrong.

Here is my saving and loading code (save to file and load from file):

/// <summary>
    /// Save the modified gridgraph (A* grid) for loading later on.
    /// </summary>
    public void SaveModifiedGridGraph()
    {
        if (File.Exists(Application.persistentDataPath + "/ModifiedGridGraphForLoading.zcr"))
        {
            //AstarPath.active.Scan();

            BinaryFormatter bf1 = new BinaryFormatter();
            FileStream file1 = File.Open(Application.persistentDataPath + "/ModifiedGridGraphForLoading.zcr", FileMode.Open);
            //file1 = File.Open(Application.persistentDataPath + "/ModifiedAlphaMapInfoForLoading.zcr", FileMode.Open);

            byte[] bytes = AstarPath.active.astarData.SerializeGraphs();

            bf1.Serialize(file1, bytes);
            file1.Close();
        }
        else
        {
            //AstarPath.active.Scan();

            //Otherwise, if the persistent data file does NOT exist yet, create and save the modified terrain
            //alpha maps file on the player's computer.
            BinaryFormatter bf2 = new BinaryFormatter();
            //The ".zcr" extension here stands for "Zoo Creator" at the moment.  I can change if I finalize a game name.
            FileStream file2 = File.Create(Application.persistentDataPath + "/ModifiedGridGraphForLoading.zcr");
            file2.Close();

            file2 = File.Open(Application.persistentDataPath + "/ModifiedGridGraphForLoading.zcr", FileMode.Open);

            byte[] bytes = AstarPath.active.astarData.SerializeGraphs();

            bf2.Serialize(file2, bytes);
            file2.Close();
        }

        Debug.Log("I saved the modified GridGraph.");
    }






    /// <summary>
    /// Load the Astar GridGraph that was saved to a file (currently only one main file, but hopefully later I can
    /// associate different grid graph save files with corresponding game saves by the player (i.e. multiple files for multiple games)).
    /// </summary>
    private void LoadAstarGridGraphFromFile()
    {
        if (File.Exists(Application.persistentDataPath + "/ModifiedGridGraphForLoading.zcr"))
        {
            BinaryFormatter bf = new BinaryFormatter();
            FileStream file = File.Open(Application.persistentDataPath + "/ModifiedGridGraphForLoading.zcr", FileMode.Open);
            byte[] bytes = (byte[])bf.Deserialize(file);
            file.Close();

            AstarPath.active.astarData.DeserializeGraphs(bytes);

            AstarPath.active.Scan();
        }
    }

Hi

Which version of the package are you using?

Hi Aron,

Well, I don’t know how to find the version specifically, but according to the changelog, I think I’m using version 3.8.1. Is there a better way to find the specific version other than the changelog? Just wondering. I think it is 3.8.1 though.

It’s listed in the About tab, but the changelog version should be accurate too.
Well, 3.8.1 is a really old version, almost 3 years old at this point, there might have been a bug in that version that caused tags not to be serialized correctly (though I cannot recall any such bug). Your loading and saving code looks correct from what I can see.

If you cannot get it to work, as a workaround I suppose you could serialize the Tag field for every node in your file as well, and after the graphs have been loaded you assign the tags again (the nodes array should have exactly the same length and the nodes come in the same order, so it should be relatively easy to do).

Hmm, ya I’ve tried several things and it doesn’t seem to be loading the tags properly, so I think I’ll try to do it in a way you suggested.

I’m actually not that good at understanding your documentation/structure (sorry), so is there any chance you could post some example code on:

  1. How to get/access the gridgraph’s nodes array properly
  2. How to iterate through it and get the node tag and node position

I usually just have problems with getting the proper syntax for things like these.

Thanks Aron :slight_smile:

var grid = AstarPath.active.astarData.gridGraph;
for (int i = 0; i < grid.nodes.Length; i++) {
    serialize-in-some-way(grid.nodes[i].Tag);
}

...

for (int i = 0; i < grid.nodes.Length; i++) {
    grid.nodes[i].Tag = deserialize-in-some-way;
}

I got it to work now, thanks Aron. The issue is solved/resolved. :slight_smile:

1 Like