-
Couldn't load subscription status.
- Fork 6
WCSharp.SaveLoad v1
IMPORTANT: The save/load system required several properties to be initialised! Please read on!
The save system is a powerful system that provides the following features:
- Automatic saving and loading of any number of key-value pairs
- Supports any number of save slots
- Automatically handling synchronisation of data
- Protects save files from tampering via hashcodes, optionally including the playername
- Supports converting a save to a typed class for easier working with the data
To configure the system, a total of 4 properties on the SaveSystem class need to be configured:
- Hash1
- Hash2
- SaveFolder
- BindSavesToPlayerName
For information as to what these should be set to, please refer to the information provided by IntelliSense in Visual Studio.
Before attempting to load any files, you need to attach an event listener to SaveSystem.OnSaveLoaded. You can simply type SaveSystem.OnSaveLoaded += and then use ALT+Enter to automatically generate the following code:
SaveSystem.OnSaveLoaded += SaveSystem_OnSaveLoaded;
...
private static void SaveSystem_OnSaveLoaded(Save save)
{
}Once we've set up the event listener, we can load a save file via SaveSystem.Load(player, saveSlot). The system will automatically load in the save file, verify its validity, and synchronize it across all players. If no save file is found, or the save file is invalid (e.g. it has been tampered with), the event will produce an empty save file.
Now that we have the Save class, we can use this to retrieve and store data. Note that all data is stored as text, but there are a number of methods to easily retrieve the desired type instead. However, converting the data and checking if the is still a bit of a hassle to constantly check for keys being present. In order to make this easier, we can use a method to convert the save into a more easily manageable form. For this, we need a new class that extends the Saveable class to store our data. An example of this can be found below:
public class SaveData : Saveable
{
public string HeroNameString { get; set; } = "";
public int LevelInt { get; set; }
public float ExperienceFloat { get; set; }
}We can receive an instance of this class, loaded with all of the data present in a save file, using the following method: var saveData = save.Deserialize<SaveData>();.
Due to the restrictions on reflection in Lua, a number of conditions must be met for the properties on this class:
- Strings must be initialised to some non-null value, e.g. an empty string
"". - Every property that should be saved/loaded needs to have its name suffixed with its type. You may add properties that are not suffixed to the class, but these will not be save/loaded automatically. The currently supported types are:
- String
- Byte
- SByte
- Char
- Decimal
- Double
- Float
- Int
- UInt
- Long
- ULong
- Short
- UShort
Finally, for saving data to file, any class that extends the Saveable class has a simple Save function. Otherwise, you will need to use SaveSystem.Save(save).
- Home
- WCSharp template
- Release notes
- Desyncs
- Upgrading to War3Net v5.x
- WCSharp.Api
- WCSharp.Buffs
- WCSharp.ConstantGenerator
- WCSharp.DateTime
- WCSharp.Dummies
- WCSharp.Effects
- WCSharp.Events
- WCSharp.JsonConvert
- WCSharp.Knockbacks
- WCSharp.Lightnings
- WCSharp.Missiles
- WCSharp.SaveLoad v1.x
- WCSharp.SaveLoad v2.x
- WCSharp.Shared
- WCSharp.Sync
- WCSharp.W3MMD