Thread Tools Display Modes
02-11-14, 01:55 AM   #1
Malsomnus
A Cobalt Mageweaver
AddOn Author - Click to view addons
Join Date: Apr 2013
Posts: 203
About variable scopes

Is there any way to access an add-on's local variables from outside that add-on's code? Maybe something similar to how you access globals with _G[...]?
__________________
SanityCheck - If you've ever said the words "Sorry, I forgot" then you need this add-on.

Remember, every time you post a comment on an add-on, a kitten gets its wings!
  Reply With Quote
02-11-14, 02:19 AM   #2
Clamsoda
A Frostmaul Preserver
Join Date: Nov 2011
Posts: 269
Of course, remove the local portion of your variable. That places the variable into the global space, and should be accessible to any AddOn, after the variable has been declared.

Additionally, you could place it into _G by way of _G["MyAddOnVariable"] = "Ducks are neat."

Note: Both methods achieve the same goal; see here for a discussion about globals (you'll need to scroll down a bit).

Last edited by Clamsoda : 02-11-14 at 02:24 AM.
  Reply With Quote
02-17-14, 01:23 PM   #3
Rainrider
A Firelord
AddOn Author - Click to view addons
Join Date: Nov 2008
Posts: 454
A better way to expose your addon "local" variables to other addons is by putting them inside a global table, named after your addon's name. That way you don't have to take care that every variable is uniquely named in the global scope, just the addon name should be (you can't have two different addons with the same name). The other advantage is that by doing this one has to access your variables by addon.variable which implies that the call is intended and not accidental (i.e. not a mistyped variable that happens to be what you named it). Also this way you add a single entry to the global scope and "hide" the rest into it, instead of the clutter it would be if you added all the rest separately. One could also make a local reference to your addon table in their code and have a slightly faster access to the variables inside it.

Keep in mind that WoW's frames are just tables to Lua. In your code define your frames locally (for access speed) and give them global names, if you need that. If also may be a better idea to define functions that can access your local variables and make those available to other addons, instead of giving direct access to the variables. That way you can detect access and do additional stuff if you need to.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » About variable scopes


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off