PDA

View Full Version : Back again with another question



mixologee
September 22nd, 2021, 02:20
So I am trying to loop through a windowlist item. I have the code being called correctly (although more times than I would have thought). I am able to get the list object into a variable as well:


databasenode = { npc.id-00002.npcdrlist }

However, when I try to print out the list items, I get a nil error:



function npcDRCalc(source)
Debug.chat("I'm in npcDRCalc");
Debug.chat(source);
if source then
local sPCNodeName = ActorManager.getCreatureNodeName(source);
Debug.chat("Node Name");
Debug.chat(sPCNodeName);
local vDR = DB.getChild(sPCNodeName, "npcdrlist"); <---- Line 45
Debug.chat("After getting child");
Debug.chat(vDR);
Debug.chat(vDR.getWindows());
end
end




Script Error: [string "campaign/scripts/NPCSheetManager.lua"]:45: attempt to call field 'getWindowCount' (a nil value)
Script Error: [string "campaign/scripts/NPCSheetManager.lua"]:45: attempt to call field 'getWindowCount' (a nil value)
Script Error: [string "campaign/scripts/NPCSheetManager.lua"]:45: attempt to call field 'getWindowCount' (a nil value)
Script Error: [string "campaign/scripts/NPCSheetManager.lua"]:45: attempt to call field 'getWindowCount' (a nil value)



I though the windowlist had a built in function to get all the items from the list. I'm sure its something I am doing wrong, but can't quite put my finger on it. Thanks in advance.

Trenloe
September 22nd, 2021, 02:25
There's no "getWindowCount" in the code you list. Is that the actual code you're running - does that cover line 45 in campaign/scripts/NPCSheetManager.lua ?

EDIT: Sorry, I just saw your "<---- Line 45" comment. Do you have a simple extension, with steps to reproduce the error?

mixologee
September 22nd, 2021, 02:31
Basically I have a an windowlist object with the following code attached:



function onInit()
onListChanged();
end

function onListChanged()
local nodeWin = window.getDatabaseNode();
NPCSheetManager.npcDRCalc(nodeWin);
end


The NPCSheetManager is just a lua script with a function called npcDRCalc which is below:



function npcDRCalc(source)

Debug.chat("I'm in npcDRCalc");
Debug.chat(source);

if source then

local sPCNodeName = ActorManager.getCreatureNodeName(source);
Debug.chat("Node Name");
Debug.chat(sPCNodeName);
local vDR = DB.getChild(sPCNodeName, "npcdrlist");
Debug.chat("After getting child");
Debug.chat(vDR);
Debug.chat(vDR.getWindows());
end
end


I can probably create a quick extension if that would be easier, but wont be able to get to it until tomorrow.

mixologee
September 22nd, 2021, 13:48
Here is a ruleset to test with that just has the issue in it. Just create a new NPC and then add an item to the list.

Moon Wizard
September 22nd, 2021, 15:43
You're calling getWindowCount on a databasenode object (or possibly nil depending on return result); which is not a valid API.

I recommend against "chaining" function calls like you are doing in Line 48 of NPCSheetManager in general. By breaking apart, you can test variables by using Debug.chat(<variable>) to make sure that the objects are the type/value that you are expecting at that point in the code. Also, make sure to review the Developer API reference for valid API calls for each object type.

https://fantasygroundsunity.atlassian.net/wiki/spaces/FGCP/pages/996644535/Developer+Guide+-+Ruleset+API+Reference

Regards,
JPG

Trenloe
September 22nd, 2021, 15:56
Here is a ruleset to test with that just has the issue in it. Just create a new NPC and then add an item to the list.
Thanks for providing that. The code provided has different API calls to that listed in your posts above. Hence my comment in post #2: "There's no "getWindowCount" in the code you list. Is that the actual code you're running..." I was confused because the error was for the API command getWindowCount, but that wasn't in your code snippets provided.

So, as Moon Wizard has indicated, the code in the ruleset is attempting to use getWindowCount against a database node - which is invalid.


function npcDRCalc(source)

Debug.chat("I'm in npcDRCalc");
Debug.chat(source);

if source then

local sPCNodeName = ActorManager.getCreatureNodeName(source);
Debug.chat("Node Name");
Debug.chat(sPCNodeName);
local vDR = DB.getChild(sPCNodeName, "npcdrlist");
Debug.chat("After getting child");
Debug.chat(vDR);
local aInfo = DB.getChild(sPCNodeName, "npcdrlist").getWindowCount();
Debug.chat(aInfo);
end
end

getWindowCount can only be used against a GUI windowlist control: https://fantasygroundsunity.atlassian.net/wiki/spaces/FGCP/pages/996645260/windowlist#getWindowCount

As your code is working against the database, you can't use GUI based API calls, but you could use getChildCount to see how many children npcdrlist has: https://fantasygroundsunity.atlassian.net/wiki/spaces/FGCP/pages/996644722/databasenode#getChildCount But, as Moon Wizard mentions, you should have code that ensures the databasenode you're using getChildCount against exists, otherwise you'll get an error. You could do something like this:


local nodeNPCDRList = DB.getChild(sPCNodeName, "npcdrlist");
if nodeNPCDRList then
local nListCount = nodeNPCDRList.getChildCount();
end

mixologee
September 22nd, 2021, 17:12
Ah ok, makes sense now. I knew it was something I was doing wrong. Thanks both of you for the help. Will make this thing move along much easier I think.