User:Reshinji/subpage

Revision proposal

Square One
If you have never had experience with programming, scripting, or web-development it is important to understand that not all editors are practical for the process of coding. Document preparation sofware, for example Microsoft Word, can work, but not without excessive care and unecessary complication. A strong list of plain-text editors is available at. Trying them all will help you figure out what work flow and interface works best for you. The tool you prefer to edit your code with may not be the same you choose to edit your interface configurations.

Presumably your interest in addon development follows from an idea. You may be interested in contributing to an existing addon or writing your own from scratch. Either way, it is essential to understand the fundamental structure of addons and how they interact with the World of Warcraft client. All addons are located in the Interface\Addons subdirectory of your World of Warcraft installation folder. Each individual addon is contained in its own folder. In each addon folder there is a *.toc (table of contents) file and a variable number of additional files and folders which make up the content of the addon.

To start a standard "Hello World" example: These denote, in order, Table of Contents, the Lua code file, and the XML user interface visual elements file. The name of your AddOn directory and the name on the .toc file must match.
 * Create a directory for your AddOn named "HelloWorld"
 * Create within three files: HelloWorld.toc, HelloWorld.lua, HelloWorld.xml

The .toc or Table of Contents
This file tells WoW about your AddOn: what files to load and what order to load them in. Later you will want to peruse the TOC format page for all of the gory details about what you could put into this file. For now we are just going to give you some basic stuff to include.

Using your trusted text file editor, place the following into the HelloWorld.toc file: HelloWorld.lua HelloWorld.xml
 * 1) Interface: 30300
 * 2) Title: Hello World!
 * 3) Notes: My first AddOn

The first line indicates the version of the WoW client you, the addon author, intend to support. This ensures addons stay current or, at minimum, warns the user when a new patch may have introduced changes that break your addon. The current interface version can be found by inspecting the .toc files of other up-to-date addons, by using the (interface extraction) tool provided by Blizzard, or by visiting (wowcompares.com) to review the current interface files and its version number. By default, if your addon is allowed to go out of date, it will be disabled from future use until loading is forced by the user.

Starting from the first line:
 * 1) The compatible UI version for this addon is 30300 (or 3.3.0)
 * 2) The title of the addon to be displayed in game is "Hello World!"
 * 3) The description of the addon to be displayed in game are the notes
 * 4) HelloWorld.lua file will be loaded.
 * 5) HelloWorld.xml file will be loaded.

For more details, please visit the TOC format page.

The .lua or Lua code file
The .lua files store the functions that describe your addon behavior. These functions are provided to the WoW client as tools to process events that occur in game. For example: if you want an addon that automatically sells all of the grey items in your inventory, your addon would describe to the WoW client that it is interested in listening to the vendor interaction events and would also provide a function that scans the character's bags for items to use.

Events
There are two basic event types. The first kind of event is when something happens in the game. This might be somebody saying something, something happening to your character, another character's stats changing (nearly everything that happens in the game causes an event). The second kind of event is when you do something to a UI Element (a UI Element is something on the screen and is affectionately called a widget). This second kind of event might be clicking on something in your bags or button bar. There is a technical difference between the two types of events, and we will discuss that as the tutorial progresses. For more detail on the first kind of event see Events (API) and for the second see Widget handlers.

So, how do you tell World of Warcraft that you are interested in a particular event? There are two ways: First, you can tell World of Warcraft which code to run when a particular event occurs. This is called registering your event. Second, you can tell the XML to run a piece of code when a UI Element is manipulated (such as clicking on it or moving your mouse over it).

Functions
Functions are groupings of code that accomplish a specific purpose. There are numerous pre-defined functions provided by World of Warcraft called API functions and you can make your own user-defined functions. While there are multiple ways to create functions in Lua the easiest to understand looks like this:

function function_name ... code ... end

The is an optional keyword that limits the visibility of the function to a particular scope. Scope will be covered in more depths shortly.

The function_name is simply a name you make up so you can reference your function from other parts of your AddOn.

The are ways to pass information into the function. This is what gives functions their power.

Each time you call the function, you can supply a different set of arguments and get different results based upon them.

The ... code ... is where the work gets done in a function. Here is where you do calculations, comparisons, call other functions, etc to get the task of the function done.

The end simply marks the end of the definition of the function.

Note that this only defines or declares the function. The function is not actually run until some other piece of code references (or calls) it.

For more information on functions, please see the Lua 5.1 Reference Manual or Programming in Lua (first edition). Also see the Lua page which lists more Lua resources.

HelloWorld.lua
Now to continue with our Hello World code example. Please include the following into your HelloWorld.lua file and save it.

function HelloWorld print("Hello World!"); end

You should understand everything in here by now. This function is named HelloWorld and it has zero arguments. The code part is simply the 'print("Hello World!");' portion. And it ends with end.

This is a fine piece of code, and by itself is useless unless something calls the function. Onward to UI Elements (aka Widgets).

The .xml or XML visual elements file
UI Elements, or Widgets, are all of the tiny bits of graphics that make up the User Interface. World of Warcraft uses XML to layout everything that you see on the screen. Additionally, when things happen (called Events, remember?) to the widgets on the screen, Widget Handlers can be called to perform whatever action you want. We will see shortly how we tell WoW which widgets we are interested in and which Events we want handled by which Widget Handler.

Blizzard XML format declared
For those of you who don't know, XML stands for eXtensible Markup Language and is a means of tagging content with identifiers. What the identifiers are and how they are organized can be defined in something called a Schema. In our case, we want to create XML documents that WoW will understand, so we will use the Schema provided by Blizzard for the Wow User Interface.

We declare that our document conforms to the Blizzard schema with the following bit of magic:

 

The exact meaning of all of the above is beyond the scope of this tutorial. Consider it a magic formula that you always put in every .xml file you will create for the WoW user interface. For those of you that like to reformat things, the first three lines can be merged to line line (use spaces), but the fourth line (..\..\FrameXML\UI.xsd"> ) needs to be on a line by itself starting in column 1.

There are a few general notes that you need to know about concerning XML, particularly as it is used by WoW. The generalized format of a tag is:



A tag must have a tagname, and it may have zero or more attributes along with the attributes' associated values in double quotes. The tag is everything between the ''. The tag is closed by an end tag with the same name as tag: ' '. Tagnames do not have spaces, are case sensitive, start with a capital letter, and additional words are also capitalized. A valid tagname might be 'BackgroundWidgets', while 'backgroundwidgets' would not be valid. Blizzard defines all valid tagnames in their UI.xsd. The XML user interface page has a good list under Widget Elements which will aid you until we get further along.

Everything between the tag and the end tag is content to the tag. Everything. Even other tags along with their content. In the case where there is no content to a tag, the end of the tag has '/>' instead of just '>' and there is no separate end tag. A complete tag with no content looks like:



Using the above piece of magic as an example, we can see that it is a tag with the name 'Ui' and it has three attributes (the funny forth line is a part of the prior attribute). Content is represented by the space between the end of the tag (the '>' on the fourth line) and the '' end tag.

HelloWorld.xml
Now to continue with our Hello World XML example. Please edit your HelloWorld.xml file copy and paste this bit of magic into it:

 

Now before we create the frame for our addon, we have to add a simple line of code that tells the World of Warcraft engine where to find our function in our .lua file. Notice this is a tag with no content, which gets a '/>' and no end tag. This is because this tag is solely for the engine.



World of Warcraft connects everything to a frame, even other frames. So, in order to create something that WoW will interact with, we create a frame:

 

The tagname is 'Frame' and we have used the 'name' attribute and given the attribute the value of 'HelloWorldFrame'.

Our frame tag is included as content to the Ui tag and so goes between the start Ui tag and the end Ui tag. To help keep track of what is surrounding what, we indent the content with respect to the enclosing tags like this:

    

It is very important that you do not mix up the various end tags and indenting helps keep things straight. Notice that the 'Frame' and 'Script' tag (And for the 'Frame' both the start tag and the end tag) are completely surrounded by the 'Ui' tag.

Inside the frame, one of the many things we can define are Scripts. Scripts are nothing more than small pieces of Lua code. Where we place the script determines when it will be invoked. Because Scripts live within a Frame we include the 'Scripts' tag inside the 'Frame' tag. Notice the difference, in the 'Scripts' tag, the 's' sets it apart from the 'Script' tag.

<Frame name="HelloWorldFrame"> <Scripts> </Scripts> </Frame>

The various widgets have several Events that can occur and if we want to declare a Widget Handler to process the event we include the event name under the Scripts tag of the widget we are interested in. Not every widget has the same set of events. In this example, we are interested in an event named 'OnLoad'. The OnLoad event happens when the widget is loaded into the UI. For this example, we want the OnLoad event to run the script named HelloWorld. This script was defined in the HelloWorld.lua as a function.

<Scripts> <OnLoad> HelloWorld; </OnLoad> </Scripts>

Take a look at the Widget handlers page for a list of widgets and the events you can write widget handlers for.

The complete HelloWorld.xml file should look like this:

 <Script File="HelloWorld.lua"/> <Frame name="HelloWorldFrame"> <Scripts> <OnLoad> HelloWorld; </OnLoad> </Scripts> </Frame> </Ui>

There is an important item you should note in the above. The HelloWorld; is the only piece which is NOT a tag or an attribute. It is important to note that content in a WoW .xml UI document is always a piece of code if it is not another set of tags and their associated attributes. The only valid place for a piece of code is under the tag for an event.

Having gotten this far, it is time to run your new AddOn.

Running your HelloWorld AddOn
You should have a directory under your AddOns directory named "HelloWorld" and in that directory should be three files named HelloWorld.toc, HelloWorld.lua, and HelloWorld.xml. The contents of these three files should be EXACTLY as noted above.

Now start World of Warcraft and log into your account, but don't select your character yet. Please click the red 'AddOns' button on the lower left of the character selection screen to see all of the AddOns WoW has detected. There will be one for every folder in your AddOns directory except for the AddOns starting with Blizzard_.

You should see your new HelloWorld in this list. The name should be yellow, and the checkbox to the left should be checked.

If the name is Red and you see an Out of date message to the right, you probably didn't change the ## Interface: 30300 value as described above under The .toc or Table of Contents. Please review that section and make the appropriate change. I do not recommend running your AddOns with the 'Load out of date AddOns' checkbox checked. That's asking for trouble as an AddOn that attempts to use old UI features can corrupt a new UI. Nearly every patch that changes the UI level has had problems with old AddOns that have not been updated to conform to the new UI standards.

If you don't see the name at all, make sure that you have placed the HelloWorld directory in the AddOns directory which is in the Interface directory under the World of Warcraft installation location. On my system (Windows) it is 'C:\Program Files\World of Warcraft\Interface\AddOns\HelloWorld'. The files inside that directory should all start with 'HelloWorld' and have the .toc, .lua, and .xml endings.

Please note the CaSe of the names. While Windows is insensitive to case for directory and file names, the case is important to other systems (e.g. Mac, Linux) Also, inside the game itself, World of Warcraft is sensitive to the case for the names of its variables and filenames. Keep the case the same to avoid problems.

Now, you have a yellow HelloWorld! showing up in your AddOn list. Note the ! in the name. The name of the AddOn shown in this list is taken from the ## Title: Hello World! line in the HelloWorld.toc file. In the future, we will see how to change colors and languages.

If you move your mouse cursor over the HelloWorld name, you should see a tool-tip pop up with two lines in it. The first line is the same as the Title, and the second line is taken from the ## Notes: My first AddOn line in the .toc file. This can also be customized for color and language.

Cancel out of the AddOns display and enter the world with any of your characters. Once your character loads, you should see a message in the default chat window that says Hello World!.

Success!!

Review
You have created your first AddOn and have successfully run it. Now let's review a bit about what was accomplished.

The 'Hello World!' text is taken from the line in the HelloWorld.lua file that reads 'print("Hello World!");'. Wrapped around that is a function named print which is responsible for displaying the text in the default chat window.

The print function is inside a function we created called HelloWorld that had no parameters. Our function will do the same thing every time it is called.

You should then recall that the name of our function was placed in the HelloWorld.xml file as the action to be taken when the Onload event of the Scripts tag of the Frame we created. We placed the name of our function in this specific place because we wanted our function to be executed (run, called, processed) when our Frame was fully loaded.

World of Warcraft knew that it should create our Frame because we placed the name of the .xml file into the .toc file. The .toc file must have the same name as the AddOn's directory and is the first of the files in our AddOn that Blizzard processes.

Inside the .toc file is where we tell World of Warcraft about our AddOn (the ## statements) and what files that need to be loaded. Every line that does not start with a ## is a file to be processed by World of Warcraft. The order that the file names appear is important because World of Warcraft processes the file in order listed.

Knowing that, we place the .lua file before the .xml file because we want the function HelloWorld declared (or defined) before we try to call it in the .xml file.

Even though this is a trivial example of an AddOn, important concepts and information have been covered. Further pages in this tutorial will cover other areas and expand upon topics initially presented here.

More

 * AddOn programming tutorial - The index page. Also contains references used throughout this tutorial.