The example code can be used to explain some of the details of what I am trying to save:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class WorldManager : MonoBehaviour
{
[System.Serializable]
public class Database
{
public List<Item> gameItems = new List<Item>();
}
public Database database;
[System.Serializable]
public class BuildableData
{
public List<FreeformBuildableData> placedFreeformBuildables = new List<FreeformBuildableData>();
}
public BuildableData buildableData;
//Update the extra data of a buildable requesting a data update:
public bool UpdateFreeformExtraData (string buildableID, string newData)
{
int foundID = buildableData.placedFreeformBuildables.FindIndex(fid => fid.uniqueID == buildableID);
if(foundID != -1)
{
buildableData.placedFreeformBuildables[foundID].extraData = newData;
return true;
}
return false;
}
//Load the world:
private void LoadWorld ()
{
}
//Save the world:
public void SaveWorld ()
{
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class FreeformBuildableData
{
//What is the ID of this buildable?:
public string uniqueID = "";
//Where is this buildable and what angle is it facing?:
public Vector3 pos = new Vector3(0, 0, 0), rot = new Vector3(0, 0, 0);
//The other data the buildable contains. This can for example be a chest inventory
//or if this buildable is a crafting table then the items stored in the crafting UI:
public string extraData = "";
}
The idea would be to convert “buildableData” into JSON and save it to a text file. This however can result in a very long text file. Is there any more efficient way to save the data?
How many FreeformBuildableData elements in the list do you consider?
One of multiple ways, you can save vectors as 6 ints, which reduces much of data by far. If you need precision, just multiply by 10, 100, etc.
Serializing to binary manually instead of json would be much more size efficient. You wouldn’t be saving all the extra text describing the variables, and your Vector3’s would be just 12 bytes each without any loss of precision. Though your serialization code would be more difficult to maintain.
Keeping with Json though, if you’re saving to disk using UTF16 (Encoding.Unicode) you would cut the file size by about half by just switching to UTF8. That’s if you’re primarily using ASCII compatible characters in those strings.
Freeform Buildables are objects placed by the player (or players if on a server) such as chests and crafting benches. There may be dozens or if they build enough, hundreds.
FreeformBuildableData represnets the data behind the object, such as the position, rotation, and items stored in the object if is a chest-object.
Few thousands lines for txt file is nothing really.
So if you have 500 chests, each 10 items, that just 5k lines of text.
Even if it takes few seconds to load, as for saving / loading, that is nothing to be concerned really, since this shouldn’t happen too frequent.
I would say, try, simulate and profile it.
See, if this will be anyhow of an issue.
ScriptableObjects are quite cool when it comes to save/load. They have native FromJson() ToJson() methods and there are some tutorials and examples out there how you can use them for save&load.