PDA

View Full Version : Trying to understand DB.getvalue(node,"link"...) behavior in 2E



MostTornBrain
March 10th, 2022, 23:37
Hi,

I'm working on an extension for 2E that overrides the onLinkChanged() function in cta_entry.lua. Specifically, the existing 2E function is:


function onLinkChanged()
local node = getDatabaseNode();
-- If a PC, then set up the links to the char sheet
local sClass, sRecord = DB.getValue(node,"link","","");
--Debug.console("cta_Entry.lua","onLinkChanged","sClass",sClass);
if sClass == "charsheet" then
linkPCFields(DB.findNode(sRecord));
elseif sClass == "npc" then
-- linkNPCFields(DB.findNode(sRecord));
end
--onIDChanged();
end

and I am basically copying this function and adding some extra functionality on the "elseif" for "npc".

What confuses me: if I drag an NPC onto the combat tracker, my function gets called, but the sClass value returned from DB.getValue(node,"link","","") is nil. However, if I then save and reload my campaign, this same NPC returns sClass of "npc" when onLinkChanged() is called. What would be different between the active drag and drop of an NPC vs. reloading the campaign?

Is there any way I can get the equivalent of the sClass value when the drag and drop onto the combat tracker first happens? Obviously having to reload the campaign to get onLinkChanged() to detect an NPC is a bit unwieldy in regular use. Basically, I'm trying to know when an NPC is dropped onto the combat tracker so my extension can do some special handling of it, but my special handling only gets triggered if I reload the campaign.

Thanks for any ideas.

Cheers,
Brian

MostTornBrain
March 11th, 2022, 00:34
I think I figured out the issue. As usual, I misunderstood something that was happening elsewhere in the code. I'll followup with a better explanation once I am certain of the details, but I am posting a reply now as I didn't want anyone wasting their time with a reply when I think I am on the way to now figuring out my issue.

Cheers,
Brian

MostTornBrain
March 11th, 2022, 03:31
It was a self-inflicted problem.

I had an override for the "combatants_entry_host" windowclass onInt() function and in that I was calling onLinkChanged(). For a drag and drop to the CT, onInit() was getting called in the middle of the CTA addCTANPC() function when it called DB.createChild(CombatManager.CT_LIST). I had been falsely assuming it was happening near the end of the function. The problem is, this happens _before_ the 'link' DB value between the NPC and CT is created, so it will always be nil at that time. It wasn't an issue when loading the campaign from a saved file since the CT entry wasn't being created in the same manner, so the chicken and egg problem wouldn't occur.

My hackish solution was to add an onFirstLayout() function for the "combatants_entry_host" windowclass and have that call onLinkChanged(), since at that point the full CT entry has been populated with all the DB values.

Cheers,
Brian