| | All '''ModBehaviours''' have the '''SaveSetting''' and '''LoadSetting''' method to save data globally. Note that '''SaveSetting''' takes a string, but '''LoadSetting''' is generic and will try to convert the string to the data type you specify. So to save an integer you can use '''SaveSetting("Integer", value.ToString())''' and '''LoadSetting<int>("Integer")''' to load it again. | | All '''ModBehaviours''' have the '''SaveSetting''' and '''LoadSetting''' method to save data globally. Note that '''SaveSetting''' takes a string, but '''LoadSetting''' is generic and will try to convert the string to the data type you specify. So to save an integer you can use '''SaveSetting("Integer", value.ToString())''' and '''LoadSetting<int>("Integer")''' to load it again. |
| − | 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. '''Please note that any custom classes saved using this method ''has'' to have an empty constructor'''. 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. 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 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. '''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. 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. 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. |