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

Setting a Minimum Value for a calculation

Apologies in advance for this once again being a fairly newbie question, but the help I've received has been immensely helpful. I am looking to set a minimum value on a calculation (to 0).  Effectively I am attempting to add an encumbrance calculation to my inventory.  Each time carried weight exceeds your base carry attribute, I want it to go up by 1. The main meat of the question is to do this, I am first hoping to calculate your Maximum Carried limit (which I've already worked out), reduced directly by Amount Carried.  If I am able to set a floor of 0 on this calculation, it means you would only net a result above 0 if you are carrying more than the subtracted amount. I think the rest of the calculation from there is a breeze, but I am unsure how to lock this calculation at a minimum of 0.  I think what I want is the Min function, but I'm unsure how to wield it properly in this context.  I'm not even 100% sure it's possible. Summary:  I am trying to take the following calculation and make the minimum result 0, not in the negatives: floor(@{CARRY_CURRENT}) - floor(@{TOTAL_CARRYCAPACITY}) I think the rest of what I'm trying to do shouldn't be a problem if I can accomplish that.  Thanks again!
1598106961

Edited 1598107105
David M.
Pro
API Scripter
Sounds like you want to use the kh1 syntax in an inline roll, which means "keep highest 1". [[ {-10, 0}kh1 ]] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//will output 0 [[ {(@{CARRY_CURRENT} - @{TOTAL_CARRYCAPACITY}), 0}kh1 ]]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//will output the highest choosing between 0 and the calculated (current-capacity) More details and other options can be found here <a href="https://wiki.roll20.net/Dice_Reference" rel="nofollow">https://wiki.roll20.net/Dice_Reference</a>
Forgive my ignorance on the subject, but does that need to be apart of a sheet worker or can that be put into an Input field?&nbsp; Right now the full code for the calculation I have is this: &lt;input type="text" class="sheet-text" name="attr_CARRY_PENALTY" &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;value="floor({(@{CARRY_CURRENT} - @{TOTAL_CARRYCAPACITY}), 0})" &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;style="width:24px;height:12px; color:#FF0000; font-size: 8pt; border-color:#262626; font-weight: bolder;&nbsp; disabled="true"/&gt; I tried putting in the above formula and could not get it to function, and tried adding kh1 to the end of the formula. Thanks again for the help, I'm really just getting into the javascript side of things.
1598127266
David M.
Pro
API Scripter
Oh shoot, I didn't even realize this was on the character sheet forum! I was thinking this was for a macro. I've down some scripting, but unfortunately I don't have any experience with custom char sheet mods. The kh1 is for die rolls so would require the double square bracket syntax. I'd be surprised if this worked within the character sheet scope. I'm so sorry if this led you down a rabbit hole.&nbsp;
1598128380

Edited 1598128412
GiGs
Pro
Sheet Author
API Scripter
If you're doing this in a character sheet, the best way is to use a sheet worker. That means you want to change your input to &lt;input type="text" class="sheet-text" name="attr_CARRY_PENALTY" &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;value="0"&nbsp;style="width:24px;height:12px; color:#FF0000; font-size: 8pt; border-color:#262626; font-weight: bolder;"/&gt; Note that your style was missing its end quotes, so the disabled=true part wouldnt have worked the way you had it. You'd also be better off moving the style into a class, to simplify the code. Then create a sheet worker, something like on('change:carry_current change:total_carrycapacity' , function() { &nbsp; &nbsp; getAttrs(['CARRY_CURRENT', 'TOTAL_CARRYCAPACITY'], function(v) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; var current = parseInt(v.CARRY_CURRENT) || 0; &nbsp; &nbsp; &nbsp; &nbsp; var total = parseInt(v.TOTAL_CARRYCAPACITY) || 0; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; var penalty = Math.max(0, current - total); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; setAttrs({ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; CARRY_PENALTY: penalty &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; }); &nbsp; &nbsp; }); }); I have a feeling your original formula is missing a divider, because there's no reason to use floor in there as it stands. But if you were to include a divide, let's say 2 for descriptive purposes, you would change the penalty line to &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; var penalty = Math.max(0, Math.floor((current - total)/2));
1598169885

Edited 1598173085
GM
Pro
Ah, thank you once again.&nbsp; I was attempting to make a worker to accomplish that but couldn't get it to work and this example is a great help to understand why.&nbsp; I appreciate the help! ED: I have been messing with that formula, and I cannot get any math to actually happen even when presented exactly (or adjusted).&nbsp; I'm not sure what's happening.&nbsp; It will write CARRY_PENALTY with the value of CARRY_CURRENT, but it will not subtract CARRY_CAPACITY.&nbsp; I tried messing with this and having it subtract other variables instead, and the same results:&nbsp; It will set Penalty to whatever the first attribute referenced in the Math is set to and not adjust it based on a second attribute. I've tried re-writing it from the example - and understand much more what's going on with the setup now I think - but I'm not sure why the current - total line seems to ignore the Total.&nbsp; Am I doing something wrong? ED:&nbsp; Tried further experimentation and tried to make the math function come to other results, such as adding or multiplying and I also tried changing math.max to other math functions.&nbsp; For whatever reason, no math seems to take place.&nbsp; It always presents the first listed attribute as the CARRY_PENALTY value, so it's the same as CARRY_CURRENT in the default example.&nbsp; Not sure what I'm doing wrong here. Essentially the end result I am working towards is this: If Carried Weight &gt; Carried Capacity, assign a 1 Point Penalty each time your additional weight exceeds your Base Carry stat.&nbsp; So if your Base Carry is 10, and you are carrying 80 of 60 weight, I'm trying to arrive at a 2 Point Penalty.&nbsp; Anyway sorry for the ramble, just explaining what I'm doing if there's a better way.&nbsp; I think I have the majority worked out.&nbsp; Unlike Google Sheets however I'm having trouble basically with the "If Carried Weight is less than Carry Capacity do not go into a negative penalty" problem (i.e. the value is -40 if you have a max of 60 and are carrying 20).&nbsp; Hopefully that makes sense.
1598172753

Edited 1598172883
GiGs
Pro
Sheet Author
API Scripter
Is your Total_Carrycapacity attribute an autocalc field, with disabled=true? If so, it wont work - autocalc stats are incompatible with sheet workers, and you'll need to calculate carry_capacity with a sheet worker.
1598173267

Edited 1598173756
GM
Pro
Ah, I think what you just said is likely my problem. However, when I tried to make pull an input box I couldn't make it read the number typed within either.&nbsp; I put in a test box and attempted to punch numbers into it to see if it would reduce the value by them and it also didn't have an impact. Are numbers manually typed into a text input invalid as well?
1598173644
GiGs
Pro
Sheet Author
API Scripter
Numbers work fine in both text and number type inputs, though number type is best because players cant enter invalid values. Just dont have them set as disabled. Can you post the html for your inputs? If they aren't working for you, there's something invalid about the syntax and we should be able to help you put them right.
Ah, I tried a different input box rebuild and it works great.&nbsp; The entire problem I was having with the fact I was pulling in autocalc numbers without realizing that didn't work.&nbsp; Thanks a ton, you've been extremely helpful ; sorry for having so many incredibly basic questions, but I am absolutely on the right track again.&nbsp; Very appreciated.
1598173977
GiGs
Pro
Sheet Author
API Scripter
Dont worry about asking basic questions. We all have to start somewhere and there's a lot to learn!