ModOptions: guide for mod authors
Updated a day agoModOptions: guide for mod authors
ModOptions shows your mod's settings in GTFO's settings menu (one "Mod Options" tab shared by
every mod) or, if the player turned the integration on, in TheArchive's Mod Settings. You
describe the settings once, in code; ModOptions builds both menus from that, saves your .cfg
and applies edits made to it while the game runs.
Getting started
1. Reference it
ModOptions is compiled against, never shipped with your mod.
<PropertyGroup>
<ModOptions Condition="'$(ModOptions)' == ''">$(BepInEx)\plugins\Oxylight-ModOptions\ModOptions.dll</ModOptions>
</PropertyGroup>
<ItemGroup>
<Reference Include="$(ModOptions)" Private="false" />
</ItemGroup>
$(BepInEx) is your r2modman profile's BepInEx folder; r2modman installs ModOptions into plugins\Oxylight-ModOptions\. ModOptions.xml next to the DLL gives
you IntelliSense.
2. Declare the dependency
[BepInPlugin(GUID, NAME, VERSION)]
[BepInDependency("Oxylight.ModOptions", ">=1.0.0")] // the lowest version you need
public sealed class Plugin : BasePlugin
Keep the >=. BepInEx 6 reads the version as a range, and a bare "1.0.0" means exactly
1.0.0: your mod would refuse to load once players update ModOptions.
Thunderstore manifest.json:
"dependencies": [
"BepInEx-BepInExPack_GTFO-3.2.4",
"Oxylight-ModOptions-1.0.0"
]
3. Describe your settings in Load()
using ModOptions;
public override void Load()
{
var enabled = Config.Bind("General", "Enabled", true, "Turns the mod on or off.");
var volume = Config.Bind("General", "Volume", 0.5f,
new ConfigDescription("How loud it is.", new AcceptableValueRange<float>(0f, 1f)));
var distance = Config.Bind("General", "Distance", 15,
new ConfigDescription("How far it reaches.", new AcceptableValueRange<int>(0, 50)));
var mode = Config.Bind("General", "Mode", MyMode.Normal, "Which mode to use.");
var file = Config.Bind("Files", "DataFile", "", "Which file to load.");
ModPage page = ModOptionsApi.Register(GUID, "My Mod", "What my mod does.");
page.HotReloadSupported(true);
page.Author("Your Name"); // shown in the INFO block
page.Info.AddLink("Source code", "GitHub", "https://github.com/you/my-mod");
page.AddToggle(enabled, "Enabled");
page.AddSlider(volume, "Volume", SliderDisplay.Percent);
page.AddSlider(distance, "Distance").Step(5).Unit(" m");
page.AddDropdown(mode, "Mode", m => m == MyMode.Normal ? "Normal" : "Fast");
SettingsPage advanced = page.AddSubPage("Advanced", "Files and troubleshooting.");
advanced.AddHeader("Files");
FilePickerRow dataFile = advanced.AddFilePicker(file, "Data file", baseFolder: Path.Combine(Paths.ConfigPath, "MyMod"))
.Filter("Data files", "*.json;*.txt")
.Filter("All files", "*.*");
advanced.AddButton("Cache", "Clear now", ClearCache);
// The stored value may be relative to the base folder; FullPath resolves it (null when empty).
file.SettingChanged += (_, _) => LoadData(dataFile.FullPath);
}
Register in Load(). ModOptions builds the menus after all plugins have loaded, so rows
added later only show in the vanilla tab, not in TheArchive (a warning is logged).
API
Everything is in the ModOptions namespace.
ModOptionsApi
| Member | What it does |
|---|---|
ModPage Register(string guid, string title, string? description = null) |
Adds your mod to the list and returns its main page. One registration per GUID; throws ArgumentException on a duplicate or empty GUID/title. |
string Version |
The running ModOptions version. |
const string Guid |
ModOptions' BepInEx GUID ("Oxylight.ModOptions"), e.g. for [BepInDependency(ModOptionsApi.Guid, ">=1.0.0")]. |
SettingsPage (and ModPage, the main page)
Every row edits a BepInEx ConfigEntry<T>. label is what players see. description is the
tooltip and defaults to the entry's own ConfigDescription.
Every method returns the row it added, as its own type, so per-row options can be chained. All
row types derive from SettingRow.
| Method | Returns | Row |
|---|---|---|
AddToggle(ConfigEntry<bool>, label, description?) |
ToggleRow |
On/Off button. |
AddDropdown<TEnum>(ConfigEntry<TEnum>, label, Func<TEnum,string>? format, description?) |
DropdownRow |
Lists every enum value. format sets the texts (the value names when omitted). |
AddSlider(ConfigEntry<float>, label, SliderDisplay display = Percent, description?) |
SliderRow |
Float slider. Requires an AcceptableValueRange<float>. |
AddSlider(ConfigEntry<int>, label, SliderDisplay display = Integer, description?) |
SliderRow |
Int slider; snaps to whole numbers, or to Step(n). Requires an AcceptableValueRange<int>. |
AddText(ConfigEntry<string>, label, string? emptyText, description?) |
TextRow |
Text field; Enter or leaving the field saves it. emptyText shows while it's empty. |
AddFilePicker(ConfigEntry<string>, label, string? baseFolder, description?) |
FilePickerRow |
Path field plus a "Browse..." row that opens the Windows file dialog. Files inside baseFolder are stored as relative paths with forward slashes (sub/image.png), other files as full paths. Read the path from the row's FullPath, which resolves both. |
AddButton(label, buttonText, Action onClick, description?) |
ButtonRow |
Runs onClick on the main thread. |
AddHeader(text) |
HeaderRow |
Section title. |
AddLabel(text, description?) |
LabelRow |
Read-only text; rich text tags work. Long text wraps and shrinks to fit; if it still does not fit, the row grows. |
AddLink(label, buttonText, url, description?) |
ButtonRow |
A button that opens an http(s):// address in the player's browser. The tooltip is the address unless you give one. |
AddSubPage(title, description?) |
SettingsPage |
A page below this one. The "Open" and "Back" rows are added for you. |
Read-only properties: SettingsPage.Title and SettingsPage.Description (what you passed to
Register or AddSubPage), ModPage.Guid, and SettingRow.Label.
SliderDisplay:
Percent: a float 0..1 shows as 0–100%; an int shows its value with "%".Integer,OneDecimal,TwoDecimals.
What gets stored:
- A float slider without a step is rounded to what
displayshows (two decimals forPercent), so the number on screen is the number in the file. - With
Step, the value snaps to the minimum plus a whole number of steps, and the file holds that step value (to six decimals), whatever the text shows. Int sliders always snap (step 1 by default). - The maximum can always be chosen, also when the range isn't a whole number of steps.
Mod-wide options (ModPage)
| Method | Effect |
|---|---|
HotReloadSupported(bool) |
Say whether your mod applies changes while the game runs. See "Restart markers" below. |
ManageConfigFileYourself() |
ModOptions won't save or watch your config file. Use it only if your mod already does that itself. |
ConfigSectionsInMenuOrder() |
Writes your .cfg's sections in the order they first appear on your pages instead of BepInEx's alphabetical order. Sections no page uses come after; settings inside a section keep their binding order. Off by default. |
ShowInfo(bool) |
Whether your main page ends with an INFO block: "Title v1.2.3 by Author". The version comes from your [BepInPlugin] (found by the GUID you registered with), the author from Author. On by default; in TheArchive it sits at the bottom of your Add-ons entry. |
Author(string) |
The name after "by" in the INFO block. Left out until you set it. |
Info |
The INFO block itself, for your own rows (see below). |
The INFO block (page.Info)
It always comes last on your main page: the "INFO" header, the "Title v1.2.3 by Author" line, then
the rows you add, in the order you add them. Every method returns the row (LabelRow,
ButtonRow), like the page methods.
| Member | Effect |
|---|---|
AddLabel(text, description?) |
A line of text. |
AddLink(label, buttonText, url, description?) |
A button that opens a web page. |
AddButton(label, buttonText, Action onClick, description?) |
A button that runs your code. |
ShowVersionLine(bool) |
Drops the generated version line when false. With no line and no rows, there's no block. |
page.Author("Oxylight");
page.Info.AddLabel("Thanks to food for the original menu.");
page.Info.AddLink("Source code", "GitHub", "https://github.com/you/my-mod");
Per-row options (on the rows the Add* methods return)
| Method | Effect |
|---|---|
RestartRequired() / RestartNotRequired() |
Override the mod-wide restart setting for this row. On every row type; they only show on rows that edit a config entry. |
SliderRow.Unit(string) |
Text after the number, e.g. " m". Not used while a ValueText is set. |
SliderRow.Step(float) |
Snap to the minimum plus a whole number of steps, e.g. Step(1f / 30); the maximum stays reachable. Must be above 0; on an int slider a whole number of at least 1 (default 1). Throws ArgumentException otherwise. |
SliderRow.ValueText(Func<float,string>) |
Your own text for the value, in both menus. It replaces the SliderDisplay text and the Unit, so put any unit in your text. It gets the stored value (whole numbers on an int slider), on the main thread, each time the number is redrawn. A value edited into the .cfg by hand, or saved before you added a Step, isn't snapped until the slider is moved, so don't assume it sits on a step. If it throws or returns null, the default text shows and an exception is logged once. null restores the default text. |
FilePickerRow.Filter(name, patterns) |
Adds a file type, e.g. Filter("Images", "*.png;*.jpg"). Filters show in the order added and the first one is preselected; with no filter the dialog shows all files. |
FilePickerRow.DialogTitle(string), FilePickerRow.StartFolder(string) |
Dialog title, and where it opens while the field is empty. |
FilePickerRow.OnSameFilePicked(Action) |
Runs when the player picks the file that is already set (no SettingChanged is raised then), e.g. to re-read it. |
FilePickerRow.FullPath |
Full path of the file the setting points to now, or null when it's empty or not a valid path. A relative value resolves against baseFolder (without one, against the game's working folder). Current inside SettingChanged too; any thread; doesn't check that the file exists. |
A volume on a decibel scale, with 31 stops from 0 dB down to muted:
page.AddSlider(volume, "Volume")
.Step(1f / 30)
.ValueText(v =>
{
int step = (int)Math.Round((1f - v) * 30f);
return step >= 30 ? "Muted" : $"{-2 * step} dB";
});
MainThread.Post(Action)
Runs an action on Unity's main thread during the next frame. Safe from any thread. An action may post again (itself included); that runs one frame later, so "try again next frame" works.
Restart markers
ModOptions can't know whether your mod reacts to a changed value while the game runs. So by
default every row is marked with a red [!] and the tooltip "Takes effect after restart",
the same marker TheArchive uses.
- If your mod reads
entry.Valuewhen it needs it, or listens toSettingChanged, callpage.HotReloadSupported(true)and the markers go away. - Mark the exceptions per row with
.RestartRequired()or.RestartNotRequired().
How config files are handled
For every config file your rows use, ModOptions:
- saves it shortly after changes stop instead of on every change, so a dragged slider doesn't
write the file every frame. It also saves pending changes on quit. It does this by setting
SaveOnConfigSet = falseon yourConfigFile; values your own code sets are saved the same way. - watches it for edits made outside the game. A changed file is read and every changed value
is set on its entry, so BepInEx raises
SettingChangedas usual. Its own saves never trigger a reload, and a file is never re-read while a menu change is still waiting to be saved.
If your mod already has its own file watcher, remove it. Two watchers on one file fight over
it. If you need to keep yours, call ManageConfigFileYourself().
TheArchive mode, in detail
- Every mod gets its own entry under Add-ons, named after the mod, the same as a mod that integrates with TheArchive itself. Its main page's rows are on the entry's page. ModOptions' own entry ("Mod Options") has the Info block at the bottom, as in the vanilla tab.
- Pages become TheArchive settings: sub-pages are dynamic submenus, headers are
FSHeaders, and the file picker becomes a text field plus a "Choose file" button. - Your
.cfgstays the source of truth. TheArchive's own saved copy is overwritten from the.cfgat startup. - TheArchive shows enum value names; custom dropdown texts only appear in the vanilla tab.
Slider units are added to the label, e.g. "Distance (m)", unless the slider has a
ValueText. - Switching the integration on or off needs a restart, because TheArchive can't remove settings once they're added.
Optional dependency
To use ModOptions only when it's installed, declare a soft dependency and keep every ModOptions call in a separate class that's only touched when it's present:
[BepInDependency("Oxylight.ModOptions", BepInDependency.DependencyFlags.SoftDependency)]
public sealed class Plugin : BasePlugin
{
public override void Load()
{
// ... binds ...
if (IL2CPPChainloader.Instance.Plugins.ContainsKey("Oxylight.ModOptions"))
SettingsMenu.Register(this); // the only class that mentions ModOptions types
}
}
Compatibility promise
- Public signatures are never changed or removed, and existing methods never get new parameters
(optional parameters are compiled into your DLL, so adding one would break it). New features
come as new methods, mostly on the row and page handles. That is why every
Add*returns its own row type. - The API is what this guide lists.
ModOptions.Integration.ArchiveFeatureBaseis public only because generated code derives from it; don't use it. - Versions follow semver. Declare the lowest version you need in
[BepInDependency].