PDA

View Full Version : XML Code Problem - Calculating Fields



Meatball
May 23rd, 2006, 04:27
Alright guys, I'm trying to work out some fields on a custom character sheet. I'm attempting to calculate a field by taking one field and dividing it by another field that was already calculated as an average.

Here's the basics. In the game system I use there is something called "Encumbrance" It's a penalty to skills that is calculated by taking the total weight or load you are carrying and dividing that by your endurance. Endurance is calculated by averaging three attributes, Strength, Stamina and Willpower.

My eyes are going a bit wonky looking at the code, so maybe someone can see something.

First off, the code that calculates the total load carried appears to be working correctly and everythind adds up fine on the sheet.

<numbercontrol name="TotalLoad">
<bounds rect="233,461,26,16" />
<readonly />
<nodrop />
<nodrag />
<value type="readonly" />
<nokeyedit />
<hideonvalue value="0" />
<font name="smallcontrol" />
<source name="WeaponWeight" op="+" />
<source name="ArmourWeight" op="+" />
<source name="Weight1" op="+" />
<source name="Weight2" op="+" />
<source name="Weight3" op="+" />
</numbercontrol>

Next, I have the calculation that averages the three attributes to calculate Endurance working correctly and it is showing up as expected on the character sheet. (Thanks to Doc4's post on averaging (https://www.fantasygrounds.com/forums/showthread.php?t=3149&highlight=averaging) that helped me to get this working.)

<!-- :Set up Dummy Variable to be used with Average "function" -->

<numbercontrol name="dummy">
<invisible />
<value type="readonly" />
</numbercontrol>

<!-- Endurance is made up of 3 stats, set up dummy variable "3" to hold the # 3. -->

<numbercontrol name="3">
<invisible />
<value type="readonly" />
<source name="dummy" op="+" valuemap="0:3" />
</numbercontrol>

<!-- Endurance - Add Strength + Stamina + Will Then Divide by "dummy" variable of 3 -->

<numbercontrol name="Endurance">
<bounds rect="231,286,26,15" />
<font name="smallcontrol" />
<center />
<nodrop />
<noreset />
<value type="readonly" />
<source name="Strength" op="+" />
<source name="Stamina" op="+" />
<source name="Will" op="+" />
<source name="3" op="/" />
<description text="Endurance" />
<hideonvalue value="0" />
</numbercontrol>

So far, everything looks good. But now when I attempt to add the code to calculate Encumbrance by taking the total load and divide it by the endurance, FG crashes as soon as I try to create a new character sheet. Here's the code:

<numbercontrol name="Encumbrance" >
<bounds rect="233,493,26,16" />
<readonly />
<nodrop />
<nodrag />
<value type="readonly" />
<nokeyedit />
<hideonvalue value="0" />
<font name="smallcontrol" />
<source name="TotalLoad" op="+" />
<source name="Endurance" op="/" />
</numbercontrol>

Any help/thoughts would be mucho appreciated. Thanks!

Oberoten
May 23rd, 2006, 08:20
Division by zero I think. If initial value is zero and you divide with it, it won't work. Anyone have a solution on how to make it non-zero if zero without throwing the entire equation down the drain?

Meatball
May 23rd, 2006, 12:44
Yep, that's definitely it, since totalload is 0 on a new sheet, it crashes.

I commented out the Encumbrance code. Opened up a new sheet and put data into STR, Stamina, Will and added some values to weight. Closed it out and uncommencted the code. When I opened it back up, Encumbrance was calculating correctly now.

Not too sure how to make it work with a new sheet though.

Oberoten
May 23rd, 2006, 12:57
Yep, that's definitely it, since totalload is 0 on a new sheet, it crashes.

I commented out the Encumbrance code. Opened up a new sheet and put data into STR, Stamina, Will and added some values to weight. Closed it out and uncommencted the code. When I opened it back up, Encumbrance was calculating correctly now.

Not too sure how to make it work with a new sheet though.

I'd make the initial value into 1 for now. I am sure with the new script system in 1.6 we'll have the tools needed to avoid Div/0 :)

Meatball
May 23rd, 2006, 16:28
What's the syntax to initialize that before the calculation?

Oberoten
May 23rd, 2006, 22:44
<source name="TotalLoad" valuemap="0:1" />

Add that line to your function before doing the division and it should work I think... Haven't had the chance to test it properly really.

Meatball
May 24th, 2006, 02:01
Thanks for all the help Obero! Much appreciated. Where specifically should I initialize that, I tried it as follows, and it still crashed out on me.

<numbercontrol name="Encumbrance" >
<bounds rect="233,493,26,16" />
<readonly />
<nodrop />
<nodrag />
<value type="readonly" />
<nokeyedit />
<hideonvalue value="0" />
<font name="smallcontrol" />
<source name="TotalLoad" valuemap="0:1" />
<source name="TotalLoad" op="+" />
<source name="Endurance" op="/" />
</numbercontrol>

Toadwart
May 24th, 2006, 02:01
this might work (though I'm actually not sure if the valuemap is applied to the "Endureance" value before it is used in the division OR to the result of the division...?)



<numbercontrol name="Encumbrance" >
<bounds rect="233,493,26,16" />
<readonly />
<nodrop />
<nodrag />
<value type="readonly" />
<nokeyedit />
<hideonvalue value="0" />
<font name="smallcontrol" />
<source name="TotalLoad" op="+" />
<source name="Endurance" op="/" valuemap="0:1" />
</numbercontrol>




If that doesn't work then this will:


<numbercontrol name="Endurance">
<bounds rect="231,286,26,15" />
<font name="smallcontrol" />
<center />
<nodrop />
<noreset />
<value type="readonly" />
<source name="Strength" op="+" />
<source name="Stamina" op="+" />
<source name="Will" op="+" />
<source name="3" op="/" />
<description text="Endurance" />
<hideonvalue value="0" />
</numbercontrol>


<numbercontrol name="EnduranceNonZero" >
<source name="Endurance" valuemap="0:1" />
</numbercontrol>

<numbercontrol name="Encumbrance" >
<bounds rect="233,493,26,16" />
<readonly />
<nodrop />
<nodrag />
<value type="readonly" />
<nokeyedit />
<hideonvalue value="0" />
<font name="smallcontrol" />
<source name="TotalLoad" op="+" />
<source name="EnduranceNonZero" op="/" />
</numbercontrol>

Meatball
May 24th, 2006, 03:05
Looks like the first one worked, adding the valuemap seems to allow it to run correctly before any values are put in (it dumps 1 in there), and if values are in, it doesn't appear to affect the actual number.

Thanks to you both!

Toadwart
May 24th, 2006, 10:25
No worries.
Incidentally, I don't recognise the <nokeyedit /> node in your first post and it doesn't seem to be used in any of the default d20 xml files.
Is it a valid node? and what does it do?

Meatball
May 24th, 2006, 12:25
Not sure...Crusader actually wrote most of the ruleset I'm working on. I haven't made any changes to that section, so I'm not too sure what that function actually does.

Crusader
May 25th, 2006, 21:11
In fact, neither do I. When I started out with my ruleset I basically just took the existing d20-ruleset and copied and pasted and modified until it looked like I wanted it to. There's probably a lot of weirdness in that code. :D

Meatball
May 29th, 2006, 01:18
Heh, I'm sure I didn't help.