I have seen countless addons do this, yet when I try I get nil reference errors.
Take Sous Shef for example. In the main file it defines its main data table:
Then in another file called "Common" it adds functions to this same global table. At the top of the file it declares a local variable for this global namespace table like this:
Code:
local SousChef = SousChef
It then is able to add functions to the global space table as normal like this:
Code:
function SousChef.HookInventory()
...
end
Works fine. So, now lets try it in my addons.
In my main file I declare a main global namespace table just like Sous Chef:
Then in a separate file I use to house a bunch of utility functions I try the Sous Chef method of declaring this global table as a local named the same:
...and try to add functions to the global table the same way:
Code:
function MRL.SetSavedVar(index, tier, value)
...
end
Oops, sorry. You used a letter before 'N' and the addon was created on the 2nd Tuesday after a full moon! You can't access that feature! Instead, you will get: "...attempt to index a nil value"!
Another example is Awesome Guild Store. In the Startup file it declares its global name space:
Code:
AwesomeGuildStore = {}
Then in SalesCategorySelector it references it in a slightly different way:
Code:
local SalesCategorySelector = ZO_Object:Subclass()
AwesomeGuildStore.SalesCategorySelector = SalesCategorySelector
It then adds functions in this global space like this:
Code:
function SalesCategorySelector:CreateSubcategory(name, category, categoryPreset)
...
end
OK, let's try this method of declaring a Subclass, which I really don't like the look of but what the heck, worth a try.
Same utility file:
Code:
local SavedVariables = ZO_Object:Subclass()
MRL.SavedVariables = SavedVariables
function SavedVariables:SetSavedVar(index, tier, value)
...
end
"...attempt to index a nil value"
Can someone please explain to me why this works in one addon and not another?
Why can I not seem to add functions to my global addon table from my other files?