5E Character Create Playlist
  1. #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>
    I'm not even sure if the 'trunc' is correct, but even removing it has strange results. The total AC values differ when starting FG up from scratch and after the armor bonus values are modified. If the <op>+</op> is removed from the armor bonus source, no changes are made to AC. If it's left there, it simply adds the value.

    Any ideas where I'm going wrong?

    Zakarius

  2. #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 &GT; 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.

  3. #3

    Join Date
    Jul 2006
    Location
    Taichung, TAIWAN
    Posts
    50
    Quote Originally Posted by joshuha
    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 &GT; it will help.
    Actually, that code with the 'greater than' sign is packaged with the original FG II and seems to work fine. The code I added begins with:

    if sourcename == "ac.sources.armor" then

    Quote Originally Posted by joshuha
    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.
    Thanks a lot... it worked like a charm!

    Zakarius
    Last edited by Zakarius; July 19th, 2007 at 06:14.

  4. #4
    Foen's Avatar
    Join Date
    Jan 2007
    Location
    Suffolk, England
    Posts
    2,007
    Instead of math.floor((armor + 1)/2) the formula math.ceil(armor/2) is slightly neater. Math.ceil rounds up.

    Stuart
    (Foen)

  5. #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 &lt;.

  6. #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>

  7. #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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
5E Product Walkthrough Playlist

Log in

Log in