Thread Tools Display Modes
05/05/15, 06:07 AM   #1
Phinix
 
Phinix's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 186
LUA refuses to see global scope from sub files.

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:

SousChef = {}
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:

Code:
MRL = {}
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:

Code:
local MRL = MRL
...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?

Last edited by Phinix : 05/05/15 at 06:26 AM.
  Reply With Quote
05/05/15, 06:14 AM   #2
Phinix
 
Phinix's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 186
I really want to use the first method Sous Chef employs, just because I would like to keep all of my functions in one global table.

However for me, defining MRL = {} in one file and local MRL = MRL in another doesn't work.
  Reply With Quote
05/05/15, 07:01 AM   #3
SpellBuilder
 
SpellBuilder's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2014
Posts: 10
Check your file load order in your .txt file. Could it be that you forget to include all .lua files there, or the order is incorrect?

EDT: Or this 'nil reference error' comes from somewhere else. When do you get this error? On addon load, or when you try to call this function. Please notice difference in definition with '.' and ':'. Second one is a syntactic sugar and hides local 'self' variable inside its definition.

Last edited by SpellBuilder : 05/05/15 at 07:05 AM.
  Reply With Quote
05/05/15, 07:07 AM   #4
Phinix
 
Phinix's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 186
*sigh*

I figured it out.

The problem was that the order of files declared in your manifest .txt file actually matters. >.>

So, you have to declare your main .lua file that sets up your global name space before you declare your sub-files.

Who knew? XD

EDIT: Yep, SpellBuilder you beat me to it. That was what it was.
  Reply With Quote
05/05/15, 07:48 AM   #5
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 5,032
I knew
Learned it by having errors as well though

Generally lua is wanting you to have source code, and the manifest declarations, in THAT order you need the variables declared in the files.
Only exception is OO code where the function names etc. can be somewhere below your current place of calling the function, in the same file.
If they are in another file I think you must include that other file BEFORE the file where you try to execute the code.
  Reply With Quote
05/05/15, 02:32 PM   #6
Phinix
 
Phinix's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 186
Originally Posted by Baertram View Post
I knew
Learned it by having errors as well though

Generally lua is wanting you to have source code, and the manifest declarations, in THAT order you need the variables declared in the files.
Only exception is OO code where the function names etc. can be somewhere below your current place of calling the function, in the same file.
If they are in another file I think you must include that other file BEFORE the file where you try to execute the code.
Baertram knows all!

  Reply With Quote
05/05/15, 03:28 PM   #7
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 5,032
Lerning from the best around here Thanks to all in this community, if I didn't thanked you (enough) before.
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » LUA refuses to see global scope from sub files.


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