View Single Post
04-18-12, 05:23 PM   #5
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by Wildbreath View Post
but when i create frames as local - creates one widget or two and stores first in global table and rewtiting by second CreateFrame?

local frame1 = CreateFrame("frame","frame1",UIParent)
local frame2 = CreateFrame("frame","frame1",UIParent)
You are still creating two separate frame objects.

Code:
print(frame1 == frame2)
==> false
-- They are two separate objects, not the same object.

print(frame2 == _G["frame1"])
==> true
-- The global name "frame1" was most recently assigned to the frame
-- assigned to the frame2 variable.

print(frame1 == _G["frame1"])
==> false
-- The global name "frame1" was assigned to the frame2 object
-- after it was assigned to the frame1 object. The later assignment
-- takes priority, so the global name no longer points to the original object.
Frames are (basically) tables. Like all tables in Lua, they are passed by reference, not by value.

Code:
t1 = {}
t2 = {}
-- Creates two new table objects.

print(t1 == t2) ==> false
-- Despite having identical contents, they are not the same object.

t2 = t1
-- Point the t2 variable to the table originally assigned to the t1 variable.
-- The table originally assigned to the t2 variable still exists,
-- but no references to it remain, so it cannot be accessed, and will
-- eventually be garbage-collected.

t1.x = 5

print(t2.x) ==> 5
-- Since both variables now point to the same table, any changes to that table
-- are accessible through either variable.
Frames are exactly the same, except that they do not get garbage-collected because they have special properties (eg. userdata).

Also, your code is a terrible idea. If a frame already exists named "MyTestFrame" and I run this code:

Code:
local frame = CreateFrame("Frame", "MyTestFrame")
I want a new frame object, not some random frame object from another addon that happens to have the same global name.

Also, your code does not take into account that global variables can contain any type of data. For example, if I try to create a frame whose global name is "ABANDON_QUEST":

Code:
local frame = CreateFrame("Button", "ABANDON_QUEST", UIParent, "UIPanelButtonTemplate")
frame:SetPoint("CENTER")
frame:SetSize(100, 100)
frame:SetText("Abandon Quest!")
... your code would set the contents of my frame variable to the string that the Blizzard UI originally assigned to the global variable ABANDON_QUEST, and the rest of my code would fail, because a string does not have :SetPoint, :SetSize, or :SetText methods.

Your code does not optimize anything. It adds complexity, adds overhead, and would make it much more difficult to identify the cause of any naming conflicts.

Last edited by Phanx : 04-18-12 at 05:31 PM.
  Reply With Quote