Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Can numbers be prevented from reaching negative integers?

I'm testing my sheet and noticed that one stat can go negative (it shouldn't by the rules). Is there anything I can put into the script that leaves it at 0?
1532904130
Pat S.
Forum Champion
Sheet Author
Iif this is a custom sheet that you are creating, you can set the field to numeric and set a minium for that field.
Pat S. said: Iif this is a custom sheet that you are creating, you can set the field to numeric and set a minium for that field. Thanks. What's the HTML for that? I don't recall learning how that's done on the wiki.
See here: <a href="https://www.w3schools.com/htmL/html_form_input_types.asp" rel="nofollow">https://www.w3schools.com/htmL/html_form_input_types.asp</a> and scroll down for "Input Type Number."
1532912328

Edited 1532912335
Grim G.
Sheet Author
Rabulias said: See here: <a href="https://www.w3schools.com/htmL/html_form_input_types.asp" rel="nofollow">https://www.w3schools.com/htmL/html_form_input_types.asp</a> and scroll down for "Input Type Number." Thanks, but it doesn't seem to be working. The input number I'm putting it on is disabled and has a value equal to the sum of two others. Is there any way around with the info I just gave?
1532918751
GiGs
Pro
Sheet Author
API Scripter
Can the other two numbers go negative? If not, put the min there too. Otherwise, you can use "ceil(0,your number)" in an autocalc field, or have a sheet worker change the number (if its not a disabled field). As always, its a good idea to post the code you are using already (just the relevant bits), to make it easier for people to suggest solutions.
1532923830
Pat S.
Forum Champion
Sheet Author
Now that I'm home I can give snippet of code from my sheet. It is a manual input field. &lt;div class="sheet-col-1-6"&gt;&lt;input type="number" name="attr_intelligence" min="3" max="18" step="1" value="10" /&gt;&lt;/div&gt; I have all the stats setup for minimum of 3 and max of 18 along with step of 1. The default value is 10.&nbsp; With that said, are you looking at working with a sheetworker, an autocalc formula, or what? As GG mentions, some code you are working with along with your intent/use would help everyone figure if they can make a good/proper suggestion.
G G said: Can the other two numbers go negative? If not, put the min there too. Otherwise, you can use "ceil(0,your number)" in an autocalc field, or have a sheet worker change the number (if its not a disabled field). As always, its a good idea to post the code you are using already (just the relevant bits), to make it easier for people to suggest solutions. One of them can and probably will go negative. So that won't work. Is a sheetworker the only other way? Because I remember anything about JavaScript. As for my code, here's the important bits. Minus the&nbsp; @{health_mod} . &lt;div class='sheet-col'&gt; &lt;!--Primary Stats--&gt; &lt;h3&gt;Stats&lt;/h3&gt; &lt;div class='sheet-2colrow'&gt; &lt;div class='sheet-col'&gt; &lt;label&gt;Body:&lt;/label&gt;&lt;input type="number" name="attr_body"/&gt;&lt;button type="roll" value="/roll 1d6 + @{body} + @{health_mod}"&gt;&lt;/button&gt; &lt;label&gt;Mind:&lt;/label&gt;&lt;input type="number" name="attr_mind"/&gt;&lt;button type="roll" value="/roll 1d6 + @{mind} + @{health_mod}"&gt;&lt;/button&gt; &lt;label&gt;Spirit:&lt;/label&gt;&lt;input type="number" name="attr_spirit"/&gt;&lt;button type="roll" value="/roll 1d6 + @{spirit} + @{health_mod}"&gt;&lt;/button&gt; &lt;label&gt;Stat Points:&lt;/label&gt;&lt;input type="number" name="attr_stat_points"/&gt; &lt;/div&gt; &lt;div class='sheet-col'&gt; &lt;label&gt;Fame:&lt;/label&gt;&lt;input type="number" name="attr_fame"/&gt;&lt;button type="roll" value="/roll 1d6 + @{fame}"&gt;&lt;/button&gt; &lt;label&gt;Fame Points:&lt;/label&gt;&lt;input type="number" name="attr_fame_points"/&gt; &lt;label&gt;Reserve:&lt;/label&gt;&lt;input type="number" name="attr_reserve"/&gt; &lt;label&gt;Armor:&lt;/label&gt;&lt;input type="number" name="attr_personal_armor"/&gt; &lt;/div&gt; &lt;/div&gt; &lt;!--Secondary Stats--&gt; &lt;div&gt; &lt;h3 align="center"&gt;Secondary Stats&lt;/h3&gt; &lt;table border="1px" align="center"&gt; &lt;tr align="right"&gt; &lt;td&gt;Defence=&lt;input Type="number" value="3 + @{body} + @{athletics}" name="attr_defence" disabled="true"/&gt;&lt;/td&gt; &lt;td&gt;3+Body&lt;input type="number" value="3 + @{body}" name="attr_body_defence" disabled="true"/&gt;&lt;/td&gt; &lt;td&gt;+Athletics&lt;input type="number" name="attr_athletics"/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr align="right"&gt; &lt;td&gt;Resist=&lt;input type="number" value="@{spirit} + @{personal_armor}" name="attr_resist" min="0" disabled="true"/&gt;&lt;/td&gt; &lt;td&gt;Spirit&lt;input type="number" value="@{spirit}" name="attr_spirit_resist" disabled="true"/&gt;&lt;/td&gt; &lt;td&gt;+Armor&lt;input type="number" value="@{personal_armor}" name="attr_armor_resist" disabled="true"/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr align="right"&gt; &lt;td&gt;Health=&lt;input type="number" value="3 + @{body} + @{spirit}"name="attr_health" disabled="true"/&gt;&lt;/td&gt; &lt;td&gt;3+Body&lt;input type="number" value="3 + @{body}" name="attr_body_health" disabled="true"/&gt;&lt;/td&gt; &lt;td&gt;+Spirit&lt;input type="number" value="@{spirit}" name="attr_spirit_health" disabled="true"/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/div&gt; &lt;div&gt;
1532970022
vÍnce
Pro
Sheet Author
This problem sounded familiar This may help. <a href="https://app.roll20.net/forum/permalink/2146837/" rel="nofollow">https://app.roll20.net/forum/permalink/2146837/</a> more &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; @Brian said; This one isn't CSS Wizardry, more math wizardry, but I spent enough time trying to get it to work that I felt I should share. We do not have access to the&nbsp; min(x) &nbsp;or&nbsp; max(x) &nbsp;functions in autocalc fields. However, we can get the same result, thanks to the&nbsp; abs(x) &nbsp;function! &lt;input type="number" name="attr_A" value="0" /&gt; &lt;input type="number" name="attr_B" value="0" /&gt; &lt;input type="number" name="attr_MAX" value="((@{A} + @{B}) + abs(@{A} - @{B})) / 2" disabled="true" /&gt; &lt;input type="number" name="attr_MIN" value="((@{A} + @{B}) - abs(@{A} - @{B})) / 2" disabled="true" /&gt;
1532970266

Edited 1532970645
GiGs
Pro
Sheet Author
API Scripter
min="0" wont work on a disabled field, that only works for fields that people can edit. Try swapping &lt;td&gt;Resist=&lt;input type="number" value="@{spirit} + @{personal_armor}" name="attr_resist" min="0" disabled="true"/&gt;&lt;/td&gt; for &lt;td&gt;Resist=&lt;input type="number" value="ceil(0,@{spirit} + @{personal_armor})" name="attr_resist" disabled="true"/&gt;&lt;/td&gt; You might need to enclose that in [[ ]], not sure (like so: [[ ceil(0,@{spirit} + @{personal_armor})]], but try it without it first. If that doesnt work, I'm sure there's a syntax that will, or a simple sheet worker can replace the calculation.
1532971102

Edited 1532971129
GiGs
Pro
Sheet Author
API Scripter
If the suggestion I posted doesnt work, incorporating Vince's version would look like this: value="(@{spirit} + @{personal_armor} + abs(@{spirit} + @{personal_armor})) / 2" This looks a little different to the MAX formula posted: ((@{A} + @{B}) + abs(@{A} - @{B})) / 2 But in that expression, @{A} is your (@{spirit} + @{personal_armor}) calculation, and @{B} is 0, since you're looking for the Max of your calculation and 0. Since 0 can be removed, we are left with this: value="(@{spirit} + @{personal_armor} + abs(@{spirit} + @{personal_armor})) / 2"
G G said: If the suggestion I posted doesnt work, incorporating Vince's version would look like this: value="(@{spirit} + @{personal_armor} + abs(@{spirit} + @{personal_armor})) / 2" This looks a little different to the MAX formula posted: ((@{A} + @{B}) + abs(@{A} - @{B})) / 2 But in that expression, @{A} is your (@{spirit} + @{personal_armor}) calculation, and @{B} is 0, since you're looking for the Max of your calculation and 0. Since 0 can be removed, we are left with this: value="(@{spirit} + @{personal_armor} + abs(@{spirit} + @{personal_armor})) / 2" Yeah, this worked when I spliced it in. I only partially understand it though. What does abs do exactly? why is / 2 at the end? And why do the variables have to be in there twice?
1532988496
GiGs
Pro
Sheet Author
API Scripter
abs takes the absolute value of a number, so it treats -3 as 3. So this caclulation is ( (number) + abs(number) ) /2 When all numbers are positive, you end up with double the original total, which is why you have /2 at the end, to return to zero. If the number is negative, lets say -3, you end up with: -3 + abs(-3) ==&gt; -3 + 3 = 0.&nbsp; So using abs in this formula stops the total from going below zero. You can grab a sheet a paper, and test it with numbers you set yourself, like say -3 for spirit and 7 for armour, and see what happens, and again for -7 and 3. Remember that abs() removes the negative sign.
1532990617
vÍnce
Pro
Sheet Author
How does this work? Brain is a Vulcan. ;-)