PDA

View Full Version : [CoreRPG] Force a field update



Maspalio
November 11th, 2019, 11:28
Hello,

I am stuck with a problem that I can't find a solution to, mainly because of a lack of knowledge.

Let’s say i have a field called Strength, with a numerical value, i.e 6. This field has a source=«strength.value» name that i use to call this value later. I got a second field too on the same character sheet, wich has to build from this Strength value, i.e. Base Strength value+1. To do this, i have a lua file with the formulae, calling the Strength via getDatabaseNode().getChild().getvalue.

The problem appears when i change and update the base Strength value of 6 to another one, i.e. 8: the second field doesn’t refresh accordingly, despite the setValue and function onInit used. It stays at 6+1 and i have to reload the entire ruleset to see the second field updated to 8+1.

What am i missing or doing wrong?

Thanks by advance

Moon Wizard
November 11th, 2019, 18:47
Database fields have no built-in "linking" capability. This has to be done by registering update handlers in your field that wants to be updated for the field that it wants to watch.

The number_linked template that is in CoreRPG does this by allowing you to specify <source> tags when you use the template, which it then adds together. There are many examples in 5E and 3.5E of this template being used.

Or you can build your own by using the DB.addHandler/removeHandler APIs or databasenode.onUpdate API.

Regards,
JPG

Maspalio
November 13th, 2019, 12:10
Thanks Moon. Your reply is still obscure to me, but i'll take a look at 5E and 3,5E alike.

Maspalio
November 19th, 2019, 14:09
Hi again.

I did some tries using the <numberlink> and <source> tags. Unfortunately, i don't get the result i want and i'm a bit confused. Here is the code, if someone could lend me a hand.


Here is the field with the number that serves as a refence:


<number name="morality_score" source="morality.score">
<anchored to="morality_frame" position="insidetopleft" offset="127,40" width="25" height="20" />
<max>10</max>
<tooltip textres="char_tooltip_edit_or_roll" /> <!-- Changer texte -->
</number>



Then, here is the second field that must get the score of the above field, each time it is modified:


<number_linked name="alivetrait">
<anchored to="huntingframe" position="insidetopleft" offset="127,40" width="24" height="18" />
<frame name="fieldgrey" offset="7,5,7,5" />
<script file="campaign/npc/scores_health.lua" />
<readonly />
<source>
<name>morality.score</name>
<op>+</op>
</source>
</number_linked>



Each time this is done, i got a numerical operation to perform, wich is (8 -the score of the first reference field).
This operation is called via a .lua file loaded through the <script> tag in the second field above (<script file="campaign/npc/scores_health.lua" />)


function onInit()
local nMorality = 0;
local nMorality = getDatabaseNode().getChild("morality.score").getValue();

local alivetrait = 8-nMorality;
if (alivetrait < 0) then
alivetrait = 0; (or == 0; ?)
end

getDatabaseNode().getChild("alivetrait").setValue(alivetrait);
end



The main problem is the script is not used as i update the field with a new value...

Thanks by advance for your help

Trenloe
November 19th, 2019, 15:57
The main problem is the script is not used as i update the field with a new value...
As Moon Wizard mentions, there is no automatic linking. You need to set up an onUpdate event handler against the underlying database field.

See details on event handlers here: https://www.fantasygrounds.com/wiki/index.php/Developer_Guide_-_Rulesets_-_Scripting#Using_Handlers

I haven't checked all of your code, as I'm not aware of your complete setup regarding the window/control and database hierarchy, so the following code may still need some tweeking to make it work within your setup. For example, I'd probably change the number_linked control to a numberfield instead - with this code there's no need to use the number_linked template. But this is the gist of what you're looking for (code in the alivetrait control):


function onInit()
-- Get the database node for morality.score database field.
local moralityNode = getDatabaseNode().getChild("morality.score");

-- This sets the onUpdate event handler for the moralityNode to run the updateAlivetrait code
moralityNode.onUpdate = updateAlivetrait;
end

function updateAlivetrait(source)
-- Gets the new value of the source of the onUpdate event
local nMorality = source.getValue();

local nAlivetrait = 8 - nMorality;
if (nAlivetrait < 0) then
nAlivetrait = 0;
end

-- Set the value in the control
setValue(nAlivetrait);
end