Saving and loading maps
Map Graph comes with a basic system for saving and loading maps. This page describes how to use this system and what some of its limitations are.
Saving
In order to save your generated data, follow these steps:
- Create a Save Data object. Open the create menu and select "Create > Map Graph > Save Data > Save Data".
- Add an input parameter of type SaveData to the graph of which you want to save data.
- Open the graph and add a Save Texture Data node.
- Add a SaveData input parameter node to the graph and connect it to the "Save Data" port.
- Connect the "Texture" port to the node of which the output you want to save.
Now whenever the graph is processed, it will save the texture data to the Save Data object, so it can be loaded at a later time.
Loading
In order to load saved data from a Save Data object, follow these steps:
- Create a Load Graph. Depending on whether you use GameObjects or Tilemaps, you'll want to create a "Load GameObjects Graph" or a "Load Tilemap Graph". You can find the options to create these under "Create > Map Graph > Save Data".
- Assign a Named Color Set to the newly created graph. You'll likely want this to be the same Named Color Set as assigned to the graph that originally saved the data.
- Click "Add to scene" in the inspector to add a runner for the load graph to the scene.
- Assign values to the input parameters.
- Assign the Save Data object that contains the data you want to load to the Save Data parameter.
- Assign the tilemap/gameobject you want to load the result into, to the Tilemap/Container parameter.
- Assign the appropriate tileset/prefab set to the Tileset/Prefab Set parameter. This is likely the same set used when generating the original saved data. Though you can use different sets, if you want to load the saved data but in a different style, using different tiles/prefabs.
Now whenever the load graph is processed, it will restore the saved data to the given tilemap or parent gameobject.
One thing to note is that the load graphs, are graph objects like any other, meaning you can open them and modify them to better suit your needs. Or you can create your own loader graph from scratch, by creating a new graph and adding one or more Load Texture Data nodes to it.
Limitations
There are some limitations to be aware of:
- A Save Data object can only contain one set of Texture Data, meaning that if you want to save and load multiple layers of tiles/objects, you'll need to create a Save Data object for each of them.
- The saved data contains texture data, that means it doesn't store the exact entries that are picked for each type from a set. That means that when loading the map, a new entry is selected, which might differ from the one that was used when the map was saved. For example, if you have a "Tree" tile type in your tileset, with multiple different trees, a tree tile might have a different tree after loading than when it was saved.
More advanced saving and loading
For a more complex save/load solution, you should either look for an existing solution or create your own.
Map Graph doesn't (and probably won't) provide a more advanced save/load system. The reason being, that saving and loading is a complex subject in its own right, that is best left to already existing solutions that specialize in solving this problem.
Something to be aware of when looking for a more advanced save/load solution
If your project is using Unity tilemaps, be aware that tilemaps are not serializable out of the box and thus might not be supported by every existing solution.