WoW:Getting started with writing AddOns (source)
Revision as of 04:48, 15 August 2023
, 15 August 2023Move page script moved page Getting started with writing AddOns to WoW:Getting started with writing AddOns without leaving a redirect
mNo edit summary |
m (Move page script moved page Getting started with writing AddOns to WoW:Getting started with writing AddOns without leaving a redirect) |
||
| (13 intermediate revisions by 2 users not shown) | |||
| Line 2: | Line 2: | ||
The following is a guide for getting started with building WoW AddOns. Creating your own AddOns can be a difficult process at first, but can be very satisfying and is a great way to learn programming in general. | The following is a guide for getting started with building WoW AddOns. Creating your own AddOns can be a difficult process at first, but can be very satisfying and is a great way to learn programming in general. | ||
If you are mostly interested in what files and structure comprise a WoW AddOn, you can skip to [[WoW AddOn]], and come back here or move on from there. | If you are mostly interested in what files and structure comprise a WoW AddOn, you can skip to [[AddOn|WoW AddOn]], and come back here or move on from there. | ||
== What should I make == | == What should I make == | ||
| Line 18: | Line 18: | ||
Research the programming environment: | Research the programming environment: | ||
* [[Lua]] - This is the programming language used by WoW for UI AddOns. | * [[Lua]] - This is the programming language used by WoW for UI AddOns. | ||
* [[ | * [[AddOn]] - These pages explain the basic structure of a WoW AddOn. | ||
* [[World of Warcraft API]] - These are the functions and ways to talk to WoW with Lua. | * [[World of Warcraft API]] - These are the functions and ways to talk to WoW with Lua. | ||
* [[Curse]] - Find a real but simple example of an existing AddOn. | * [[\Curse Forge]] or [[\WoW Interface (site)|WoW Interface]] - Find a real but simple example of an existing AddOn. Most real AddOns, even the simple ones, however can be difficult to read for getting started, with many moving parts. | ||
* [[Lua editors]] - A long list of code editors and other tools often used by other AddOn creators. | * [[Lua editors]] - A long list of code editors and other tools often used by other AddOn creators. | ||
Start with making a simple AddOn that has almost no moving parts, where you can see what all of the basic parts are and move forward from there. Use the reference material described above, especially [[WoW AddOn]], as that describes what really makes up an AddOn. | Start with making a simple AddOn that has almost no moving parts, where you can see what all of the basic parts are and move forward from there. Use the reference material described above, especially in [[AddOn|WoW AddOn]], as that describes what really makes up an AddOn. | ||
After completing your first one you will then have enough skill to start to build the real AddOn you wanted to originally make. | After completing your first one you will then have enough skill to start to build the real AddOn you wanted to originally make. | ||
| Line 45: | Line 45: | ||
=== File Types === | === File Types === | ||
There are three main types of files that you'll need to worry about with AddOn editing: | There are three main types of files that you'll need to worry about with AddOn editing: | ||
* [[TOC | * [[TOC file]] - This file is required for any WoW AddOn, and supplies WoW with information about your AddOn and its files. | ||
* LUA | * [[LUA file]] - This contains Lua code for your AddOn. | ||
* XML | * [[XML file]] - This holds the layout of UI windows, buttons, and similar for your AddOn. | ||
=== TOC File === | === TOC File === | ||
This is the file that instructs WoW in some basics about your AddOn and what other files you want loaded. Here's a short example of one. If you pasted this code into a 'MyAddOn.toc' file it would work as a beginning toc file: | This is the file that instructs WoW in some basics about your AddOn and what other files you want loaded. Here's a short example of one. If you pasted this code into a 'MyAddOn.toc' file it would work as a beginning toc file: | ||
<pre> | |||
## Interface: {{API LatestInterface}} | |||
## Title: My AddOn | |||
## Notes: This AddOn does nothing but display a frame with a button | |||
## Author: My Name | |||
MyAddOn.xml | |||
MyAddOn.lua | |||
</pre> | |||
You can read more about what you need or can put in a TOC over at [[The TOC Format]]. | You can read more about what you need or can put in a TOC over at [[The TOC Format]]. | ||
| Line 64: | Line 66: | ||
[[Lua]] files contain functional pieces of code. You may choose to only have one of these or break up your code into multiple files for a specific purpose. Here's a short example of one: | [[Lua]] files contain functional pieces of code. You may choose to only have one of these or break up your code into multiple files for a specific purpose. Here's a short example of one: | ||
<pre> | |||
function MyAddOn_OnLoad() | |||
SlashCmdList["MyAddOn"] = MyAddOn_SlashCommand; | |||
SLASH_MYADDON1= "/myaddon"; | |||
this:RegisterEvent("VARIABLES_LOADED") | |||
end | |||
function MyAddOn_SlashCommand() | |||
print("Hello, WoW!") | |||
end | |||
</pre> | |||
=== XML Files === | === XML Files === | ||
XML files | XML files can be used to specify the visual style of your frames. More importantly though a frame allows you to easily pass events to your Lua files. [[Frame XML]] files are how [[Blizzard]] defines most of their own UI windows in the game. Check out [[XML User Interface]] for details. Here's a short example of an XML file: | ||
<pre> | |||
<Script file="MyAddon.lua"/> | |||
<Frame name="MyAddon"> | |||
<Scripts> | |||
<OnLoad> | |||
MyAddon_OnLoad(); | |||
</OnLoad> | |||
</Scripts> | |||
</Frame> | |||
</pre> | |||
== Start Coding == | == Start Coding == | ||
| Line 90: | Line 96: | ||
Beware Lua functions and variables are globals by default and are directly visible to the other addons. If authors blindly use simple and common names, like: | Beware Lua functions and variables are globals by default and are directly visible to the other addons. If authors blindly use simple and common names, like: | ||
<pre> | |||
addonEnabled = true; | |||
function GetArrowsCount() | |||
... | ... | ||
end | |||
</pre> | |||
An addon will easily conflict with variables of the same name from other addons or from the Blizzard interface. Instead, you should write your addon more like a Lua module: place all the data and functions in a single global table, named after the addon, and use local declarations otherwise (no other globals), like: | |||
<pre> | |||
MarksmanAddOn = { }; -- about the only global name in the addon | |||
local addonEnabled = true; -- local names are all ok | |||
function MarksmanAddOn:GetArrowsCount() | |||
v... -- function is now inside the global table | |||
end | |||
MarksmanAddOn.arrowsCount = MarksmanAddOn:GetArrowsCount() | |||
</pre> | |||
For large add-ons this can get complicated, and the current version of Lua doesn't offer much to help. Use a full, clear, name for the global table, and assign a shorter name in your .lua files, like: | For large add-ons this can get complicated, and the current version of Lua doesn't offer much to help. Use a full, clear, name for the global table, and assign a shorter name in your .lua files, like: | ||
<pre> | |||
local mod = MarksmanAddOn; | |||
-- then you can write | |||
mod.arrowsCount = mod:GetArrowsCount() | |||
Other | </pre> | ||
Other than the global table for the addon, you may still need globals for the saved variables and the addon slash commands (see below) if you have any. | |||
== Slash Commands == | == Slash Commands == | ||
| Line 119: | Line 134: | ||
* The SavedVariables is only read from on UI loading, not real time, and only saved to on UI unloading. This will generally be from a user logging in or logging out. | * The SavedVariables is only read from on UI loading, not real time, and only saved to on UI unloading. This will generally be from a user logging in or logging out. | ||
* Basically, WoW makes a SavedVariables file for you named after your AddOn. You only get to specify in your TOC *which* variables get saved into that file. | * Basically, WoW makes a SavedVariables file for you named after your AddOn. You only get to specify in your TOC *which* variables get saved into that file. | ||
== Advanced but Important Topics == | == Advanced but Important Topics == | ||
| Line 126: | Line 140: | ||
After learning all of the basics, there are times where you may want to see how the Blizzard UI code works to help you in making your own AddOn. And getting information on how the WoW UI works can be helpful to making your AddOn function. | After learning all of the basics, there are times where you may want to see how the Blizzard UI code works to help you in making your own AddOn. And getting information on how the WoW UI works can be helpful to making your AddOn function. | ||
See [[Extracting interface files]] for getting the Blizzard UI AddOn source code which can also be used as a reference. | See [[Extracting WoW user interface files]] for getting the Blizzard UI AddOn source code which can also be used as a reference. | ||
=== Localization === | === Localization === | ||
| Line 135: | Line 149: | ||
{{elink|site=FreeKode.org|link=http://freekode.org/wow-first-addon/|desc=World of Warcraft first addon|bydate=May 3, 2015}} | {{elink|site=FreeKode.org|link=http://freekode.org/wow-first-addon/|desc=World of Warcraft first addon|bydate=May 3, 2015}} | ||
{{elink|prefix= |site=GitHub|link=https://github.com/freekode/TestAddon/|desc=freekode/TestAddon}} | {{elink|prefix= |site=GitHub|link=https://github.com/freekode/TestAddon/|desc=freekode/TestAddon}} | ||
[[Category:HOWTOs | [[Category:WoW HOWTOs]] | ||