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

If statements with text

I am currently making a sheet with a hunger mechanic.  In order to act as a reminder, I am trying to make an if statement that gives a small text blurb depending on how high the hunger amount is on the character sheet.  My attempts so far have been in vain, please help.
1721271711
GiGs
Pro
Sheet Author
API Scripter
This is very easy with a sheet worker, but we need context. What is the text, where will it be displayed, what is the attribute name of the hunger attribute and does every point cause a change in the text? Let's say your text is displayed in a span named hunger_text, you could do something like: on ( 'change:hunger' , function () {     getAttrs ([ 'hunger' ], function ( values ) {         const hunger = parseInt ( values . hunger ) | 0 ;         let hunger_text = '' ;         if ( hunger == 1 ) {             hunger_text = "first" ;         } else if ( hunger == 2 ) {             hunger_text = "second" ;         } else if ( hunger == 3 ) {             hunger_text = "third" ;         } else {             hunger_text = "everything else" ;         }         setAttrs ({             hunger_text : hunger_text         });     }); });
<label>Hunger </label><input type="number" name='attr_Hunger' class='sheet-short'> <br /> this is the code I have for hunger with the goal for this to appear under it so if hunger is at 5, text 1 appears, and if its at 10 text 1 is replaced by text 2
1721273047

Edited 1721273066
GiGs
Pro
Sheet Author
API Scripter
Try this on ( 'change:hunger' , function () {     getAttrs ([ 'hunger' ], function ( values ) {         const hunger = parseInt ( values . hunger ) | 0 ;         let hunger_text = '' ;         if ( hunger >= 10 ) {             hunger_text = "text 2" ;         } else if ( hunger >= 5 ) {             hunger_text = "text 1" ;         } else {             hunger_text = "whatever is below 5" ;         }         setAttrs ({             hunger_text : hunger_text         });     }); }); When building an if statement, you need to plan the order carefully. This says anything 10 or above, then anything 5 or above, then anything else. An if statement stops at the first condition that matches. So if I had the >=5 test first, the code would skip the rest of the branches of the if statement, and would include the 10+ group.
Never used HTML before, so I think I messed up the span message, can you check over what I have done to add it in? <label>Hunger </label><input type="number" name='attr_Hunger' class='sheet-short'> <br /> <script type="text/worker"> on('change:hunger', function () { getAttrs(['hunger'], function(values) { const hunger = parseInt(values.hunger) | 0; let hunger_text = ''; if(hunger >= 10) { hunger_text = "text 2"; } else if(hunger >= 5) { hunger_text = "text 1"; } else { hunger_text = "whatever is below 5"; } setAttrs({ hunger_text: hunger_text }); }); }); </script> <span 'hunger_text'> <br />
1721291257
GiGs
Pro
Sheet Author
API Scripter
You did mess up the span, it should look like this: &lt;span name='attr_hunger_text'&gt;&lt;/span&gt; In Roll20 HTML, you can have attributes linked to spans, inputs, and textareas, and they use the same name format: name='attr_attribute-name'&gt; . You can also include other tags like class="class-name" . Also, your script block can 9and should!) be placed at the end of all your HTML. You can have one script block and put all your sheet workers inside it. This is the script block: &lt;script type="text/worker&gt; // put all your sheet workers here &lt;/script&gt; You can be guided into Roll20 html here: <a href="https://cybersphere.me/roll20/" rel="nofollow">https://cybersphere.me/roll20/</a>