| − | All '''ModBehaviours''' have the '''SaveSetting''' and '''LoadSetting''' method to save data globally. Note that '''SaveSetting''' and '''LoadSetting''' are generic and will call ToString() and try to convert the saved string to the data type you specify, so this will only work for simple types. | + | All '''ModBehaviours''' have the '''SaveSetting''' and '''LoadSetting''' method to save data globally. Note that '''SaveSetting''' and '''LoadSetting''' are generic and will call ToString() and try to convert the saved string to the data type you specify, so this will only work for simple types, like integers, floats, doubles, bools, strings, etc. To load something back, use '''LoadSetting<type>("Key", defaultvalue)''', e.g. '''LoadSetting<bool>("Notifications", false)'''. |
| | 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. | | 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. |