Skip to content

Examples

Nestorboy edited this page Dec 10, 2024 · 2 revisions

BepInEx ConfigEntry

Here's a simple example usage of EasySettings that uses BepInEx' ConfigEntry class:

using BepInEx;
using BepInEx.Configuration;
using Nessie.ATLYSS.EasySettings;
using UnityEngine;

namespace ExampleMod;

[BepInPlugin("ExampleMod", "Example Mod", "1.0.0")]
public class ExampleModPlugin : BaseUnityPlugin
{
    // We store the ConfigEntry's such that we can retrieve their values as they will be updated when the settings are.
    internal static ConfigEntry<float> ConfigFloat;
    internal static ConfigEntry<bool> ConfigBool;
    internal static ConfigEntry<KeyCode> ConfigKey;

    private void Awake()
    {
        InitConfig();

        // OnInitialized is invoked when all the UI template elements have been initialized.
        // If you try to push elements before OnInitialized you will get exceptions.
        // Currently it's important to subscribe to Settings.OnInitialized during Awake, as it will be invoked during Start.
        Settings.OnInitialized.AddListener(AddSettings);

        // OnApplySettings is invoked whenever the user presses the Apply button.
        // When the settings are applied, we need to make sure to write all the settings to the corresponding BepInEx config file.
        Settings.OnApplySettings.AddListener(() => { Config.Save(); });
    }

    private void InitConfig()
    {
        // ConfigEntry's can be fully initialized in Config.Bind, but I find it more concise to separate out the definitions and descriptions.
        var floatDefinition = new ConfigDefinition("Example Category", "ExampleFloat");
        // When working with floats or ints, you can define a range by using AcceptableValueRange.
        // Unless specified, the default range for sliders will be between 0 and 1.
        var floatDescription = new ConfigDescription("An example float. Limited between 0 and 5.", new AcceptableValueRange<float>(0f, 5f));
        ConfigFloat = Config.Bind(floatDefinition, 0.25f, floatDescription);

        var boolDefinition = new ConfigDefinition("Example Category", "ExampleBool");
        var boolDescription = new ConfigDescription("An example bool.");
        ConfigBool = Config.Bind(boolDefinition, true, boolDescription);

        var keyDefinition = new ConfigDefinition("Example Category", "ExampleKey");
        var keyDescription = new ConfigDescription("An example key.");
        ConfigKey = Config.Bind(keyDefinition, KeyCode.LeftControl, keyDescription);
    }

    private void AddSettings()
    {
        // In the future the Settings class will probably have ways to access the other tabs too.
        SettingsTab tab = Settings.ModTab;

        tab.AddHeader("Example Settings");

        // When adding elements, you can specify a label if you want a nicer name, otherwise it will use the key from the config.
        tab.AddAdvancedSlider("Float", ConfigFloat);

        tab.AddToggle("Toggle", ConfigBool);

        tab.AddKeyButton("Key", ConfigKey);
    }
}

Clone this wiki locally