View Single Post
03/24/14, 03:24 PM   #9
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 648
Originally Posted by Xrystal View Post
I believe the first var1 is classed as global across all addons and is the same as adding it to the _G table.
Correct.
Originally Posted by Xrystal View Post
I am not sure if ESO has the equivalent of the addon wide data table but that would be the only way to be addon wide across all files for just a single addon.
No, it does not.

Local variables are available at the scope they are defined and lower. Global variables are available to all things - hence being called "global". If you use a global variable called spacer, and I accidentally leak a global variable also called spacer, whichever one was defined last will overwrite the other.

If you want variables accessible throughout your entire addon, across multiple files, then use a table to store them in. Here is just one example of how you can do that.

File1.lua
Lua Code:
  1. MyAddonTable = {}   --define your table
  2. local MyAddonTable = MyAddonTable   --go ahead and give it a local reference, too, for this file
  3. MyAddonTable.var = "varForAllFiles"

File2.lua
Lua Code:
  1. --since File1 loaded first, MyAddonTable has already been created
  2. local var = MyAddonTable.var   --we can copy the value of MyAddonTable.var to a local variable
  Reply With Quote