WoW:Lua basics

From AddOn Studio
Jump to navigation Jump to search


The World of Warcraft game client provides many functions available for use in Lua script files provided by AddOns. These functions are the WoW API, are also used by Blizzard to build the game client interface. They allow addons to query information about the player and the game world, to receive notifications (events) when the player interacts with the game world, and to trigger player actions like changing target, casting spells, using professions, using vehicles, joining a group, etc.

Global variables in Lua[edit]

By default any new variable you create in Lua scripts is global, meaning the variable can be accessed (and overwritten) by other scripts that happens to use the same variable name. To create local variables, precede the new variable with the keyword "local". Local variables are visible (and accessible) only from the file or block in which they are created.

All AddOns in World of Warcraft share the same global variables. Considering many players have lots of AddOns installed and loaded at the same time, it quickly follows that AddOns should avoid simple names for their global variables. Otherwise it is rather easy for two AddOns to declare and use the same variable, ending with one of the AddOns overwriting that variable for the other.

For this reason you should always use local variables everywhere in your script files, and only use a single global variable, or a couple of them, with a longer name specific to your AddOn (so it is unlikely other AddOn will use the same name, for example: "RoleBuffAddOn"). Use this global name for a Lua object (table), that includes all other AddOn data you want to be available and visible between script files. If you only have a single script file, you can keep all your variables local.

Same Lua scripts create a local variable named "m" and use it to store and retrieve data/functions for "the current module", but I suggest you use "mod" instead. For example each of your scripts could include somewhere at the start:

local mod = RoleBuffAddOn;

(here "RoleBuffAddOn" is a global object created by the first of your script files)

This way scripts can put data and functions into the local object "mod", and they will be available to all other scripts in your AddOn.

Variable and function names[edit]

WoW API functions and event names provided by Blizzard begin with a capital letter, like IsMounted() or UnitIsUnit(). Note these are global names.

For this reason you should keep your local variables and functions beginning with a lowercase letter, like combatCheckWarrior(). This way you know that new functions added by Blizzard in WoW API, will never introduce a conflict with your own function names.