Thread Tools Display Modes
02-24-09, 03:13 PM   #1
ZikO
An Aku'mai Servant
Join Date: Jun 2008
Posts: 36
What does _G mean?

Hi guys.

As you see, I have started coding in lua and I also try to understand cod written by the others. I found something I can't quite understand. One of the codes I found is part of BugSack. What does these two assignments mean?

Code:
local BugGrabber = _G.BugGrabber -- <- HERE
local BugGrabberDB = _G.BugGrabberDB
<- HERE

And also what does L before [""] means?

Code:
BugSack.options = {
	type = "group",
	handler = BugSack,
	args = {
		show = {
			type = "group",
			name = L["Show sack"],
			desc = L["Show errors in the sack."],
...
Thanks.
  Reply With Quote
02-24-09, 03:32 PM   #2
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
They're both tables. In the case of "_G", it's the global lookup table: _G[BugSack] is the same as _G.BugSack. The "L" is usually a locale table for translations. L["Speak"] would output "Sprechen" in the German locale, for example.
  Reply With Quote
02-24-09, 03:34 PM   #3
Tristanian
Andúril
Premium Member
AddOn Author - Click to view addons
Join Date: Nov 2007
Posts: 279
_G is a global variable that holds the global environment. BugSack (and many other addons) are usually setting a local reference to that environment using "local _G = getfenv(0)" in the beginning of the code. What BugSack does later on is setting local references to the "BugGrabber" and "BugGrabberDB" globals, so that it can access them a bit faster.

As for the L table, if you look closely at the code, it's being defined at the start, as :

local L = LibStub("AceLocale-3.0"):GetLocale("BugSack")

basically its a local table being used (in the vast majority of occasiong) to hold localization strings. In this particular implementation, the table is being fed by the AceLocale-3.0 lib, which we've used earlier to register localization strings (in the form of table entries), usually inside a localization file.
__________________
  Reply With Quote
02-26-09, 07:54 AM   #4
ZikO
An Aku'mai Servant
Join Date: Jun 2008
Posts: 36
Thanks guys.
  Reply With Quote
02-26-09, 09:08 AM   #5
Slakah
A Molten Giant
 
Slakah's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2007
Posts: 863
_G is also handy when using setfenv().
  Reply With Quote
02-27-09, 08:56 AM   #6
ZikO
An Aku'mai Servant
Join Date: Jun 2008
Posts: 36
Hmmm, I'm still confused. I am after the lecture of Programming in LUA where things are described roughly well but this is one I can't get through easily :/

So, is the point of typing
Code:
local _G = getfenv(0)
only to get to global environment faster? I have also find getfenv(1). What's the difference between getfenv(0) and getfenv(1)?

Regards.
  Reply With Quote
02-27-09, 09:26 AM   #7
IBLJerry
A Deviate Faerie Dragon
AddOn Author - Click to view addons
Join Date: Dec 2006
Posts: 12
For some time, Blizzard did not provide _G at all. So addon authors started to call getfenv(0) to get it. Now that Blizzard does provide _G, the only reason to use getfenv(0) is historical.

"fenv" in getfenv/setfenv means "function environment". It corresponds to the table that will be used to resolve global access in the function. getfenv() and setfenv() accept functions as a parameter, but also numbers. When a number is used, it corresponds to the call stack level. getfenv(1) returns the environment of the function that called getfenv(), getfenv(2) returns the environment of the function that called that function, ...
0 is a special value that means the "global environment".

Note that playing with the environment is rarely done inside wow, but a lot more common in command line lua. So, in wow, all calls to getfenv(), whatever the parameter, will return the same table, that's why you can find people calling getfenv(1). Once you start programming in lua outside of wow, mostly when you start writing modules, it becomes easier to understand.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » What does _G mean?


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