WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Is there a better way? (https://www.wowinterface.com/forums/showthread.php?t=51552)

Sweetsour 01-16-15 05:35 PM

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?

Duugu 01-16-15 06:17 PM

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.

Sweetsour 01-16-15 06:27 PM

Thanks for the info and link!

Seerah 01-16-15 10:47 PM

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.")

Phanx 01-17-15 02:42 AM

Quote:

Originally Posted by Sweetsour (Post 305022)
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;

Sweetsour 01-17-15 01:18 PM

Thanks guys, this really helps :)

Sweetsour 01-17-15 02:38 PM

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 :)

Lombra 01-17-15 02:56 PM

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


Seerah 01-17-15 07:28 PM

Quote:

Originally Posted by Lombra (Post 305136)
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.

Sweetsour 01-17-15 07:37 PM

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);

SDPhantom 01-17-15 07:51 PM

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.

Seerah 01-18-15 12:04 AM

Still good habits to get into, nonetheless.

SDPhantom 01-18-15 11:57 AM

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.

Seerah 01-18-15 12:30 PM

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. :p


All times are GMT -6. The time now is 03:16 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI