Skip to content

Writing Addons

Ronald Kinard edited this page May 5, 2017 · 1 revision

Here's an example addon file structure.

+ addons/my_addon_name/
|-+ init.lua
|-+ submodule.lua
|-+ anothersubmodule/
|---+ init.lua

When the addon plugin starts, it will find all the directories under addons and, sequentially, call Lua require on its name to make Lua cache the module. Then, it will check the return value of the module for a special key.

Lua modules

Here is an example of a conventional Lua module (init.lua or somethingelse.lua)

local function myfunc()
  return 'hello func'
end

return {
  myfunc = myfunc
}

Other modules can import the exported table you return from your script. For example, if your addon was named MyAddon,

local MyAddon = require('MyAddon')

local function somefunc()
  print(MyAddon.myfunc())
end

This would print "hello func" to the log.

You can require any other addon or library as you need them. Just make sure not to pollute the global namespace unless you absolutely have to. Always prefix new declarations with local and explicitly export them with the return value of your module.

In the file structure given above, addons/my_addon_name/submodule.lua would be accessible from other code with require('my_addon_name.submodule'), and addons/my_addon_name/anothersubmodule/init.lua from require('my_addon_name.anothersubmodule').

For Lua experts, package.path is set in addons/init.lua and defaults to ./addons/?/init.lua;./addons/?.lua, enabling the require behavior we want here.

Hooking and doing things on-screen

Here is where your module must export certain values.

local function present()
  imgui.Text('Hello world!')
end

local function init()
  return {
    name = "My Addon",
    version = "1.0",
    author = "Me",
    description = "A fun addon with a tooltip description in the addon browser!",
    present = present
  }
end

return {
  __addon = {
    init = init
  }
}

Your __addon.init exported function must return a table describing your addon, and all of the hooks it wants to use, in order for the plugin to register it. You can see how it works by looking at the psointernal addon code.

Clone this wiki locally