Skip to content
Gabe616 edited this page Jan 28, 2023 · 20 revisions

Addons

OC+ provides an API for users to create custom addons.

This page documents how to use such API and example usecases.

Examples

local RS = game:GetService("RunService")

return {
    id = "coolexample",
    title = "Cool Example Addon",
    description = "A very cool addon which does absolutely nothing.",
    version = 1.0,
    author = 304346361,
    github = "https://raw.githubusercontent.com/Gabe616/OCPlus/main/addons/example.lua",
    icon = "11176073563",
    settings = {
        {
            name = "Be Cool",
            id = "cool",
            type = "toggle",
            description = "Makes the addon 200% cooler."
        },
        {
            name = "Do Cool Thing",
            id = "coolkb",
            type = "keybind",
            description = "The key to press to do cool thing."
        },
        {
            name = "Cool Type",
            id = "coolsw",
            type = "choose",
            description = "Type of coolness",
            choose = {"Meh", "Very Cool"}
        },
    },
    data = {
        cool = false,
        coolkb = "Return", -- Keybinds are saved as strings so they can be saved to a JSON file
        coolsw = "Meh"
    },
    run = {
        start = function(Util)
            Util.log("Enabled!")
            Util.run.update(Util)
        end,
        input = function(Util, input)
            if input.KeyCode.Name == Util.data.coolkb then
                Util.log("Pressed the swag key!")
            end
        end,
        update = function(Util, key)
            if key then -- Called by addon manager
                Util.log(("%s has been changed to %s!"):format(key, tostring(Util.data[key])))
            end

            Util.removeEvent("coolness") -- Unregisters the coolness event
            if Util.data.cool then
                -- Registers coolness event again only if cool is set to true
                Util.addEvent("coolness", RS.RenderStepped:Connect(function()
                    Util.log(("Coolness currently set to: %s"):format(Util.data.coolsw))
                    -- This is just an example, do NOT do this! It will clog up the console
                end))
            end
        end,
        stop = function(Util)
            Util.log("Shutting down!")
            -- RenderStepped is automatically unregistered
        end
    },
}

For more addons, see the addons directory

Creating an Addon

To create an Addon, use the boilerplate code below:

local RS = game:GetService("RunService")

return {
    title = "Hello World!",
    description = "An example addon.",
    version = 1.0,
    author = 304346361
}

When your Addon gets accepted, a github parameter will be automatically added for updating, just like so:

-- Everything must be in the return {...}
github = "https://raw.githubusercontent.com/Gabe616/OCPlus/main/addons/helloworld.lua"

To add settings, add the settings table to the return function:

-- Everything must be in the return {...}
settings = {
    {
         name = "Toggle Example",
         id = "toggle",
         type = "toggle",
         description = "An example of a toggle!"
    },
    {
         name = "Number Example",
         id = "number",
         type = "number",
         description = "An example of a number value!"
    },
    {
         name = "Keybind Example",
         id = "key",
         type = "keybind",
         description = "An example of a keybind!"
    },
}

To use those settings, add the data table.

-- Everything must be in the return {...}
data = { --  This is required in order to use these settings in your script
    toggle = false,
    key = "Return", -- Keybinds are saved as strings so they can be saved to a JSON file
    number = 118118
},

Finally, the run table contains the addon code.

-- Everything must be in the return {...}
run = {
    start = function(Util)
        Util.log("Enabled!")
        Util.run.update(Util)
    end,
    input = function(Util, input)
        if input.KeyCode.Name == Util.data.coolkb then
            Util.log("Pressed the swag key!")
        end
    end
    update = function(Util, key)
        if key then -- Called by addon manager
            Util.log(("%s has been changed to %s!"):format(key, Util.data[key]))
        end

        Util.removeEvent("coolness") -- Unregisters the coolness event
        if Util.data.cool then
            -- Registers coolness event again only if cool is set to true
            Util.addEvent("coolness", RS.RenderStepped:Connect(function()
                Util.log(("Coolness currently set to: %s"):format(Util.data.coolsw))
                -- This is just an example, do NOT do this! It will clog up the console
            end))
        end
    end,
    stop = function(Util)
        Util.log("Shutting down!")
        -- RenderStepped is automatically unregistered
    end
}

Addon Documentation

Addon :: object

The Addon object.

Addon.id :: string

The Addon's custom ID.

Addon.title :: string

The Addon's visible title

Addon.description :: string

The Addon's description

Addon.version :: float

The Addon's version

Addon.author :: int

The Addon's author user ID

Addon.github :: string

Link to a raw github URL to your Addon

Addon.icon :: ?string

(optional) ID of an image used as the icon of the Addon

Addon.settings :: Setting[]

Array of Setting objects


Setting :: object

The Setting object

Setting.name :: string

Visual name of the setting

Setting.id :: string

Hidden ID of the setting

Setting.type :: string

The type of the setting Can be:

  • toggle — checkbox
  • choose — dropdown
  • string — text
  • number — number
  • keybind — keybind

Setting.minVal :: ?number

(optional) If Setting.type is set to number, then it's the minimum value; otherwise it's the minimum length for string Defaults to 0 for string and -999999999 for number.

Setting.maxVal :: ?number

(optional) If Setting.type is set to number, then it's the maximum value; otherwise it's the maximum length for string Defaults to 999999999 for string and 999999999 for number.

Setting.description :: string

The description of the setting

Setting.choose? :: ?string

(optional) Available options for the setting.
(Only used for the setting type choose)


Addon.data :: object

An object of the Addon's save data (must be JSON friendly!)

Addon.run :: Runner

Dictionary containing the Addon's runner functions


Runner :: object

The Runner object. All runner functions are surrounded by pcall() and errors logged with Util.error.

Runner.start :: function

The function that gets ran when the addon is enabled. This function is not required, but will throw a warning if not provided.
Passed arguments:

  • Util — The Addon's Util object

Runner.update :: ?function

(optional) The function that gets ran when a setting is changed.
Passed arguments:

  • Util — The Addon's Util object
  • key — The Setting's ID that was changed.

Runner.input :: ?function

(optional) The function that gets ran when a key is pressed.
Passed arguments:

Runner.stop :: function

The function that gets ran when the addon is disabled or the script is reloaded. This function is not required, but will throw a warning if not provided.
Passed arguments:

  • Util — The Addon's Util object

Util :: object

The Util object

Util.data :: object

The Addon.data object

Util.run :: Runner

The Addon.run object

Util.canInteract :: boolean

Whether the player can enable/disable the addon or change a setting.

Util.disable :: function()

Disables the addon. Automatically runs Util.removeAllEvents and Runner.stop

Util.log :: function(message :: any, source :: string)

Logs a message to the console with the addon's name prepended.

Util.warn :: function(message :: any, source :: string)

Warns a message to the console with the addon's name prepended.

Util.error :: function(message :: any, source :: string)

Errors a message to the console with the addon's name prepended.

Util.addEvent :: function(id :: string, event :: RBXScriptConnection)

Used for safely registering addon-only events

Util.removeEvent :: function(id :: string) => RBXScriptConnection|nil

Removes an addon-only event and returns it

Util.removeAllEvents :: function() => nil

Removes all of the addon-only events.

Testing Addons

If you want to test your Addon, simply navigate to your executor's workspace folder. There, you will find a folder called ocplus. If there isn't such folder, execute the OC+ script and look again. Then, navigate to ocplus/addons and create a file called YOUR_ADDON.lua and paste your addon into it.

Help Out

Do you feel like something is missing, or just want to help out? Create an issue/pull request!