User:Cogswobble/Addon Tutorial/Structure of an Addon

From Warcraft Wiki
Jump to navigation Jump to search

Before you create an addon, you need to understand the basic structure of addons, and how WoW finds and loads them. Below is description of this structure. More detail will be given when actually constructing an addon.

Basic Structure

All files for an addon are stored in a directory with the name of the addon. This directory can contain multiple levels of subdirectories containing more files. Most addons are composed of three basic file types: TOC, XML, and Lua. Other files may be included as well (e.g. custom graphic files).

TOC

Every addon has one (and only one) TOC file. The TOC is the Table of Contents for the addon. It contains some basic information about the addon, as well as a manifest of the XML and Lua files used by the addon. TOC files are named as AddonName.toc and must appear in the top level directory for the addon.

Example of a TOC file:

## Interface: 30000
## Title : MyAddOn
## Notes: This AddOn does nothing but display a frame with a button
## Author: My Name
## eMail: [email protected]
## URL: http://www.wowwiki.com/
## Version: 1.0
## Dependencies: Sea
## OptionalDeps: Chronos
## DefaultState: enabled
## SavedVariables: settingName, otherSettingName
MyScript.lua
MyAddOn.xml
MyFrame.xml
MyButton.xml

For more information, see TOC format

Lua

Lua is a programing language which is used to instruct an addon how to behave. Most addons will have at least one Lua file. UNKNOWN: Can an addon with only XML files be used to modify the look of standard UI components/widgets?

Lua files end with the .lua extension and can appear anywhere within the directory or subdirectories of an addon.

Example of a Lua file:

function MyAddon_OnLoad()
	SlashCmdList["MyAddon"] = MyAddon_SlashCommand;
	SLASH_MYADDON1= "/myaddon";
	this:RegisterEvent("VARIABLES_LOADED")
end

For more information, see Lua.

XML

XML is a language for describing data. Different pieces of data can be "tagged" with labels (e.g. <name>Cogswobble</name>). Blizzard uses XML to describe graphical elements of it's interface (some times refered to as widgets). XML files end with the .xml extension and can appear anywhere within the directory or subdirectories of an addon.

One of the elements the XML can contain is a reference to a Lua script for handling events from a widget.

Example of an XML file:

<Ui xmlns="http://www.blizzard.com/wow/ui/"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.blizzard.com/wow/ui/ ..\FrameXML\UI.xsd">
  <Frame name="MyAddon_Frame">
    <Anchors>
      <Anchor point="CENTER"/>
    </Anchors>
    <Frames>
      <Button name="MyAddon_Button">
        <Anchors>
          <Anchor point="CENTER"/>
        </Anchors>
      </Button>
    </Frames>
  </Frame>
</Ui>

For more information, see XML user interface

Loading Addons

Addons are installed by copying their directory to <WoW installation directory>\Interface\AddOns. When WoW is started, it checks this directory for addons. It reads their TOC files and uses this information to bring the addon into memory (assuming that the addon is configured as load on start up, which is the default). The information in the top part of the TOC is used to verify that the UI version is up to date and to communicate with the user (title and notes).