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();
}
}