Skip to content

namespace

CI edited this page Nov 14, 2024 · 16 revisions

In each example namespace refers to the 2nd value of the addon vararg, e.g:

local _, namespace = ...

namespace:IsRetail()

Checks if the current client is running the "retail" version.


namespace:IsClassicEra()

Checks if the current client is running the "classic era" version (e.g. vanilla).


namespace:IsClassic()

Checks if the current client is running the "classic" version.


namespace:HasBuild(buildNumber[, interfaceVersion])

Checks if the current client is running a build equal to or newer than the specified.
Optionally also check against the interface version.


namespace:Defer(callback[, ...])

Defers a function callback (with optional arguments) until after combat ends.
Callback can be the global name of a function.
Triggers immediately if player is not in combat.


namespace:DeferMethod(object, method[, ...])

Defers a method on object (with optional arguments) until after combat ends.
Triggers immediately if player is not in combat.


namespace.eventMixin

A multi-purpose event-mixin.

These methods are also available as methods directly on namespace, e.g:

namespace:RegisterEvent('BAG_UPDATE', function(self, ...)
    -- do something
end)

namespace.eventMixin:RegisterEvent(event, callback)

Registers a frame event with the callback function.
If the callback returns positive it will be unregistered.


namespace.eventMixin:UnregisterEvent(event, callback)

Unregisters a frame event from the callback function.


namespace.eventMixin:IsEventRegistered(event, callback)

Checks if the frame event is registered with the callback function.


namespace.eventMixin:TriggerEvent(event[, ...])

Manually trigger the event (with optional arguments) on all registered callbacks.
If the callback returns positive it will be unregistered.


namespace.eventMixin:RegisterUnitEvent(event, unit[, unitN,...], callback)

Registers a unit-specific frame event with the callback function.
If the callback returns positive it will be unregistered for that unit.


namespace.eventMixin:UnregisterUnitEvent(event, unit[, unitN,...], callback)

Unregisters a unit-specific frame event from the callback function.


namespace.eventMixin:IsUnitEventRegistered(event, unit[, unitN,...], callback)

Checks if the unit-specific frame event is registered with the callback function.


namespace.eventMixin:TriggerEvent(event, unit[, unitN,...][, ...])

Manually trigger the unit-specific event (with optional arguments) on all registered callbacks.
If the callback returns positive it will be unregistered.


namespace.eventMixin:RegisterCombatEvent(subEvent, callback)

Registers a combat subEvent with the callback function.
If the callback returns positive it will be unregistered.


namespace.eventMixin:UnregisterCombatEvent(subEvent, callback)

Unregisters a combat subEvent from the callback function.


namespace.eventMixin:TriggerCombatEvent(subEvent)

Manually trigger the combat subEvent on all registered callbacks.
If the callback returns positive it will be unregistered.

  • Note: this is pretty useless on it's own, and should only ever be triggered by the event system.

namespace:OnLoad()

Shorthand for the ADDON_LOADED for the addon.

Usage:

function namespace:OnLoad()
    -- I'm loaded!
end

namespace:OnLogin()

Shorthand for the PLAYER_LOGIN.

Usage:

function namespace:OnLogin()
    -- player has logged in!
end

namespace:event

Registers a to an anonymous function.

Usage:

function namespace:BAG_UPDATE(bagID)
    -- do something
end

namespace:event([...])

Manually trigger all registered anonymous event callbacks, with optional arguments.

Usage:

namespace:BAG_UPDATE(1) -- triggers the above example

namespace:RegisterSlash(command[, commandN,...], callback)

Registers chat slash command(s) with a callback function.

Usage:

namespace:RegisterSlash('/hello', '/hi', function(input)
    print('Hi')
end)

namespace:tsize(table)

Returns the number of entries in the table.
Works for associative tables as opposed to #table.


namespace:startswith(str, contents)

Checks if the first string starts with the 2nd string.


namespace:IsAddOnEnabled(addonName)

Checks whether the addon exists and is enabled.


namespace:HookAddOn(addonName, callback)

Registers a hook for when an addon with the name addonName loads with a callback function.


namespace.L(locale)[string]

Sets a localization string for the given locale.

Usage:

local L = namespace.L('deDE')
L['New string'] = 'Neue saite'

namespace.L[string]

Reads a localized string for the active locale.
If a localized string for the active locale is not available the string will be read back.

Usage:

print(namespace.L['New string']) --> "Neue saite" on german clients, "New string" on all others
print(namespace.L['Unknown']) --> "Unknown" on all clients since there are no localizations

namespace:ArgCheck(arg, argIndex, type[, type...])

Checks if the argument arg at position argIndex is of type(s).


namespace:ExtractFieldsFromUnitGUID(guid)

Returns the individual fields from the given guid, typecast to their correct types.


namespace:GetUnitID(unit)

Returns the integer id of the given unit.


namespace:GetItemLinkFromID(itemID)

Generates an item link from an itemID.
This is a crude generation and won't have valid data for complex items.


namespace:GetPlayerMapID()

Returns the ID of the current map the zone the player is located in.


namespace:GetPlayerPosition(mapID)

Returns the x and y coordinates for the player in the given mapID (if they are valid).


namespace:GetUnitAura(unit, spellID, filter)

Returns the aura by spellID on the unit, if it exists.

  • unitID
  • spellID - spell ID to check for
  • filter - aura filter, see UnitAura

namespace:CreateColor(r, g, b[, a])

Wrapper for CreateColor that can handle >1-255 range as well.
Alpha (a) will always be in the 0-1 range.


namespace:CreateColor(hex)

Wrapper for CreateColor that can handle hex colors (both RRGGBB and AARRGGBB).


namespace:FormatTime(timeInSeconds)

Formats the given timeInSeconds to a readable, but abbreviated format.


namespace:Hide(object[, child,...])

Forcefully hide an object, or its child.
It will recurse down to the last child if provided.


namespace:Print(...)

Prints out a message in the chat frame, prefixed with the addon name in color.


namespace:Printf(fmt, ...)

Wrapper for namespace:Print(...) and string.format.


namespace:Dump(object[, startKey])

Wrapper for DevTools_Dump.


namespace:DumpUI(object)

Similar to namespace:Dump(object); a wrapper for the graphical version.


namespace:RegisterSettings(savedvariables, settings)

Registers a set of settings with the interface options panel.
The values will be stored by the settings' objects' key in savedvariables.

Should be used with the options methods below.

Usage:

namespace:RegisterSettings('MyAddOnDB', {
    {
        key = 'myToggle',
        type = 'toggle',
        title = 'My Toggle',
        tooltip = 'Longer description of the toggle in a tooltip',
        default = false,
    },
    {
        key = 'mySlider',
        type = 'slider',
        title = 'My Slider',
        tooltip = 'Longer description of the slider in a tooltip',
        default = 0.5,
        minValue = 0.1,
        maxValue = 1.0,
        valueStep = 0.01,
        valueFormat = formatter, -- callback function or a string for string.format
    },
    {
        key = 'myMenu',
        type = 'menu',
        title = 'My Menu',
        tooltip = 'Longer description of the menu in a tooltip',
        default = 'key1',
        options = {
            {value = key1, label = 'First option'},
            {value = key2, label = 'Second option'},
            {value = key3, label = 'Third option'},
        },
    },
    {
        key = 'myColor',
        type = 'color',
        title = 'My Color',
        tooltip = 'Longer description of the color in a tooltip',
        default = 'ff00ff', -- either "RRGGBB" or "AARRGGBB" format
    }
})

namespace:RegisterSubSettings(name, settings)

Registers a set of settings as a sub-category. name must be unique.
The savedvariables will be stored under the main savedvariables in a table entry named after name.

The settings are identical to that of namespace:RegisterSettings.


namespace:RegisterSubSettingsCanvas(name, callback)

Registers a canvas sub-category. This does not handle savedvariables.

name must be unique, and callback is called with a canvas frame as payload.

Canvas frame has a custom method SetDefaultsHandler which takes a callback as arg1. This callback is triggered when the "Defaults" button is clicked.


namespace:RegisterSettingsSlash(...)

Wrapper for namespace:RegisterSlash(...), except the callback is provided and will open the settings panel for this addon.


namespace:GetOption(key)

Returns the value for the given option key.


namespace:SetOption(key, value)

Sets a new value to the given options key.


namespace:AreOptionsLoaded()

Checks to see if the savedvariables has been loaded in the game.


namespace:RegisterOptionCallback(key, callback)

Register a callback function with the option key.


namespace:TriggerOptionCallbacks(key, value)

Trigger all registered option callbacks for the given key, supplying the value.


namespace:RegisterMapSettings(savedvariable, settings)

Registers a set of settings to inject into the world map tracking menu. The values will be stored by the settings' objects' key in savedvariables.

The settings object is identical to the one for RegisterSetting.


namespace:CreateFrame(...)

A wrapper for CreateFrame, mixed in with namespace.eventMixin.


namespace:CreateButton(...)

A wrapper for namespace:CreateFrame(...), but will handle key direction preferences of the client.
Use this specifically to create clickable buttons.


namespace:CreateScrollList(parent)

Creates and returns a scroll box with scroll bar and a data provider in a list representation. It gets automatically sized to fill the space of the parent.

It provides the following methods, and is initialized whenever data is provided, so do that last.

  • list:SetInsets([top], [bottom], [left], [right]) - sets scroll box insets (all optional)
  • list:SetElementType(kind) - sets the element type or template (required)
  • list:SetElementHeight(height) - sets the element height (required)
  • list:SetElementSpacing(spacing) - sets the spacing between elements (optional)
  • list:SetElementSortingMethod(callback) - sets the sort method for element data (optional)
  • list:SetElementOnLoad(callback) - sets the OnLoad method for each element (optional)
    • the callback signature is (element)
  • list:SetElementOnUpdate(callback) - sets the callback for element data updates (optional)
    • the callback signature is (element, data)
  • list:SetElementOnScript(script, callback) - sets the script handler for an element (optional)
  • list:AddData(...)
  • list:AddDataByKeys(table)
  • list:RemoveData(...)
  • list:ResetData()

namespace:CreateScrollGrid(parent)

Creates and returns a scroll box with scroll bar and a data provider in a grid representation.
It gets automatically sized to fill the space of the parent.

It provides the following methods, and is initialized whenever data is provided, so do that last.

  • grid:SetInsets([top], [bottom], [left], [right]) - sets scroll box insets (all optional)
  • grid:SetElementType(kind) - sets the element type or template (required)
  • grid:SetElementHeight(height) - sets the element height (required)
  • grid:SetElementWidth(width) - sets the element width (required)
  • grid:SetElementSize(width[, height]) - sets the element width and height, shorthand for the two above, height falls back to width if not provided
  • grid:SetElementSpacing(horizontal[, vertical]) - sets the spacing between elements, vertical falls back to horizontal if not provided (optional)
  • grid:SetElementSortingMethod(callback) - sets the sort method for element data (optional)
  • grid:SetElementOnLoad(callback) - sets the OnLoad method for each element (optional)
    • the callback signature is (element)
  • grid:SetElementOnUpdate(callback) - sets the callback for element data updates (optional)
    • the callback signature is (element, data)
  • grid:SetElementOnScript(script, callback) - sets the script handler for an element (optional)
  • grid:AddData(...)
  • grid:AddDataByKeys(table)
  • grid:RemoveData(...)
  • grid:ResetData()
Clone this wiki locally