View Full Version : Scripting Question
JMOxx75
December 24th, 2011, 12:53
Fantasy grounds is telling me that a script is attempting to access a global variable that has a value of nil. Only problem is the variable is declared as a local variable inside a function in this script?
local posn = position[1];
It thinks that position is a global variable?
This is a script inside an extension if that means anything.
JMOxx75
December 28th, 2011, 23:09
ok there is an field inside the template in the xml file called <position>. So all this is trying to do is get the coordinates from this field. I guess I just need some help figuring out how to make it stop thinking that its a global variable so it starts acting right.
If this script isn't called until a character is created and then a field on said character is populated then why is this throwing an error when I fire up FG2 before a character is even created?
Zeus
December 28th, 2011, 23:37
A little unsure as I can't see the whole XML/LUA you are referring to however, try wrapping the variable declaration with a conditional check, i.e.
local posn = 0;
if position and position[1] then
posn = position[1];
end
Also, this will only work if the script is part of the window class' script block. If the calling script is from within a control within the window class you need to call
posn = window.position[1];
JMOxx75
December 29th, 2011, 01:08
FYI, I am attempting to use scripts that were in the Jotss extension for the base ruleset in the foundation_core ruleset. Murgh Bpurn was the person who made the Jotss extension. It appears that the foundation_core ruleset may be supported more and thats why I'm moving it over. Also I am just trying to learn how to do this stuff and I figured making a character sheet extension for PDQ system in the foundation_core ruleset would be a start especially since most of the scripting was done by Murgh Bpurn already in the Jotss ext.
Script Error: [string "scripts/template_tabcontrol.lua"]:149: attempt to call global 'addBitmapWidget' (a nil value)
Script Error: [string "scripts/template_DropDown.lua"]:26: attempt to index global 'position' (a nil value)
Script Error: [string "scripts/minisheet_snapper.lua"]:210: attempt to index local 'window' (a nil value)
Those are my errors. The second one is the one i'm working on. The template snippet in the charsheet_main.xml file looks like this:
<DropDown name="quality_1_modifieddropdown">
<target>quality_1_modified</target>
<position>0,4</position>
<lookup>Quality</lookup>
<script file="scripts/template_DropDown.lua"/>
</DropDown>
The function on line 26 looks like this:
function onInit()
local posn = position[1];
local i = string.find(posn,",");
local targetname = "";
if lookupFieldName and lookupFieldName[1]~="" then
fieldname = lookupFieldName[1];
end
All its trying to do is get the <position>0,4</position> values out of that field. But for some reason it thinks that position[1] is a global?
Also I can't tell by looking at the script exactly what position it is looking for, cursor, window...?
Trenloe
December 29th, 2011, 02:17
All its trying to do is get the <position>0,4</position> values out of that field. But for some reason it thinks that position[1] is a global?
I'm not 100% familiar with this, but the reference document "Accessing XML parameters from script" section of this page (https://www.fantasygrounds.com/modguide/scripting.xcp) says that indices are only used if the XML tag has a child tag, if it doesn't then the contents are represented as a string value.
Therefore, I'd say just use "position" to return "0,4" (the string value of the tag), not "position[1]" as this is not valid - and hence why you're getting the error.
JMOxx75
December 29th, 2011, 17:47
Is there a more robust explanation of how extensions interact with rulesets, more importantly how the design of a ruleset can interfere with the functioning of an extension.
Zeus
December 30th, 2011, 10:35
Is there a more robust explanation of how extensions interact with rulesets, more importantly how the design of a ruleset can interfere with the functioning of an extension.
The only guide (https://www.fantasygrounds.com/modguide/extensions.xcp) that I am aware of is in the Ruleset Modification Guide in the Library however it may not cover the detail you are after. Take a look and if you are still struggling, post up a few specific questions and we'll see if we can answer them for you.
JMOxx75
December 30th, 2011, 18:39
What does the (...) mean?
<script> function onDrag(...) return window.onDrag(...); end function onDoubleClick(...) return window.onDoubleClick(...); end </script>
Zeus
December 30th, 2011, 19:09
Dot notation is used for relative addressing of database nodes.
In your example, ... is used to refer to the database node three levels up from the current node.
Other examples:
. - refers to current node
.. - refers to two levels up or parent of current node
... - refers to three levels up or grandparent of current node
StuartW
January 1st, 2012, 03:55
DrZeuss is almost right: the "." and ".." strings refer to the current database node and its parent, respectively, but three dots have a special meaning in Lua. When Lua calls a function, you can pass any number of arguments, and the three dots mean "however many arguments were received in this function call, pass them all on to a sub-function call".
In this case the drag event is passed up to the parent window, and whatever arguments were in the original event call are passed straight through. That means the event script doesn't need to know or care about the arguments it receives.
Stuart
Zeus
January 1st, 2012, 10:11
@StuartW - Thanks for correcting my explanation. I did not know of the specific ... functionality. My only LUA experience comes from coding for FGII and PSN and I have never used the ... notation myself.
Thinking about it, I should have guessed this as onDrag method doesn't take any database nodes as parameters. Good to know for the future.
Happy New Year BTW.
Moon Wizard
January 1st, 2012, 20:27
A little bit more information on the availability of variables within the LUA scripts.
Each window, control or global script has a unique instance; and its own set of variables. These variables can be managed by the scripts. (added, modified, deleted)
Any variables defined as "local" can only be accessed within a script instance.
Any variables not defined as "local" can be accessed by any script within the ruleset given that you can get a reference to the object that owns that script instance.
For windows and controls, any XML tags specified in the windowclass or control definition will be instantiated as LUA variables for the script instance of that object. (i.e. anchored, bounds, font, etc.)
Cheers,
JPG
Powered by vBulletin® Version 4.2.1 Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.