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 .
×
May your rolls be merry + bright! 🎄
Create a free account

Add dice modifier if !=0

1570391448

Edited 1570391521
BlackyB
Sheet Author
Hello, I'm curently working on my second custom character sheet and I have some modifier (checkboxes) added to a classic attribute roll and I would like to only add this modifiers in the calculation if they're not equal to 0 Here's the code : value="/roll [[@{Empathy}+@{Social_Pos1}+@{Social_Pos2}-@{Social_Neg1}-@{Social_Neg2}+ ?{modifier|0}]]d10 (@{character_name} - empathy test)"/> SocialPos/Neg are the checkboxes and their value is 1 or 2, and 0 when not checked, but it displays : "Rolling 3+0+0+0+0" when rolling them unchecked Is there a way to only roll when !=0  and to show "Rolling 3"
1570432305

Edited 1570432960
GiGs
Pro
Sheet Author
API Scripter
There's no way to have conditional elements in a macro. You cant have "if something is not 0, do this" within the macro. What you can do though, since you are making the character sheet, is build the macro in a sheet worker. Until that roll button is clicked, the macro is just text, and you can manipulate it with sheet worker programming, So what you would need to do is create a hidden attribute to hold the button text, and replace the button code above with a reference to this attribute. Like so value="/roll @{button_text}d10 (@{character_name} - empathy test)"/> And then your hidden attribute would be <input type="hidden" name="attr_button_text" value="[[@{Empathy}+@{Social_Pos1}+@{Social_Pos2}-@{Social_Neg1}-@{Social_Neg2}+ ?{modifier|0}]]"> Now you create a sheet worker which responds to the SocialPos/Neg checkboxes, and replaces the button_text attribute's value with the contents you want in the button. on("change:socialpos1  change:socialneg 1 change:socialpos2  change:socialneg 2 sheet:opened", function () {     getAttrs(['SocialPos1','SocialPos2','SocialNeg1','SocialNeg2'], function(values) {         const pos1 = parseInt(values.SocialPos1)||0;         const neg1 = parseInt(values.SocialNeg1)||0;         const pos2 = parseInt(values.SocialPos2)||0;         const neg2 = parseInt(values.SocialNeg2)||0;         let macro = "[[@{Empathy}"; if(pos1) macro = macro + "+@{Social_Pos1}"; if(pos2) macro = macro + "+@{Social_Pos2}"; if(neg1) macro = macro + "-@{Social_Neg1}"; if(neg2) macro = macro + "-@{Social_Neg2}";         macro = macro + "+ ?{modifier|0}]]";         setAttrs({             button_text: macro         });     }); }); Untested, but that's the basic method you can use. There arer much more compact ways to write this worker, but this is easier to follow what's happening. if(pos1) only runs the statement afterwards, if pos1 is not equal zero. If its zero, it ignores the rest of that line.
1570432915
GiGs
Pro
Sheet Author
API Scripter
You could also replace lines like this if(pos1) macro = macro + "+@{Social_Pos1}"; with if(pos1) macro = macro + "+1"; or even total up the social_pos and Neg stats and just add them directly, like this         const pos1 = parseInt(values.SocialPos1)||0;         const neg1 = parseInt(values.SocialNeg1)||0;         const pos2 = parseInt(values.SocialPos2)||0;         const neg2 = parseInt(values.SocialNeg2)||0; const sum = pos1 + pos2 - neg1 - neg2; const macro = "[[@{Empathy} +(" + sum + ") + ?{modifier|0}]]"; There's different ways to construct it, depending on exactly what you want in the output.
Yeah I though we couldn't use Javascript when creating a character sheet, I'm currently reading wiki about sheet worker
1570438019
GiGs
Pro
Sheet Author
API Scripter
You cant use javascript for the html. In webdesign, javascript is often used to perform layout tasks - you cant do that in roll20. You can use sheet workers though - they have a very specific and limited use: they can only read attributes, perform calculations, and then change attribute values. See Building Character Sheets: Sheet Worker Scripts .