PDA

View Full Version : global script modules problem



MeepoSose
September 4th, 2009, 16:58
I am having a problem sharing info from a global script.

I am trying to create a bunch of lookup values I can share across all scripts. I added the following line to the extension.xml file under the <base> element:



<script name="Lookups" file="scripts/global_lookups.lua" />


I declare a bunch of tables inside that file as global variables (i.e., not prefaced with "local <variablename>" and then I try to reference them from another script. I always seem to get an error about attempting to index a nil value. The lua references I saw online indicate this should work but maybe this is a known limitation on the implementation of lua within FG. Anyone know?

print(Lookups.myVariable); returns a nil attempting to index global "Lookups"

Other things I tried with similar results are

dofile("global_lookups.lua");
require("global_lookups.lua");
module("global_lookups.lua");

within the onInit function of my calling script. For each of those attempts, it doesn't recognize "dofile", "require" or "module."

Foen
September 4th, 2009, 17:12
Global variables *do* work, but there are some constraints around the order in which you declare things. For example, I don't think you should declare a variable in an extension global script and then try to access it from the onInit of a ruleset (non-extension) script file as it initialises the latter before the former, I believe.

Likewise, referencing a global script from another one which is declared earlier, may cause problems:


<script name="ScriptA" file="scripts/script_a.lua"/>
<script name="ScriptB" file="scripts/script_b.lua"/>

Will cause problems if you try to reference something in ScriptB from ScriptA.

All this is a bit trial and error, so I'm not even sure if the foregoing is correct as I can't find much documentation on it.

Foen

MeepoSose
September 4th, 2009, 17:32
Hmmm... I thought there might be something like that going on, so I moved my script statement above everything else in <base> within the extension.xml. It still doesn't seem to reference it from a second script that is called via adventure_npcs.xml.

So basically, my setup looks like this:

1. global script defined in extension.xml (before everything else)
2. adventure_npcs.xml is overridden with a version in the extension (although not referenced as an <includefile source="adventure_npcs.xml" />
3. adventure_npcs.xml uses a template field which links to another script, in this case "npcskillfield.lua"
4. npcskillfield.lua attempts to call into the global script from within the onInit method.

I am wondering if this is getting further complicated by trying to do this within an extension as opposed to a generic ruleset.

BTW, thanks Foen for your quick response.

MeepoSose
September 9th, 2009, 19:15
Hmmm... I thought there might be something like that going on, so I moved my script statement above everything else in <base> within the extension.xml. It still doesn't seem to reference it from a second script that is called via adventure_npcs.xml.

So basically, my setup looks like this:

1. global script defined in extension.xml (before everything else)
2. adventure_npcs.xml is overridden with a version in the extension (although not referenced as an <includefile source="adventure_npcs.xml" />
3. adventure_npcs.xml uses a template field which links to another script, in this case "npcskillfield.lua"
4. npcskillfield.lua attempts to call into the global script from within the onInit method.

I am wondering if this is getting further complicated by trying to do this within an extension as opposed to a generic ruleset.

BTW, thanks Foen for your quick response.


Simple mistake. The problem was that I was declaring my variables and populating them outside of a function. I put them inside my onInit() function within my global lua script and they were then accessible.