Changes

Jump to navigation Jump to search
Line 99: Line 99:     
== Per save data ==
 
== Per save data ==
You can also save data to the player's save file, to keep settings per save, by overriding '''ModMeta''''s '''Serialize''' and '''Deserialize''' methods. These use a '''WriteDictionary''', which is just a dictionary of whatever you want, and the game will handle turning it into bits.
+
You can also save data to the player's save file, to keep settings per save, by overriding your '''ModBehaviours'''' '''Serialize''' and '''Deserialize''' methods. These use a '''WriteDictionary''', which is just a dictionary of whatever you want, and the game will handle turning it into bits.
   −
'''Please note that any custom classes saved using this method ''has'' to have an empty constructor'''. If you want some fields not to be serialized, use the '''[System.NonSerialized]''' attribute. Properties are not serialized, so you need to create backing fields.
+
The WriteDictionary is powerful, in that it has no limitations on the data you can save, but '''please note that any custom classes saved using this method ''has'' to have an empty constructor'''. If you want some fields not to be serialized, use the '''[System.NonSerialized]''' attribute. Properties are not serialized, so you need to create backing fields. Saving custom classes from your mod will also break saves if the mod is no longer installed, so it's best to avoid when you can.
   −
When you want to save data, create a '''WriteDictionary''' and populate it with values and return it in the '''Serialize''' method, this will then be returned when a save is loaded in the '''Deserialize''' method.
+
When you want to save data, create a '''WriteDictionary''' and populate it with values and return it in the '''Serialize''' method, this will then be returned when a save is loaded in the '''Deserialize''' method. If you wanted to save a setting you can write '''data["Notifications"] = true''' and load it back with '''Notifications = data.Get("Notifications", true)'''
    
There is also a '''LoadMode''' parameter, which tells you whether it was a full save, a build mode save or a company save (Usually used when the player is moving to another map or during multiplayer synchronization), which will allow you to only save data needed for each type. E.g. you probably don't have to save player preferences for a build mode save.
 
There is also a '''LoadMode''' parameter, which tells you whether it was a full save, a build mode save or a company save (Usually used when the player is moving to another map or during multiplayer synchronization), which will allow you to only save data needed for each type. E.g. you probably don't have to save player preferences for a build mode save.
 +
 +
You can also override the serialization methods in the ModMeta class to take complete control of saving data. Returning null will make the mod not save any data.
    
To help you with serializing data from your ModBehaviours, the ModMeta class has a '''ModBehaviours''' list of all active ModBehaviours that were creating when the mod was first loaded.
 
To help you with serializing data from your ModBehaviours, the ModMeta class has a '''ModBehaviours''' list of all active ModBehaviours that were creating when the mod was first loaded.
 +
 +
Note that your the save data is identified by the Name you've set in the ModMeta class. If you change this name, it will no longer be able to find the correct data from older saves and name clashes will result in data loss.
    
== Reading external data ==
 
== Reading external data ==

Navigation menu