Thread Tools Display Modes
01-16-15, 05:35 PM   #1
Sweetsour
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Dec 2014
Posts: 130
Is there a better way?

I have a curiosty I wanted to shed light on. I was curious if there's any point, or if it's worth it, to setup variables like so:

"Normal" method
Lua Code:
  1. addon.MainFrame = CreateFrame("Frame","MainFrame",UIParent);
  2.  
  3. -- Like This
  4. addon.MainFrame.MyLabel = CreateFrame("Frame",nil,addon.MainFrame);
  5. addon.MainFrame.MyLabel.Text = addon.MainFrame.MyLabel:CreateFontString(nil,"BACKGROUND","GameFontNormal");
  6. -- Or Like This
  7. addon.MyLabel = CreateFrame("Frame",nil,addon.MainFrame);
  8. addon.MyLabel.Text = addon.MyLabel:CreateFontString(nil,"BACKGROUND","GameFontNormal");

What about making a new [local] table to house frame elements?
Lua Code:
  1. local main = {};
  2. main = addon.MainFrame;
  3.  
  4. main.MyLbl = CreateFrame("Frame",nil,main,"InputBoxTemplate");
  5. main.MyLblText = main.MyLbl:CreateFontString(nil,"BACKGROUND","GameFontNormal");
  6. -- ... etc

I guess what I'm really asking is; is there extra ways to optmize variables? Also, does the length of variable names (ie. "addon.MainFrame.MyLabel.Text" vs "main.MyLblText") affect overall addon performance?
  Reply With Quote
01-16-15, 06:17 PM   #2
Duugu
Premium Member
 
Duugu's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 851
According to http://www.lua.org/gems/sample.pdf locals are faster than globals. Thus the second version should be faster.
Is it worth it? Depends on what you're doing. Probably not.

Regarding your second question: as every table lookup needs time I would guess that nested tables are slower.

Last edited by Duugu : 01-16-15 at 06:23 PM.
  Reply With Quote
01-16-15, 06:27 PM   #3
Sweetsour
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Dec 2014
Posts: 130
Thanks for the info and link!
  Reply With Quote
01-16-15, 10:47 PM   #4
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
I do something like this sometimes:
Lua Code:
  1. local myFrame = CreateFrame("Frame", nil, UIParent)  --this frame has no global name, only a local reference
  2. myFrame.label = myFrame:CreateFontString(......)
Then, if that child will be accessed more than once, I'll assign it to a local variable for faster lookups.
Lua Code:
  1. local myLabel = myFrame.label
  2. myLabel:SetPoint("CENTER")
  3. myLabel:SetText("Some text.")
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
01-17-15, 02:42 AM   #5
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by Sweetsour View Post
Lua Code:
  1. local main = {};
  2. main = addon.MainFrame;
Also this is kind of silly -- if you want "main" to be a reference to "addon.MainFrame" just set it as one directly, without creating and immediately discarding a random table in between:

Lua Code:
  1. local main = addon.MainFrame;
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
01-17-15, 01:18 PM   #6
Sweetsour
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Dec 2014
Posts: 130
Thanks guys, this really helps
  Reply With Quote
01-17-15, 02:38 PM   #7
Sweetsour
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Dec 2014
Posts: 130
Had another quick question with this code:

Lua Code:
  1. -- File A
  2. addon.FunctionInFileB(myVariable)
  3.  
  4. -- File B
  5. function addon.FunctionInFileB(var)
  6.      -- Various code
  7. end

Is "var" in File B's function considered global, or local?

EDIT: Based on what I've found via google, it seems they are local (which I had figured). If this is incorrect, please say so

Last edited by Sweetsour : 01-17-15 at 02:47 PM.
  Reply With Quote
01-17-15, 02:56 PM   #8
Lombra
A Molten Giant
 
Lombra's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 554
Yeah, it is local to the function, in exactly the same way as it would be had you done this:
Code:
function addon.FunctionInFileB()
     local var
end
__________________
Grab your sword and fight the Horde!
  Reply With Quote
01-17-15, 07:28 PM   #9
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
Originally Posted by Lombra View Post
Yeah, it is local to the function, in exactly the same way as it would be had you done this:
Code:
function addon.FunctionInFileB()
     local var
end
... Teeeechnically, yes... Except in Sweetsour's code, var would have the value myVariable passed to it, whereas in your code, var is nil.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
01-17-15, 07:37 PM   #10
Sweetsour
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Dec 2014
Posts: 130
So based on what I've leared from all this, I would assume Method A is better than Method B?

Method A
Lua Code:
  1. -- Initial Frame Setup
  2.     addon.CallBtn = CreateFrame("Button",nil,self); -- Used globablly across addon files
  3.     local CallBtn = addon.CallBtn;
  4.     CallBtn:SetNormalTexture("Interface\\Buttons\\UI-GroupLoot-Dice-Up");
  5.     CallBtn:SetHighlightTexture("Interface\\Buttons\\UI-GroupLoot-Dice-Highlight");
  6.     CallBtn:SetPushedTexture("Interface\\Buttons\\UI-GroupLoot-Dice-Down");
  7.     CallBtn:SetDisabledTexture("Interface\\Buttons\\UI-GroupLoot-Dice-Down");
  8.     CallBtn:SetWidth(50);
  9.     CallBtn:SetHeight(50);
  10.     CallBtn:SetPoint("TOP",0,-27);
  11.     CallBtn:Disable();
  12.     CallBtn:SetAlpha(0.4);

Method B
Lua Code:
  1. -- Initial Frame Setup
  2.     addon.CallBtn = CreateFrame("Button",nil,self); -- Used globablly across addon files
  3.     addon.CallBtn:SetNormalTexture("Interface\\Buttons\\UI-GroupLoot-Dice-Up");
  4.     addon.CallBtn:SetHighlightTexture("Interface\\Buttons\\UI-GroupLoot-Dice-Highlight");
  5.     addon.CallBtn:SetPushedTexture("Interface\\Buttons\\UI-GroupLoot-Dice-Down");
  6.     addon.CallBtn:SetDisabledTexture("Interface\\Buttons\\UI-GroupLoot-Dice-Down");
  7.     addon.CallBtn:SetWidth(50);
  8.     addon.CallBtn:SetHeight(50);
  9.     addon.CallBtn:SetPoint("TOP",0,-27);
  10.     addon.CallBtn:Disable();
  11.     addon.CallBtn:SetAlpha(0.4);
  Reply With Quote
01-17-15, 07:51 PM   #11
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,323
Technically yes, but when you're going through your init process, CPU power spent isn't as critical. The only time passing a value to a local makes a real difference is in code that is meant to run often. Examples of these are OnUpdate scripts and CombatLog events.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
01-18-15, 12:04 AM   #12
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
Still good habits to get into, nonetheless.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
01-18-15, 11:57 AM   #13
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,323
I'm just imagining how cluttered the local namespace would get in really big projects if everyone started doing this with init code. I honestly don't want to be the one to debug code like that.

Occasionally, if I don't need a reference back to the UI object I'm creating, I do something like this.
Note: This is only an example.
Lua Code:
  1. do
  2.     local CallBtn = CreateFrame("Button",nil,self);
  3.     CallBtn:SetNormalTexture("Interface\\Buttons\\UI-GroupLoot-Dice-Up");
  4.     CallBtn:SetHighlightTexture("Interface\\Buttons\\UI-GroupLoot-Dice-Highlight");
  5.     CallBtn:SetPushedTexture("Interface\\Buttons\\UI-GroupLoot-Dice-Down");
  6.     CallBtn:SetDisabledTexture("Interface\\Buttons\\UI-GroupLoot-Dice-Down");
  7.     CallBtn:SetWidth(50);
  8.     CallBtn:SetHeight(50);
  9.     CallBtn:SetPoint("TOP",0,-27);
  10.     CallBtn:Disable();
  11.     CallBtn:SetAlpha(0.4);
  12. end

The do...end block forces a lexical scope to be defined inside it. After the block is done executing, all locals defined inside it are freed.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 01-18-15 at 11:59 AM.
  Reply With Quote
01-18-15, 12:30 PM   #14
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
And if this
Lua Code:
  1. MyAddon.frame = CreateFrame("Frame")
  2. local frame = MyAddon.frame
were done inside of a function or other block, then it's scope would be limited to that block. Just like what you just wrote.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Is there a better way?


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