Originally Posted by Fizzlemizz
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.