Re: Modding Tutorials
Posted: Mon Apr 30, 2018 3:40 pm
A few words on how to use the IHasModSaveData interface recently introduced to allow mods to store and retrieve save data.
First of all we must define a class to hold the data that needs to be saved. SaveLoadManager sends and requests the mod an instance of this class, which is automatically serialized in a file specific to the mod.
In the worst case this file can be deleted but it may be not a bad idea to make use of the versioning system for a smoother upgrade.
When the mod is started we have to link the interface to the Mod instance given by InitParams. We can do this in Init, Start or another method used to retrieve the singleton.
The inteface requires one property and three methods to be implemented.
First of all we must define a class to hold the data that needs to be saved. SaveLoadManager sends and requests the mod an instance of this class, which is automatically serialized in a file specific to the mod.
In the worst case this file can be deleted but it may be not a bad idea to make use of the versioning system for a smoother upgrade.
Code: Select all
[FullSerializer.fsObject("v1")]
public class MyModSaveData
{
public string Text;
public List<int> Items;
}
Code: Select all
mod.SaveDataInterface = instance;
- SaveDataType
This is the type of the data class, used for deserialization.Code: Select all
public Type SaveDataType { get { return typeof(MyModSaveData); } }
- NewSaveData()
This is called when the mod is installed for the first time or the serialized file is missing for any reason.Code: Select all
public object NewSaveData() { return new MyModSaveData { Text = "Default text", Items = new List<int>(); }; }
- GetSaveData()
This method asks the data to serialize when a new save is created.
If the return value is null the file is not created.Code: Select all
public object GetSaveData() { return new MyModSaveData { Text = text, Items = items; }; }
- RestoreSaveData()
Give us the data deserialized (or result of NewSaveData()) to be applied in the running instance. Be sure to set all values to avoid leftovers from the previously running save.Code: Select all
public void RestoreSaveData(object saveData) { var myModSaveData = (MyModSaveData)saveData; text = myModSaveData.Text; items = myModSaveData.Items; }