CUCoreLib

Misc / API

Settings API

Register native settings rows in the vanilla options menu with CUCoreLib!

Adding a setting

ModOptionsRegistry.Register adds a row to the game’s own options menu.

public static bool SparksEnabled;

private void Awake()
{
    ModOptionsRegistry.Register(ModOptionDefinition.Bool("mymod.sparks", true)
        .WithCategory(Setting.SettingCategory.Video)
        .OnApply(value => SparksEnabled = value));
}

The .OnApply(...) callback is called with the saved value when settings load, and again whenever the player changes it. Store it in a field and read that field from your code.

The value you pass is the default, used for new players and by “Reset to default”.

Row types

BuilderRowExtra arguments
ModOptionDefinition.BoolToggle
ModOptionDefinition.FloatSlidermin, max, and optionally a formatter for the shown value
ModOptionDefinition.IntNumber inputmin, max
ModOptionDefinition.DropdownDropdownThe choices. The value is the chosen index
ModOptionDefinition.KeybindKey buttonThe value is a KeyCode
ModOptionDefinition.TextText inputOptionally placeholder text
ModOptionsRegistry.Register(ModOptionDefinition.Float(
    "mymod.clinkvolume", 0.8f, 0f, 1f,
    formatValue: value => Mathf.RoundToInt(value * 100f) + "%")
    .WithCategory(Setting.SettingCategory.Audio)
    .OnApply(value => ClinkVolume = value));
In-game screenshot of the clink volume slider in the options menu.

Your own tab

Create a custom category by ModOptionCategory.Localized(translationKey) instead of a vanilla category to give your mod its own tab on the right of the settings menu. Every setting with the same translation key shares the tab, and long tabs scroll.

var tab = ModOptionCategory.Localized("mymod.settings.category");
ModOptionsRegistry.Register(ModOptionDefinition.Bool("mymod.sparks", true)
    .WithCategory(tab)
    .OnApply(value => SparksEnabled = value));

“Reset to default” on that tab only resets that tab.

Local-only settings (multiplayer)

In multiplayer, the host’s settings are sent to every client and applied on top of theirs. That’s fine for gameplay rules, but not as great for things like graphics, audio or keybinds.

  • Add .LocalOnly() to keep a setting on each player’s own machine:
ModOptionsRegistry.Register(ModOptionDefinition.Bool("mymod.sparks", true)
    .WithCategory(Setting.SettingCategory.Video)
    .LocalOnly() // the host never overrides this one
    .OnApply(value => SparksEnabled = value));

Keybind settings are always local.

Keybinds without a settings row

For a single key action, GetFriendlyKeyBind gives you a key you can choose to ignore while typing, in menus, and so on. AllowKeybindRebind then adds a row so players can change it:

private void Awake()
{
    var reload = CUCoreUtils.GetFriendlyKeyBind("R"); // default key R
    reload.disableInInputFields = true;  // don't fire while typing in a text box
    reload.disableInInventory = true;    // or while the inventory is open
    CUCoreUtils.AllowKeybindRebind("R", "Reload gun"); // rebindable in Input settings
}

private void Update()
{
    // IsPressed() respects the disable flags set above
    if (CUCoreUtils.GetFriendlyKeyBind("R").IsPressed())
    {
        ReloadGun();
    }
}

The flags are all off by default: disableInInputFields, disableInMainMenu, disableInHealthPanel, disableInInventory.

CUCoreUtils.GetFriendlyKeyName("R") gives the currently bound key’s name, for UI elements like “Press R to reload”.

Syncing with a BepInEx config entry

If your mod also has a BepInEx .cfg entry for the same setting, name the config key after the end of the setting ID. CUCoreLib then copies the in-game value into the config file whenever it changes:

ConfigEntry<bool> sparksConfig = Config.Bind("General", "sparks", true, "Show spark effects around kilns.");

ModOptionsRegistry.Register(ModOptionDefinition.Bool("mymod.sparks", sparksConfig.Value)
    .WithCategory(Setting.SettingCategory.Video)
    .OnApply(value => SparksEnabled = value));

The in-game setting takes precedence in the case of a conflict.

  • Works for bool, int, float, dropdown, keybind rows and text.

World settings (per run)

WorldSettingsRegistry uses its own factories to add rows to the run settings panel shown before a new run. The values are run-specific, meaning they save and load with it, (and are also included in the Copy/Paste preset code!)

public static float KilnHeat;

WorldSettingsRegistry.Register(WorldSettingDefinition.Float(
    "mymod.kilnheat",                  // unique ID
    "Kiln heat",                       // name
    "How hot kilns burn this run.",    // Ddscription
    "Glassworks",                      // heading category. You can make your own section, or choose an existing a vanilla one
    1f, 0.5f, 2f,                      // default, min, max (extra args)
    value => KilnHeat = value,         // runs on new run, load, and whenever it changes
    value => value.ToString("0.#") + "x"));

Pass a WorldSettingCategory (RunWide, Layer, Player, Trader, Other, Challenges) to append the row to a vanilla section instead. Custom categories go at the top of the panel, unless you place them next to a vanilla one:

WorldSettingsRegistry.PlaceCategory("Glassworks", WorldSettingCategory.Layer, below: true);
BuilderRowExtra arguments
WorldSettingDefinition.BoolToggle
WorldSettingDefinition.FloatSlidermin, max, and optionally a formatter for the shown value
WorldSettingDefinition.IntNumber inputmin, max, optionally placeholder text
WorldSettingDefinition.DropdownDropdownThe choices. The value is the chosen index
WorldSettingDefinition.TextText inputOptionally placeholder text
WorldSettingDefinition.ButtonButtonAn onClick action and optional button text. Not saved

You can also read a value directly with WorldSettingsRegistry.Get<float>("mymod.kilnheat").

  • Clearing an input resets it to its default.

Per-preset defaults

Vanilla presets (Relaxed, Paradise, Desolate…) can change the run settings.

  • WithPresetDefault does the same for your setting!
WorldSettingsRegistry.Register(WorldSettingDefinition.Float(
    "mymod.kilnheat", "Kiln heat", "How hot kilns burn this run.",
    "Glassworks", 1f, 0.5f, 2f,
    value => KilnHeat = value)
    .WithPresetDefault(WorldSettingPreset.Paradise, 0.5f)
    .WithPresetDefault(WorldSettingPreset.Desolate, 2f));

Rules

  • IDs must contain a namespace dot (mymod.something) and be unique.
  • min must not be above max.
  • Dropdowns need at least one choice, unique choice keys, and a default index that exists.
  • Put mod option labels and descriptions in the language file. Older overloads that take literal text are deprecated and still supported.