-
July 19th, 2007, 04:13 #1
- Join Date
- Jul 2006
- Location
- Taichung, TAIWAN
- Posts
- 50
XML math problem on custom charsheet...
In my campaign, half of your armor bonus (rounded up) goes to Armor Class and the other half goes to Damage Reduction. In the charsheet_combat.xml, I've modified the script as follows:
Code:<script> function onSourceUpdate() setValue(10 + calculateSources()); end function onSourceValue(source, sourcename) if sourcename == "abilities.dexterity.bonus" then local dex = source.getValue(); local max = sources["encumbrance.armormaxdexbonus"].getValue(); local maxactive = sources["encumbrance.armormaxdexbonusactive"].getValue(); if maxactive == 0 or max > dex then return dex; else return max; end else if sourcename == "ac.sources.armor" then local armor = source.getValue(); return trunc((armor + 1)/2); else return super.onSourceValue(source, sourcename); end end end </script>
Any ideas where I'm going wrong?
Zakarius
-
July 19th, 2007, 05:10 #2
One problem and it does make for unpredictable results is you are using a > sign with inline scripting. This is a big no-no as its an XML tag and probably truncating your code in the internal parser. I bet if you either put it in a file or use the escape > it will help.
As far as trunc are you trying to get just the integer part? If so math.floor() is the function you are looking for.
There's a note about this on https://fantasygrounds.com/modguide/scripting.xcp but it is not emphasised enough in my opinion.
-
July 19th, 2007, 06:12 #3
- Join Date
- Jul 2006
- Location
- Taichung, TAIWAN
- Posts
- 50
Originally Posted by joshuha
if sourcename == "ac.sources.armor" then
Originally Posted by joshuha
ZakariusLast edited by Zakarius; July 19th, 2007 at 06:14.
-
July 19th, 2007, 06:32 #4
Instead of math.floor((armor + 1)/2) the formula math.ceil(armor/2) is slightly neater. Math.ceil rounds up.
Stuart
(Foen)
-
July 19th, 2007, 08:16 #5
- Join Date
- Feb 2006
- Location
- Fairfax County, Virginia
- Posts
- 499
You can probably get away with > because it isn't ambiguous about whether it's part of a tag or not. You can't do it with <, and in fact, a < in an inline script doesn't occur anywhere in the default ruleset. It always occurs as <.
-
July 19th, 2007, 15:23 #6
- Join Date
- Jul 2006
- Location
- Taichung, TAIWAN
- Posts
- 50
A bug in FG or my code?
Thanks to your feedback, I've got my code working... kinda. When I change the Armor Bonus to 4, it immediately adds FOUR to the total AC though it's only supposed to add TWO because of the ruleset I'm using. If I close the character sheet and open it again, voila--it has added the proper +2 into the total AC (if you're curious, the other 2 points go to DR).
I've tried this in both host and client mode... same result. I've posted the relevant code for the AC tally below if anyone cares to sift through it... (or have other people experienced similar trouble with the normal d20 ruleset):
Code:<linkednumber name="ac" source="ac.totals.general"> <anchored> <to>acframe</to> <position>insidetopleft</position> <offset>20,25</offset> <size> <width>61</width> <height>32</height> </size> </anchored> <frame> <name>acicon</name> <offset>4,16,0,24</offset> </frame> <font>sheetnumber</font> <readonly /> <description> <text>Armor Class</text> </description> <source> <name>abilities.dexterity.bonus</name> <op>+</op> </source> <source> <name>ac.sources.armor</name> <op>+</op> </source> <source> <name>ac.sources.shield</name> <op>+</op> </source> <source> <name>attackbonus.grapple.size</name> <op>+</op> </source> <source> <name>attackbonus.base</name> <op>+</op> </source> <source> <name>ac.sources.naturalarmor</name> <op>+</op> </source> <source> <name>ac.sources.deflection</name> <op>+</op> </source> <source> <name>ac.sources.misc</name> <op>+</op> </source> <source> <name>ac.sources.temp.ac</name> <op>+</op> </source> <source> <name>encumbrance.armormaxdexbonus</name> </source> <source> <name>encumbrance.armormaxdexbonusactive</name> </source> <script> function onSourceUpdate() setValue(10 + calculateSources()); end function onSourceValue(source, sourcename) if sourcename == "abilities.dexterity.bonus" then local dex = source.getValue(); local max = sources["encumbrance.armormaxdexbonus"].getValue(); local maxactive = sources["encumbrance.armormaxdexbonusactive"].getValue(); if maxactive == 0 or max > dex then return dex; else return max; end else if sourcename == "ac.sources.armor" then local armor = source.getValue(); return math.floor((armor + 1)/2); else if sourcename == "ac.sources.naturalarmor" then local na = source.getValue(); return na-(math.floor(na/5)); else if sourcename == "attackbonus.base" then local bab = source.getValue(); return math.floor(bab/2); else return super.onSourceValue(source, sourcename); end end end end end </script> </linkednumber>
-
July 19th, 2007, 15:49 #7
- Join Date
- Jul 2006
- Location
- Taichung, TAIWAN
- Posts
- 50
Problem solved!
By removing all of the else .. if nesting, the code now works. I incorrectly assumed that if statements needed to check one after another, but the eventual return super.onSourceValue(source, sourcename) needn't be connected to the conditionals which all have their own return clause.
Zakarius
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks