Thread Tools Display Modes
05-20-09, 09:03 PM   #1
Sythalin
Curse staff
 
Sythalin's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2006
Posts: 680
_G vs. getglobal problem/question

frame = "Minimap"

Code:
activeProfile[frame].width = getglobal(frame):GetWidth()
Code:
activeProfile[frame].width = _G[frame]:GetWidth()
_G is new to me, so I'm not exactly sure on what I'm doing here. The first one works, but the second one doesn't. Yet..

Code:
activeProfile[frame].scale = _G[frame]:GetScale()
activeProfile[frame].level = _G[frame]:GetFrameLevel()
activeProfile[frame].strata = _G[frame]:GetFrameStrata()
All work just fine. I'm confused.
  Reply With Quote
05-20-09, 09:11 PM   #2
Samsan
An Aku'mai Servant
 
Samsan's Avatar
Join Date: May 2009
Posts: 33
Red face _g

From what I've been reading the _G is suppose to be the same as getGlobal and will be fully replacing it, but not sure myself. I tend to go with what works...lol I'm not much help there...sorry
__________________
To give is to receive so the more that you give the more that you receive.
  Reply With Quote
05-20-09, 09:24 PM   #3
Sythalin
Curse staff
 
Sythalin's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2006
Posts: 680
Actually, getglobal() isn't working for me anymore either. Some crap has apparently changed with the last patch, cause everything was working just peachy pre-patch, now nothing is working. Considering I was supposed to release this tonight, I'm kinda pissed.
  Reply With Quote
05-20-09, 09:33 PM   #4
Samsan
An Aku'mai Servant
 
Samsan's Avatar
Join Date: May 2009
Posts: 33
Oh!

I can understand that...could explain why my mod suddenly stopped as well. I truly hope u can work it out.. I also posted the snips for you
__________________
To give is to receive so the more that you give the more that you receive.
  Reply With Quote
05-20-09, 10:23 PM   #5
Sythalin
Curse staff
 
Sythalin's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2006
Posts: 680
Apparently some code cleanup changed how some things were referenced. All is working fine. I'll take a look at your snippets as soon as I get this thing wrapped up.
  Reply With Quote
05-20-09, 10:42 PM   #6
Samsan
An Aku'mai Servant
 
Samsan's Avatar
Join Date: May 2009
Posts: 33
Smile Thx

Np and thx...I appreciate your time.
__________________
To give is to receive so the more that you give the more that you receive.
  Reply With Quote
05-20-09, 11:08 PM   #7
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
iirc (you can look on wowcompares.com or at the code yourself if you wish to confirm) getglobal() is merely
Code:
function getglobal(var)
     return _G[var]
end
(or it might have a tostring() in there...)

Using _G[blah] is the same as getglobal() - just remember that whatever you use in the _G table lookup must be a string (or a variable that references a string).

example of _G lookup code:
Code:
print(_G["ChatFrame1"])
     ChatFrame1
print(_G[ChatFrame1])
     nil
for i = 1,3 do print(_G["ChatFrame"..i]) end
     ChatFrame1
     ChatFrame2
     ChatFrame3
__________________
"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
05-21-09, 02:56 AM   #8
haste
Featured Artist
 
haste's Avatar
Premium Member
Featured
Join Date: Dec 2005
Posts: 1,027
The reason that WoW has getglobal is because _G wasn't exposed to us until 2.0. Before that some of us used local _G = getfenv(0) to get a hold of it. We got access to _G as a global during the transition from Lua 5.0 to Lua 5.1.

The function is defined on the C side of things, but its probably just what Seerah stated above.

The main difference between the two is that getglobal() is a function call, while _G is just a table lookup. So all in all it's just a minor performance difference.

Another thing that should be said is that Blizzard is phasing out the use of getglobal() in favor for _G, but they aren't likely to remove getglobal(). If anything it will probably be moved to the FrameXML.
  Reply With Quote
05-21-09, 03:20 AM   #9
kraftman
A Theradrim Guardian
AddOn Author - Click to view addons
Join Date: Dec 2008
Posts: 63
Originally Posted by ChaosInc View Post
frame = "Minimap"

Code:
activeProfile[frame].width = getglobal(frame):GetWidth()
Code:
activeProfile[frame].width = _G[frame]:GetWidth()
_G is new to me, so I'm not exactly sure on what I'm doing here. The first one works, but the second one doesn't. Yet..

Code:
activeProfile[frame].scale = _G[frame]:GetScale()
activeProfile[frame].level = _G[frame]:GetFrameLevel()
activeProfile[frame].strata = _G[frame]:GetFrameStrata()
All work just fine. I'm confused.
assuming your frame is created by CreateFrame() (and isn't a string with a misleading name) then you dont need _G[] to get stuff from it. For example with

Code:
local frame = CreateFrame("Frame", "MyTestFrame", UIParent)
you can either do

Code:
activeProfile[frame].scale = frame:GetScale()
or

Code:
activeProfile[frame].scale = MyTestFrame:GetScale()
the first one doesnt need _G[] because it uses a local pointer to the frame you have create, the second doesnt need _G[] because it is using the global name of the frame.

An example of where you would need _G would be

Code:
for i = 1, 5 do
local f= CreateFrame("Frame", "MyTestFrame"..i, UIParent)
end

for i = 1, 5 do
activeProfile[frame..i].scale = _G["MyTestFrame"..i]:GetScale()
end
  Reply With Quote
05-21-09, 07:17 AM   #10
Sythalin
Curse staff
 
Sythalin's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2006
Posts: 680
Thanks for the responses, explanations and all that but, um....

Originally Posted by ChaosInc View Post
Apparently some code cleanup changed how some things were referenced. All is working fine....
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » _G vs. getglobal problem/question


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