Skip to content

Unified SaveLoad methods

Maceeiko (Maceeiko Development) edited this page May 1, 2023 · 4 revisions

SaveLoad methods

In MSCLoader 1.2.8+ there is a new method to store your saved stuff, based on feedback from other modders it brigs a unified save location for all mods into a signle file.

Best part is you don't need to trash your current save system, instead of saving your current save to file, you can save and load it as byte[] array into this new save method.

Simple example of saving and loading single values or Lists

bool testBool = false;
int testInt = 395352;
Vector3 testVector = new Vector3(34f, 0, 12.5f);
List<string> testList = new List<string>() { "test1", "test2", "test3" };
//OnSave
SaveLoad.WriteValue(this, "testBool", testBool);
SaveLoad.WriteValue(this, "testInt", testInt);
SaveLoad.WriteValue(this, "testVector", testVector);
SaveLoad.WriteValue(this, "testList", testList);

//OnLoad
bool readBool = SaveLoad.ReadValue<bool>(this, "testBool");
int readInt = SaveLoad.ReadValue<int>(this, "testInt");
Vector3 readVector = SaveLoad.ReadValue<Vector3>(this, "testVector");
List<string> readList = SaveLoad.ReadValueAsList<string>(this, "testList");

valueID - valueID needs to be uniqueID for your mod, you can read value stored under that ID using ReadValue methods

Serialize class

There is new method to serialize custom class int unified save file. To serialize your class just add in OnSave

SaveLoad.SerializeClass(this, myClassToSerialize, "mySerializedClass");

To deserialize your class just do in OnLoad

MyClass myClass = SaveLoad.DeserializeClass<MyClass>(this, "mySerializedClass");

In SerializeClass there is optional parameter to "encrypt" your data.
If you decide to do it you need to also set optional parameter to true in DeserializeClass

Saving your custom save file to unified save file

If you already have custom save method, you can also save that file into unified save file.
If your current save file is in binary format it's recommended to save it as byte[] array
Examples:

SaveLoad.WriteValue(this, "mySaveFile", myByteArray);

To Load it:

byte[] myByteArray = ReadValueAsArray<byte>(this, "mySaveFile");

Serialize save file to custom .json file

SerializeSaveFile and DeserializeSaveFile methods, serializes file to custom .json file

Clone this wiki locally