Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ModOptions API #82

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

djhert
Copy link

@djhert djhert commented Jan 10, 2025

This is an API that I created to help facilitate new ModOptions in the new system.
While the new system is easy to use, it can be simpler!

Features:

  • Easy ModOptions creation using the new B42 native ModOptions
  • Automatic value population for easy access to the options you created, named by the ID.
  • Intended to be used as a module by your own mod to support good habits.
  • (Optional) Sandbox sync support

NOTE: Options are not guaranteed populated until event "OnInitGlobalModData", which is called when starting an actual game.


Usage:

If this is accepted, I will also update my B42 ModOptions example on the Workshop to include an example as well.

In a new file in your mod (we'll name it "MyConfig.lua"), add the following:

local config = require("ModOptionsAPI"):new("UNIQUEID", "NAME")

function config:Build()
   -- These are just examples
    self:addKeyBind("keybind", "KeyBind", Keyboard.KEY_Z)
   -- Either "self" or "config" can be used
    config:addTickBox("tickbox", "Check Box", false, "A basic tooltip")
end

function config:Apply()
    -- Optional, can remove if unneeded. An exposed function that is called when options are changed in game.
end

return config

The Build() function is required, and will be called by the ModOptionsAPI "OnGameBoot" to populate the available options set by your mod.

The Apply() function is optional, and can be removed from the file if it is not needed. However, you can use this to run things while you are in game to accommodate for the option changes.

In your mod, add the following:

local options = require("MyConfig")

Now you can directly access any of your options with simply:

options."ID"

Where "ID" is the ID that you specified when creating each option. Using the above example:

local key = options.keybind
local tick = options.tickbox

Multi-checkbox options are unique, in that the getValue function also requires the index. When parsing the options for these, I opted to append the index to the end of the ID you created.

Given:

function config:Build()
    local multibox = self:addMultipleTickBox("multibox", "Multibox")
    multibox:addTickBox("Option A", false)
    multibox:addTickBox("Option B", true)
end

These would be available as:

local options = require("MyConfig")
local optionA = options.multibox_1
local optionB = options.multibox_2

Sandbox

For Sandbox variable support, add the Sandbox namespace to end end of the ":new()" function:

local config = require("ModOptionsAPI"):new("UNIQUEID", "NAME", "SANDBOX")

Any Sandbox options are now available in our config table when in-game automatically.

For example, given:

option B42ModOptions.TestValue 
{
    type = integer, min = 0, max = 100, default = 3,
    page = B42ModOptions, translation = B42ModOptions_TestValue,
}

The namespace in the above is "B42ModOptions".

This will be available in game as:

local options = require("MyConfig")
local value = options.TestValue

Supports the OnSandboxOptionsChanged created by Change Sandbox Options (by Star), so a change in Sandbox Options in Single Player is propagated immediately. The Apply() function is called in these cases as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant