Thread Tools Display Modes
01-22-14, 06:13 PM   #21
Lombra
A Molten Giant
 
Lombra's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 554
Originally Posted by Fizzlemizz View Post
For the final piece of the puzzle, is Clamsoda's assertion (belief) correct that

"_G is just a table in the global scope, "

and that is where you store (copy, place) variables and functions you want make accessible to other addons (directories under \Interface\Addons).
_G is a table in the global scope (or just "it's a global table"), but _G also *is* the entire global scope. To access variables between addons, you must have some global reference to it, yes.

Did not carefully read all replies, but I just want to clarify, that the reason people use _G is often because they already have a identically named local variable. Take the previous example:
Code:
local _, MyAddon = ...
_G.MyAddon = MyAddon
_G is necessary because otherwise you would just be assigning a value to the local variable that you just declared above. If you named that one differently, _G would not be necessary.
Code:
local _, addon = ...
MyAddon = addon
MyAddon is now global. MyAddon and _G.MyAddon points to the same table.

The reason you might not want to do MyAddon = {} directly is because ... already contains a [private addon] table, which will get passed to all Lua files in your addon folder, allowing you to use the same table in your whole addon without needing a global reference to it to access it between files.
__________________
Grab your sword and fight the Horde!
  Reply With Quote
01-22-14, 06:44 PM   #22
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,877
_G is a table in the global scope (or just "it's a global table"), but _G also *is* the entire global scope. To access variables between addons, you must have some global reference to it, yes.
I can write

function SomeFunction()
...
end

or

MyUsefulTable = {...}

And that is global (accessible to all addons) but I presume it's the sort of declaration that gets tut tutted at as "soo many globals" when people are making comments

local _, Myaddon = ...
_G.MyAddon = MyAddon

function Myaddon:SomeFunct()
...
end


Myaddon.MyUsefulTable = {...}

If I get it right then this function and table can be accessed by other addons as:
local xxx = _G.MyAddon.SomeFunc()
local yyy = _G.MyAddon.MyUsefulTable.yadayadayada

Is this is essitially declared to the "proper" method for achieving what example 1 was doing?

Apologies for hijacking the thread and being So long winded but I really do want to get it correct.

Edit: A bit of testing later and the answer seems to be a global is a global is a global and when someone says "why so many globals", it's because you've created useless globals not because youve missed some cryptic mechanism for declaring/using globals "safely".

function MyFunction()
...
end

and:

local function MyFunction()
...
end
_G.MyFunction = MyFunction

and:

local _, MyAddon = ...
function MyAddon:MyFunction()
...
end
_G.MyAddon = MyAddon

are essentially the same (with the second and third having both the local MyFunction and a global reference(pointer, address... (choose your language of choice equivilent)) to MyFunction) and all can be called from any addons using either MyFunction(), _G.MyFunction() or _G.MyAddon.MyFunction() (I didn't test wether MyAddon.MyFunction() or MyAddon:MyFunction() works but I presume one of them does... tomorrow). That was the bit that was throwing me, thinking there was an effective difference/desirability between the MyFunction() and the _G... calls.

Gnome learning, it's a slow process.

Thank you everyone for your help and patience.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 01-22-14 at 11:47 PM.
  Reply With Quote
01-22-14, 10:27 PM   #23
Choonstertwo
A Chromatic Dragonspawn
 
Choonstertwo's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2011
Posts: 194
Originally Posted by Seerah View Post
Functions don't work the same way.
Lua Code:
  1. local function myFunc() end
  2. _G.myFunc = myFunc
  3. print(myFunc) --> function:093hj1k
  4. print(_G.myFunc) --> function:1lk098a
Functions get "copied", so you now have two separate functions independent of one another. It's just like doing
Lua Code:
  1. varA = "apple"
  2. varB = varA
  3. varA = "banana"
  4. print(varA) --> "banana" (because we changed it on line 3)
  5. print(varB) --> "apple" (because varA still was "apple" when assigning varB on line 2)
That's not the case, to quote the manual:
Originally Posted by Lua 5.1 Reference Manual
Tables, functions, threads, and (full) userdata values are objects: variables do not actually contain these values, only references to them. Assignment, parameter passing, and function returns always manipulate references to such values; these operations do not imply any kind of copy.
Lua 5.1 Reference Manual

So functions work just like the table example earlier in your post:
lua Code:
  1. local function myFunc() end
  2. _G.myFunc = myFunc
  3. print(myFunc)  --> function: 0072B7D8
  4. print(_G.myFunc) --> function: 0072B7D8

The two variable hold pointers to the same function.
  Reply With Quote
01-23-14, 12:48 AM   #24
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by Choonstertwo View Post
...functions work just like the table example earlier in your post...
Yes, and you can see an example of this right in the game's base Lua implementation:

Code:
/dump string.len == strlen
While string.len and strlen are different "variables" (technically in the first case, only string is a variable, and len is a key in the table it points to, but close enough) they both point to the same function in memory.
__________________
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
01-27-14, 06:07 PM   #25
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
As explained earlier, copied objects share the same pointers. There may be some confusion about this due to the fact that the only modifiable object is a table. Even when you're writing a function hook, you're creating a separate function with either identical code or runs as a wrapper. After assigning the new function to overwrite the original variable, it changes the pointer to reference the new one. Lua does NOT hunt down other variables that contain the old pointer.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » funcName = function(p1, p2, ...) instead of function funcName(p1, p2, ...)?


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