PDA

View Full Version : onSourceUpdate() question...



Zhern
December 29th, 2016, 04:28
I've been reading through the scripting reference, looking through various other rulesets to see if the way I'm attempting to use it is wrong (results inconclusive still), and searching through previous forum posts, and I've not yet been able to identify what I'm doing wrong. Keep in mind, this is the first time I'm trying to start hooking up functionality to the new character sheet for the S&W ruleset, so be gentle, please. Also, I'm a C/C++/Java guy, so lua is still very new to me.

I'm calling the script inline in the xml. When the dexterity score is change, the ac modifier should be updated to reflect that new score. For example, if dexterity is 14, then the character receives a +1 to their AC (or -1 if using descending AC). I'm linking the dexacmod field to the source. Anyway, here is my code and error:



<number_linked name="dexacmod">
<anchored to="acframe" position="insidetopleft" offset="17,110" width="70" height="20" />
<displaysign />
<readonly />
<frame name="fieldlight" offset="7,5,7,5" />
<source>
<name>abilities.dexterity.score</name>
</source>
<script>
function onSourceUpdate(source)
if (source &ge; 13) then
window.dexacmod.setValue(1);
elseif (source &ge; 9 and source &le; 12) then
window.dexacmod.setValue(0);
else
window.dexacmod.setValue(-1);
end
end
</script>
</number_linked>


Error: Script Error: [string "charsheet_main:dexacmod"]:1: ')' expected near 'ge'

A nudge in the right direction would be appreciated. Thanks in advance.

Patrick/Zhern

Zhern
December 29th, 2016, 04:33
I'm also wondering if maybe I should be using onValueChanged() and stick the code under the dexterity control...

Trenloe
December 29th, 2016, 04:40
&ge; is not a valid entity reference in XML. See "Entity References" here: https://www.w3schools.com/xml/xml_syntax.asp

Assuming you want to use greater than or equal? then use &gt;=

vodokar
December 29th, 2016, 04:44
Rather than try to debug what you have, let me offer this method that I know works.

<template name="number_charabilitydex2bonus">
<number_abilitybonus>
<anchored position="righthigh" offset="15,0" width="36" height="20" />
<hideonvalue value="0" />
<displaysign />
<modifiersize>mini</modifiersize>
<script>
function onSourceUpdate()
local nodeWin = window.getDatabaseNode();
local nCurrentScore = DB.getValue(nodeWin, "abilities." .. target[1] .. ".score", 10);
setValue(GameSystem.getAbilityDexACBonus(nCurrentS core) + calculateSources());
end

function onInit()
addSource("abilities." .. target[1] .. ".score", 10);
super.onInit();
end
</script>
</number_abilitybonus>
</template>

On Init, it add the source that you want to monitor.
On Source Update, it gets the value of that source and looks up the value of the bonus on a table and then sets the bonus of your number field to that bonus value.

-- Table for Dexterity AC Bonus
function getAbilityDexACBonus(c)
if c < 4 then
return 4;
elseif c < 5 then
return 3;
elseif c < 6 then
return 2;
elseif c < 7 then
return 1;
elseif c < 15 then
return 0;
elseif c < 16 then
return -1;
elseif c < 17 then
return -2;
elseif c < 18 then
return -3;
elseif c < 19 then
return -4;
else
return -4;
end
end

An alternate way to do your OnInit script would be to use a database handler like this:

function onInit()
local nodeWin = window.getDatabaseNode();
DB.addHandler(DB.getPath(nodeWin, "abilities.strength.score"), "onUpdate", onSourceUpdate);
DB.addHandler(DB.getPath(nodeWin,"abilities.strength2.score"), "onUpdate", onSourceUpdate);
super.onInit();
end

That is actually a more powerful method applicable to more situations, I have found. That should give you some things to play around with.

Trenloe
December 29th, 2016, 04:49
My recommendation would be to do any onUpdate code against the underlying database - the controls run on top of this and so DB code will run when the control is updated, plus it allows you to update the database directly and still have the same logic process. It's a bit of extra work to get used to database access and functions, but it will save you so much work in the long run to process data at the database level.

I mention it with vodokar in the AD&D development here: https://www.fantasygrounds.com/forums/showthread.php?34976-AD-amp-D-Ruleset&p=302816&viewfull=1#post302816 and in various posts around that. It might "seem" to be easier to do it all in the GUI now, but my strong recommendation would be to do this against the database node the control is sourced on.

vodokar
December 29th, 2016, 04:53
If I understand what your saying, I agree. ;) The way I interpret what you said is that using the database handler is better. Correct? Once you taught me that method, I used it almost exclusively, but didn't go back and change the previous code that already was functioning.

Trenloe
December 29th, 2016, 04:56
The way I interpret what you said is that using the database handler is better. Correct?
Correct.

Zhern
December 29th, 2016, 04:58
Thanks, guys. I'll read through all this tomorrow when I'm more coherent and check out what you said in the AD&D ruleset thread. I know I'm going to have to refactor a lot of stuff as I go and I consume the best practices that you all have to offer. Thanks again.

damned
December 29th, 2016, 04:58
There is a reason AC was not auto-calc'd in 5e for a long time and thats because so many different things could modify it.
Be aware that you might need to ensure that you are getting the AC from the Armour and from any magic items and from any spells and then you are doing your DEX modifier....
If (and it looks like you might be doing this) you are just putting the AC mod in a field automatically then that is much simpler....

Zhern
December 29th, 2016, 05:01
There is a reason AC was not auto-calc'd in 5e for a long time and thats because so many different things could modify it.
Be aware that you might need to ensure that you are getting the AC from the Armour and from any magic items and from any spells and then you are doing your DEX modifier....
If (and it looks like you might be doing this) you are just putting the AC mod in a field automatically then that is much simpler....

Yeah, just putting the AC Mod in a field. There is also a field for AC from armor and AC from shield. After all of that, then I'll calculate the AC. I want it to be very intuitive to players, new player especially, how the AC is arrived at.

vodokar
December 29th, 2016, 05:02
"Thanks, guys. I'll read through all this tomorrow when I'm more coherent and check out what you said in the AD&D ruleset thread. I know I'm going to have to refactor a lot of stuff as I go and I consume the best practices that you all have to offer. Thanks again."

Ah, the brain burn. I remember that feeling. Good Stuff. It will feel great when the light bulb comes on.

Zhern
December 29th, 2016, 05:06
Yeah, it has been a long day. Write/debug code all day at work, come home and do the same.

vodokar
December 29th, 2016, 05:19
You've definitely got the edge on coding experience, but as many people have said, rpg game logic is another animal. Likely, some of things you know from your day job might actually interfere with processing the new stuff.

Zhern
December 29th, 2016, 12:12
Yeah, quite a bit is interfering, lol. I'm used to enterprise systems and architecture. This is architected a much different way and is taking me some time to wrap my head around.

Zhern
December 29th, 2016, 16:44
Okay, things are a bit less muddy now - still not 100% on the DB handler but I will eventually wrap my head around it. In the few moments I had to work on it this morning, I got it working, largely because you all were so very helpful. I'm sure I'll have more questions as we go.