_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.