View Single Post
12-12-13, 09:16 AM   #6
Vlad
A Molten Giant
 
Vlad's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2005
Posts: 793
Also when using a local function you can play with the variables and store them in unusual ways.

Like for instance this snippet. I've used similar to alter the default UnitName returned values to always include the realm, instead of nil when the unit is on the same realm as you. Note this is old code and with recent realm changes it might not work as expected.
Code:
local UnitName
do
  local _G, name, realm = _G -- the _G assignment is often frowned upon or looked at as redundant

  function UnitName(...)
    name, realm = _G.UnitName(...)
    if not realm then
      realm = GetRealmName()
    end
    return name, realm
  end
end

print(UnitName("target")) -- if the target is on your realm it actually returns the name instead of nil
The pro with doing it like this is that name and realm are local yet not in the whole scope of the addon code. And since it reuses the same storage area for both name and realm between the function calls it just feels nice.

I doubt you'd notice much performance improvement between this version and simply using "local name, realm = _G.UnitName(...)" - maybe if your testing scope is large enough you might notice some time differences.
__________________
Profile: Curse | Wowhead
  Reply With Quote