-
Notifications
You must be signed in to change notification settings - Fork 1
Saving & Loading
Saving with your mod's configuration can be done through SMAPI config files.
In this example, we will create a ModConfig class with simple properties which correspond to our settings. We assign default values for fresh configurations.
public class ModConfig {
public bool example = true;
}
In GameEvents.FirstUpdateTick
event handler:
Menu = Helper.ModRegistry.GetApi<IConfigMenu>("Juice805.StardewConfigMenu");
Package = new SimpleOptionsPackage(this);
Menu.AddOptionsPackage(Package);
var myConfig = new ModConfig();
var checkbox = new ConfigToggle("example", "Click Me!");
Package.AddOption(checkbox);
Next, when we want to save our options we simply extract from our menu components, assign them to the corresponding config property and save to json using SMAPI's Helper.WriteConfig
method.
var myToggle = myOptions.GetOption<ConfigToggle>("example")
myConfig.example = myToggle.IsOn;
// Helper is passed from SMAPI on mod load
Helper.WriteConfig<ModConfig>(myConfig);
The config file has now been created!
Loading from the config files is very simple. The following method uses the included SMAPI Helper
method ReadConfig
to load from the config JSON file, then use its properties as an intial value for the option components. This example uses the ModConfig
class from the Saving section
In GameEvents.FirstUpdateTick
event handler:
Menu = Helper.ModRegistry.GetApi<IConfigMenu>("Juice805.StardewConfigMenu");
Package = new SimpleOptionsPackage(this);
Menu.AddOptionsPackage(Package);
var config = this.Helper.ReadConfig<ModConfig>();
var checkbox = new ConfigToggle("example", "Click Me!", config.example);
Package.AddOption(checkbox);