PDA

View Full Version : Halves and Quarters



mr_h
June 30th, 2007, 20:29
In my ruleset I have an attribute (say, Strength) that is listed as 'Full', then 1/2, then 1/4th. How can I make it so that the character sheet automatically calculates the 1/2 and 1/4th bit? It might be obvious in Lua or something, but I dont know anything about the language :/


Here's what my code looks like so far:



<!-- Strength (setup the first number field)-->
<numberfield name="att_str">
<bounds>245,55,20,20</bounds>
<font>sheetnumber</font>
<frame>
<name>bonus</name>
<offset>5,4,5,4</offset>
</frame>
</numberfield>
<stringcontrol>
<bounds>165,60,60,20</bounds>
<font>sheetlabel</font>
<static>Strength</static>
</stringcontrol>

<!-- 1/2 Strength -->
<numberfield name="att_str_half">
<bounds>300,55,20,20</bounds>
<font>sheetnumber</font>
<frame>
<name>bonus</name>
<offset>5,4,5,4</offset>
</frame>
</numberfield>

<!-- 1/4 Strength -->
<numberfield name="att_str_quar">
<bounds>350,55,20,20</bounds>
<font>sheetnumber</font>
<frame>
<name>bonus</name>
<offset>5,4,5,4</offset>
</frame>
</numberfield>



Any suggestions? Thanks


PS: Anyone know the color code for 'Blue'? The <color> syntax won't take the hex numbers given to me by gimp (Gimp gives me 0000ff)

joshuha
June 30th, 2007, 21:40
Well, since these fields are always 1/2 and 1/4 is there a need to store them in the database as numberfields?

Using them as numbercontrols would probably make more sense. But basically you need an onInit function that takes the source value and multiplies it by .5 or .25.

This would work every time the sheet is opened. If you want it more fancy to work in realtime when the underlying stat is updated it requires writing a handler function on the update event for the source stat.

The simple way would look something like this:


<numbercontrol name="att_str_half">
<bounds>300,55,20,20</bounds>
<font>sheetnumber</font>
<frame>
<name>bonus</name>
<offset>5,4,5,4</offset>
</frame>
<script>
function onInit()
setValue(window.getDatabaseNode().getChild("att_str").getValue()*.5);
end
</script>
</numbercontrol>

Toadwart
June 30th, 2007, 22:19
In my ruleset I have an attribute (say,
PS: Anyone know the color code for 'Blue'? The <color> syntax won't take the hex numbers given to me by gimp (Gimp gives me 0000ff)

Try "#FF0000FF"

As-per the description for the setColor function in the lbrary: https://www.fantasygrounds.com/refdoc/numbercontrol.xcp#setColor

mr_h
July 1st, 2007, 03:41
Thanks Joshua, that works. It doesnt bother me to have it check when the charsheet is opened and no other time (this attribute shouldn't be changed anyhow). Is there a way to remove the decimal and numbers after that though? IE, round up (a 79 shows up as 39.5, but should just be a 40).
I know, probably a simple command, but an hour of looking and I can't find it.

Toadwart: That did the trick. Gimp doesn't give me all 8 numbers/letters, only 6. I wasnt able to guess the first two.

Dachannien
July 1st, 2007, 10:27
You can use the Lua math.floor() function to chop off everything after the decimal point. If you want to do traditional rounding, i.e., round up if the fractional part is 0.5 or more, then add 0.5 to the answer and then use math.floor().

mr_h
July 2nd, 2007, 02:21
Thanks, the math.floor function works. Couldn't get the adding .5 to it to work, but it does get rid of the annoying extra digits:)

Dachannien
July 2nd, 2007, 02:54
Really? Should be


y = math.floor(0.5 + x*0.5);

where x is the value you put in, usually whatever is returned by getValue().

There shouldn't be any rounding errors, since 0.5 is a (negative) power of two and can be stored exactly in a float, but if all of your x's are integers, you can add something just a hair bigger than 0.5 (like 0.51), just in case.

Toadwart
July 2nd, 2007, 04:02
Really? Should be


y = math.floor(0.5 + x*0.5);

where x is the value you put in, usually whatever is returned by getValue().

There shouldn't be any rounding errors, since 0.5 is a (negative) power of two and can be stored exactly in a float, but if all of your x's are integers, you can add something just a hair bigger than 0.5 (like 0.51), just in case.

Thanks for that snippit D. :D
Was wondering if there was an easy way to do 'traditional' rounding.