PDA

View Full Version : CombatManager.getTokenFromCT unexpectedly returning nil



darrenan
September 26th, 2021, 23:30
As part of the mook support I'm writing for the 13th Age ruleset, I thought it would be cool to be able to double click the mook mob NPC in the combat tracker to reset the image viewpoint to contain all the mooks, and to highlight them. Here is the overridden onDoubleClick function:


function onDoubleClick(x, y)
GlobalDebug.debugString("Token.onDoubleClick");

local bIsMookMob = (DB.getValue(window.getDatabaseNode(), "ismookmob", 0) ~= 0);
if not bIsMookMob then
return super.onDoubleClick(x, y);
end

local sMookMobPath = window.getDatabaseNode().getPath();
GlobalDebug.debugFormatted("Starting mook mob double click handling for %s", sMookMobPath);

local aTokenContainers = {};
for k,nodeCTMook in pairs(CombatManager.getCombatantNodes()) do
local sNodeMookMobPath = DB.getValue(nodeCTMook, "mookmob", "");
GlobalDebug.debugFormatted("sNodeMookMobPath for %s = %s", nodeCTMook.getPath(), sNodeMookMobPath or "nil");

if sMookMobPath == sNodeMookMobPath then
GlobalDebug.debugFormatted("found a match: node=%s refid=%s", DB.getValue(nodeCTMook, "tokenrefnode", ""), DB.getValue(nodeCTMook, "tokenrefid", ""));
local tokeninstance = CombatManager.getTokenFromCT(nodeCTMook);
if tokenInstance then
GlobalDebug.debugFormatted("Found mook with tokenInstance: id=%d", tokenInstance.getId());

local imageControl = ImageManager.getImageControl(tokenInstance, true);
if imageControl then
local sImageNodePath = imageControl.getDatabaseNode().getPath();
if not aTokenContainers[sImageNodePath] then
local width, height = imageControl.getImageSize();
aTokenContainers[sImageNodePath] = { image = sImageNodePath, xMin = width + 1, xMax = -1, yMin = height + 1, yMax = -1 };
end

local tokenX, tokenY = tokenInstance.getPosition();
aTokenContainers[sImageNodePath].xMin = math.min(aTokenContainers[sImageNodePath].xMin, tokenX);
aTokenContainers[sImageNodePath].xMax = math.max(aTokenContainers[sImageNodePath].xMax, tokenX);
aTokenContainers[sImageNodePath].yMin = math.min(aTokenContainers[sImageNodePath].yMin, tokenY);
aTokenContainers[sImageNodePath].yMax = math.max(aTokenContainers[sImageNodePath].yMax, tokenY);

if tokeninstance.isActivable() then
-- Set this mook active
tokeninstance.setActive(not tokeninstance.isActive());
end
end
end
end
end

-- Set the image view to contain all mooks.
for _,v in pairs(aTokenContainers) do
v.image.setViewpointCenter((v.xMin + v.xMax)/2, (v.yMin + v.yMax)/2);
end
end

And here is the output of the debugging statements in the above function when I try to run it:

49289

You can see that it successfully finds the CT node, which has valid tokenrefnode and tokenrefid data. But the next call to CombatManager.getTokenFromCT returns nil and I'm not sure why. Any ideas?

db.xml: 49291

Moon Wizard
September 28th, 2021, 02:29
This is ultimately the call made by that CombatManager.getTokenFromCT function. Maybe output the results of each of the pieces?

What do you get on this output?
Debug.chat(Token.getToken(DB.getValue(nodeCT, "tokenrefnode", ""), DB.getValue(nodeCT, "tokenrefid", "")))

If getting nil, then check in the database for image.id-00002-image in Cobblestone City.xml within moduledb of campaign after saving. Do you see that as a token in the list?

Regards,
JPG

darrenan
September 28th, 2021, 18:21
The highlighted line in the code snippet is outputing the values you're asking about to the chat window, and the output of that line is shown in the subsequent screenshot. All the values appear to be valid. I also attached db.xml for reference.



<?xml version="1.0" encoding="utf-8"?>
<root version="4.1" dataversion="20210708">
<image>
<id-00002>
<image type="image">
<allowplayerdrawing>on</allowplayerdrawing>
<grid>on</grid>
<gridsize>100,100</gridsize>
<gridoffset>998,1201</gridoffset>
<color>#66FFFFFF</color>
<brushsize>10,10</brushsize>
<layers>
<layer>
<name>CobblestoneCity-CrossStreets-100ppi[17x22].jpg</name>
<id>0</id>
<parentid>-2</parentid>
<type>image</type>
<bitmap>CobblestoneCity-CrossStreets-100ppi[17x22].jpg@Cobblestone City</bitmap>
<matrix>1,0,0,0,0,1,0,0,0,0,1,0,850,-1100,0,1</matrix>
<locked>true</locked>
</layer>
<layer>
<name>tokens/Orc_Assassin_Dagger.png@13A_Bestiary</name>
<id>6</id>
<parentid>-1</parentid>
<type>token</type>
<token>
<name>Cave Orc 1</name>
<prototype>tokens/Orc_Assassin_Dagger.png@13A_Bestiary</prototype>
<id>6</id>
<color>#FFFF0000</color>
<visibility>mask</visibility>
</token>
<matrix>0.1999512,0,0,0,0,0.1999512,0,0,0,0,1,0,848,-751,0,1</matrix>
</layer>
</layers>
</image>
</id-00002>
</image>
</root>

Moon Wizard
September 28th, 2021, 19:52
I'd need an exact set of files as well as an example campaign with steps to recreate to look any closer. I'm assuming that Token.getToken is returning nil; which most likely means that something isn't mapped correctly or there is an order of operations scenario (i.e. token created after check, etc.)

JPG

jharp
September 28th, 2021, 20:25
In my experience you cannot index past the <image type="image"> object. It is blocked on purpose if I understand correctly.
Jason

darrenan
September 28th, 2021, 22:29
I'd need an exact set of files as well as an example campaign with steps to recreate to look any closer. I'm assuming that Token.getToken is returning nil; which most likely means that something isn't mapped correctly or there is an order of operations scenario (i.e. token created after check, etc.)

JPG

Ok, I have sent you a dropbox link containing the files in question, let me know via email or pm if there's anything else you need.