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

Checkbox, hp and percentage

1575396223

Edited 1575468057
-G-
Pro
Hi there, In a custom character sheet, I'm currently using a HP system which is similar to Fate. So players can suffer 3 scratchs condition, 1 hurt condition and 1 very hurt condition and then, fall unconscious.   In game, the character sheet is designed to look this way : The sheet workers included in my character sheet are currently checking the boxes according to the ''wound'' result from an attack. When the Hurt and or Very hurt options are checked, a minus 1/-2 is applied to all rolls. The thing is, describing wounds this way doesn't fell right. It's not intuitive and I would prefer to have a hp_current / hp_max format with a bar on the token and everything. To this effect, I've created a section relative to HP and max HP: <h4>Santé et blessures :</h4> <span class="sheet-table-header">PV Actuels</span> <span class="sheet-table-header">/</span> <span class="sheet-table-header">PV Max</span> <br> <input align="right" type="number" name="attr_hp" value="0"> <span class="sheet-table-data sheet-center">/</span> <input align="right" type="number" name="attr_hpmax" disabled value="@{HP-formula}"> <input align="right" disabled=true type="hidden" name="attr_hpmax" title="@{HP-formula}" value="10*@{body} + 10*@{mass_scale} + @{body} + @{mind} + @{will} + @{agility}"> Now, I need to link the wounded level in the checkbox (Niveau de blessure) to the HP value. What I want is something like this : If HP is within 100% and 51% of HPmax = no box checked. If HP is within 50% and 26% of HPmax = Hurt (blessure) box is checked. If HP is 25% or below HPmax = Very Hurt (blessure grave) is checked. I intend to ditch the Scratch (égratignures) boxes and deal with HP total. I do like the idea of decreased efficiency though and that's why I wish to keep the hurt and very hurt boxes.  I've seen some code out there (Gigs) that I think I could use but I'm currently not used to Java. It's the next thing I'm studying though! on('change:hp change:hp_max' function(eventInfo){     getAttrs(['hp','hp_max'], function(values) {         let current = +values.hp||0;         let max = +values.hp_max||0;         let wounded = (current < max) ? 1: 0;         setAttrs({             wounded: wounded         });     }); }); I need help to create this. Thanks a lot!
1575482630

Edited 1575496810
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Hi Guillaume, I think I can help with this. First we'll need to make some minor modifications to the html of your hp section: <h4>Santé et blessures :</h4> <span class="sheet-table-header">PV Actuels</span> <span class="sheet-table-header">/</span> <span class="sheet-table-header">PV Max</span> <br> <input align="right" type="number" name="attr_hp" value="0"> <span class="sheet-table-data sheet-center">/</span> <input align="right" type="number" name="attr_hpmax" disabled value="@{HP-formula}"><!-- to have a max value of an attribute its name needs to be in the format "attr_current_name_max". Additionally, because we are going to be using sheetworkers, this needs to be "readonly" instead of "disabled". Autocalc and sheetworkers don't play well together and you should try not to mix them. --> <input align="right" disabled=true type="hidden" name="attr_hpmax" title="@{HP-formula}" value="10*@{body} + 10*@{mass_scale} + @{body} + @{mind} + @{will} + @{agility}"> So, the html section will look like this: <h4>Santé et blessures :</h4> <span class="sheet-table-header">PV Actuels</span> <span class="sheet-table-header">/</span> <span class="sheet-table-header">PV Max</span> <br> <input align="right" type="number" name="attr_hp" value="0"> <span class="sheet-table-data sheet-center">/</span> <input align="right" type="number" name="attr_hp_max" readonly value="0"> Then, we'll also need two new sheetworkers. One to set the hp_max (since we aren't using an autocalc anymore), and one to listen to hp changes and change the wounded state appropriately. let figureWounds = function(attributes,setObj){ let hpPercent = attributes.hp/attributes.hp_max, woundsObj = {}; if(hpPercent > 0.5){ woundsObj.inconscient = 0; woundsObj.blessure = 0; woundsObj.blessure_grave = 0; }else if(hpPercent > 0.25){ woundsObj.inconscient = 0; woundsObj.blessure = 1; woundsObj.blessure_grave = 0; }else if(hpPercent > 0){ woundsObj.inconscient = 0; woundsObj.blessure = 1; woundsObj.blessure_grave = 1; }else{ woundsObj.inconscient = 1; woundsObj.blessure = 1; woundsObj.blessure_grave = 1 } Object.assign(setObj,woundsObj);//Because objects are passed as a reference in javascript. Any changes made in this function will affect the original version of setObj, so we don't need to return anything. }; on('change:body change:mass_scale change:mind change:will change:agility',(event)=>{ getAttrs(['body','mass_scale','mind','will','agility','hp'],(attributes)=>{ let setObj = { hp_max:(10*attributes.body||10) + (10*attributes.mass_scale||10) + (attributes.body*1||1) + (attributes.mind*1||1) + (attributes.will*1||1) + (attributes.agility*1||1) }; attributes.hp_max = setObj.hp_max; figureWounds(attributes,setObj); setAttrs(setObj,{silent:true}); }); }); on('change:hp change:hp_max',(event)=>{ getAttrs(['hp','hp_max'],(attributes)=>{ let setObj = {}; figureWounds(attributes,setObj); setAttrs(setObj,{silent:true}); }); }); I've assumed here that blessure and blessure grave are cumulative (e.g. you suffer from both), and that the default value for body, mind, will, and agility is 1. I haven't tested this, but it should work pretty well and give you some ideas for how to fold the workers into your full sheet. And finally, just to prevent any miscommunication; Roll20 sheetworkers (and API scripts) use javascript (roughly at the ES6 or ES7 spec), not java. EDIT: Also, note that I'm setting the setAttrs call to be silent. This means it will not  cause a cascade of changes as no events will be fired for the changes it makes. In general, avoiding those cascades is a better practice than relying on them to propagate changes to multiple sets of attributes.
1575488626

Edited 1575494378
-G-
Pro
Hi Scott, I've done some testing and I've been able to set things up with the code you've given in this post. One thing I'm experiencing though, changing the wound total doesn't update the checkbox. To update the checkbox, I need to alter one attribute (mind, body, agility or will) and then click beside the character sheet (so that the wound total is updated) and switch back the attribute to it's original value and only then will the checkbox be checked. Is there a way to automatically update it without altering the attributes? O
1575496942
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Heh, that's because I had a moment of idiocy. In the listener for hp and hp_max, I used getObj instead of getAttrs. Just fix that and it should work. I've also updated my post to fix the typo. On a semi related note, you may find that you can just listen to go instead of specifying hp_max as well.
Scott C. said: listen to go instead of specifying hp_max as well. Listen to ''go'' or to ''hp''? Just to be sure!
1575524746
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Gah, and I hate phone keyboards. hp, not go
Everything is working smoothly. I've been able to interact with the Alterbar script too, which is quite nice. Thanks a lot! This lead me to another question : Is it possible to add/delete a status marker depending on the wound condition? (Normal, blessure, blessure grave) Thanks.
1575551413
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Unfortunately the statusmarkers require you to use an API script.
1575558708

Edited 1575650301
-G-
Pro
Edit: Forget everything. I've just realised that attr_blessure, attr_blessure_grave and attr_inconscient do the trick. I didn't test enough.  Many thanks for the solution Scott.