Skip to content

Using the framework as a dependency

Carnagion edited this page Feb 13, 2022 · 4 revisions

For addons to be able to use Addon Settings Framework's features, the framework must be installed first.

As long as the framework is installed, all other loaded addons have access to its features from anywhere.

Accessing the framework

The framework contains a number of functions, all located in the window.settings object. This value can be accessed from anywhere, and is set in the init function of the framework, which runs when addons are initialised.

The framework's functions can be called anytime after window.settings is initialised; however, there will be no effect if the options menu UI has not been created yet. For this reason, it is best to call the framework's functions inside a callback event to onOpenOptions or any other such event that is fired only after the options menu UI has been created.

The example shown below hooks up to the onOpenOptions event and adds a few settings once the framework has been initialised.

// Declare the addon and its functions
let addon =
    {
        // Initialise the addon
        init: function(events)
        {
            // Hook up to the onGetPlayer event
            events.on("onOpenOptions", this.onOpenOptions.bind(this));
        },
        onOpenOptions: function()
        {
            let settings = window.settings;
            // If window.settings is not initialised, the framework hasn't initialised yet, which could indicate a potential problem
            if (!settings)
            {
                return;
            }
            
            // Framework is initialised and its functions can be called as required
            settings.heading("Heading 1");
            settings.toggle("Toggle 1", ["Value 1", "Value 2", "Value 3"], "Heading 1");
            settings.switch("Switch 1", "Heading 1");
            settings.slider("Slider 1", 0, 100, 10, 50, "Heading 1");
        },
    };
// Register the addon
addons.register(addon);
Clone this wiki locally