Thread: KGPanels script
View Single Post
11-10-14, 11:15 PM   #30
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Stop doing this:

Lua Code:
  1. _G["ChatFrame1"]:Hide() -- NO!

Just do this:

Lua Code:
  1. ChatFrame1:Hide() -- YES!

Adding the extra _G lookup just makes your code more cluttered to read, and slower to run. The only reason to use a _G lookup is if you are dynamically construting a name that is not constant or known ahead of time, eg. in a loop:

Lua Code:
  1. for i = 1, 10 do
  2.      _G["ChatFrame"..i]:SetFont("Fonts\\FRIZQT__.ttf", 16, "OUTLINE")
  3. end

Also, don't do this (whoever suggested this solution, just... no, and stop using Blizzard's UI code as your guide; most of it is obviously written by people who are not familiar with Lua programming -- there's even a place where they do ~30 if/else checks to uppercase a string instead of just calling string.upper FFS):

Lua Code:
  1. local oEvents = {
  2.     "GOSSIP_SHOW",
  3.     "BANKFRAME_OPENED",
  4.     "GUILDBANKFRAME_OPENED"
  5. }
  6.  
  7. if tContains(oEvents,event) then

This is quite possibly THE most inefficient way you could achieve this goal. First, you are creating a new table every time an event fires. This is a total waste of memory, and wastes CPU cycles on garbage collection. Second, you are adding a function call (which is one of the slowest, if not the slowest, single action you can perform in Lua) and looping over your entire table, instead of just structuring your table correctly for your purpose in the first place.

You should just do this instead:

Lua Code:
  1. if event == "GOSSIP_SHOW"
  2. or event == "BANKFRAME_OPENED"
  3. or event == "GUILDBANKFRAME_OPENED"
  4. then

(You can put it all one line if you want; I just included the line breaks to keep it more readable in the tiny boxes kgPanels provides for code entry.)

If you end up checking more events, using a table might be worthwhile, but please do it the right way. Put the table in your OnLoad script so it's only created once, and structure it correctly (hash table, not array):

Lua Code:
  1. self.openEvents = {
  2.     GOSSIP_SHOW = true,
  3.     BANKFRAME_OPENED = true,
  4.     GUILDBANKFRAME_OPENED = true,
  5. }

Then refer to it in your OnEvent script:

Lua Code:
  1. if self.openEvents[event] then
__________________
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