Thread Tools Display Modes
Prev Previous Post   Next Post Next
10-30-14, 04:24 PM   #1
gmarco
An Onyxian Warder
 
gmarco's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2009
Posts: 362
Global vars ?

That's the lazy solution, but really you should just make sure you're not leaking any globals. Anywhere you define a variable, stick a "local" in front of it. If you need it accessible in a higher scope, define it in that higher scope with no value (just "local myVariable") and then give it its value later, but as a general rule, keep variables in the narrowest scope necessary. If it only needs to exist inside a certain function, make sure it's defined as local inside that function. If it only needs to exist inside a certain for loop, keep it inside the loop, etc.

You can use WowGlobalFinder to help you find all the leaks in your code.
Thanks for the reply Phanx,
I opened this new post because I like to discuss about this a little bit more if possible....

Is there a reason to use a variable as NON LOCAL ?

For example ... another addon of mine: Aggromon uses some saved variables for the configuration option.
These are defined in the .toc file:

Lua Code:
  1. ## SavedVariables: AGGROMON_ACTIVE, AGGROMON_SOUND, AGGROMON_CHAT, AGGROMON_SHOW, AGGROMON_SNDFLE, AGGROMON_ENAPVP

and in the addon I have:

Lua Code:
  1. local ADDON = ...
  2.  
  3. -- defaults if not founds values
  4. AGGROMON_ACTIVE = AGGROMON_ACTIVE or true
  5. AGGROMON_SOUND = AGGROMON_SOUND or true
  6. AGGROMON_CHAT = AGGROMON_CHAT or false
  7. AGGROMON_SHOW = AGGROMON_SHOW or false
  8. AGGROMON_SNDFLE = AGGROMON_SNDFLE or 1
  9. AGGROMON_ENAPVP = AGGROMON_ENAPVP or false
  10.  
  11. -- etc etc ...

I used them as non local, but the names should be fine to not break any others things in the UI.

The question is :-)
Is it fine or I should define them as local even if I am exporting outside the addon ...

Thanks again to all of you.

P.s
Checking better I have also this one :-)

Lua Code:
  1. SLASH_AGGROMON1 = "/aggromon";
  2. SlashCmdList["AGGROMON"] = function()
  3.     InterfaceOptionsFrame_OpenToCategory(options)
  4. end
__________________
This is Unix-Land. In quiet nights, you can hear the Windows machines reboot.

Last edited by gmarco : 10-30-14 at 04:29 PM.
  Reply With Quote
 

WoWInterface » Developer Discussions » Lua/XML Help » Global vars ?


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