Quantcast WoWInterface - View Single Post - Defining functions
View Single Post
12-12-13, 09:16 AM   #6
A Molten Giant
Vlad's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2005
Posts: 789
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.
local UnitName
  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()
    return name, realm

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