View Single Post
09-19-16, 02:34 PM   #11
Kanegasi
A Molten Giant
 
Kanegasi's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2007
Posts: 666
Originally Posted by rhiorg View Post
OK, so I understand what this does (and the concept of closures in general), but I don't know why I would want to do this. I'm digging into the Lua docs to try to figure it out, but if anybody has a succinct, practical example or explanation...that would be great. I can't for the life of me figure out the why.
Is it still the confusion with the local tostring=tostring? If you use any global values, including any default Lua functions, you are querying _G. Every time you use a global, you query _G. That is the giant table that contains everything accessible by your code. For example, there's a global variable named MAX_QUESTS in WoW that currently equals 25. You can access this variable just by its name, but you can also use _G.MAX_QUESTS or _G["MAX_QUESTS"] and both equal 25.

Lua is optimized around table access, so performance with "normal" usage is irrelevant. However, if your code loops through something or does something repeatedly within a short amount of time, like an OnUpdate script going through a sizeable table, it's good practice to "localize" any globals you are using. Depending on how heavy your processes are, there is a measurable performance increase with using local objects versus global objects.

Besides the minor performance increase with heavy usage, it's just good practice to "localize" globals you intend to repeatedly use.

Last edited by Kanegasi : 09-19-16 at 02:38 PM.
  Reply With Quote