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

[Sheet Workers] using 'greater than' & 'less than' with 'if else'

I wish to make a sheet worker script/thing that, should an attribute be higher than a certain number, one setAttrs is done.  And, if less than a certain number, a different setAttrs is done.  I have scripts I copy and adjust for specific numbers, but not greater or less than.  How would I set it up? Here is a chunk of my code:     //BTM on("change:Body sheet:opened", function() {     getAttrs(["Body","BTM"], function(values) {         if (values.Body == 2) {             setAttrs({                  BTM: 0             })         } else if (values.Body == 3) {             setAttrs({  BTM: -1             })         }     }); }); Thanks for looking
1465235697
Kryx
Pro
Sheet Author
API Scripter
on("change:Body sheet:opened", function() {     getAttrs(["Body","BTM"], function(values) {         if (values.Body > 2) {             setAttrs({                  BTM: 0             })         } else if (values.Body < 20) {             setAttrs({  BTM: -1             })         } else { setAttrs({  BTM: 100             }) }     }); }); else not necessary.
1465236289

Edited 1465236314
Corin S.
Sheet Author
While I don't know the particular stat relations we should be dealing with, I believe something like this should work. //BTM on("change:Body sheet:opened", function() {     getAttrs(["Body","BTM"], function(values) {         if (values.Body < 2) {             setAttrs({                  BTM: 1             })         }         else if (values.Body == 2) {             setAttrs({                  BTM: 0             })         }     else if (values.Body == 3) {             setAttrs({  BTM: -1             })         }         else if (values.Body > 3) {             setAttrs({      BTM: -2             })         }     }); }); In the event you need less than or equal to or greater than or equal to, the symbols are <= or >=, accordingly. Ah. Sniped.
1465238756
Finderski
Plus
Sheet Author
Compendium Curator
One thing that needs to be mentioned...the values coming from the getAttrs() is going to be an array in a string format. So, if you want to evaluate the number coming from getAttrs() you will need to wrap the value in a parseInt().  Something like: if (parseInt(values.Body) < 2) { Also, I believe all the attributes being referenced need to be small...so "Body" should be "body"
This is working, thanks! "Body" works, as well.  So it reconizes capital letters.  However, I am having a hard time getting else if with "<" and ">" to work with more than a single condition.  For example:     //Auto Wound Check -Stun & Death Saves on("change:Wound sheet:opened", function() {     getAttrs(["Wound","SeriousWound"], function(values) {         if (values.Wound <= 1.001) {             setAttrs({                  LightlyWounded: 0,                 SeriouslyWounded: 0,                 CriticallyWounded: 0,                 Mortal0Wounded: 0,                 Mortal1Wounded: 0,                 Mortal2Wounded: 0,                 Mortal3Wounded: 0,                 Mortal4Wounded: 0,                 Mortal5Wounded: 0,                 Mortal6Wounded: 0             })         }         else if (values.Wound >= 1.001) {             setAttrs({                  LightlyWounded: 1,                 SeriouslyWounded: 0,                 CriticallyWounded: 0,                 Mortal0Wounded: 0,                 Mortal1Wounded: 0,                 Mortal2Wounded: 0,                 Mortal3Wounded: 0,                 Mortal4Wounded: 0,                 Mortal5Wounded: 0,                 Mortal6Wounded: 0             })         }         else if (values.Wound >= 1.011) {             setAttrs({                  LightlyWounded: 1,                 SeriouslyWounded: 1,                 CriticallyWounded: 0,                 Mortal0Wounded: 0,                 Mortal1Wounded: 0,                 Mortal2Wounded: 0,                 Mortal3Wounded: 0,                 Mortal4Wounded: 0,                 Mortal5Wounded: 0,                 Mortal6Wounded: 0             })         }         else if (values.Wound >= 2.001) {             setAttrs({                  LightlyWounded: 1,                 SeriouslyWounded: 1,                 CriticallyWounded: 1,                 Mortal0Wounded: 0,                 Mortal1Wounded: 0,                 Mortal2Wounded: 0,                 Mortal3Wounded: 0,                 Mortal4Wounded: 0,                 Mortal5Wounded: 0,                 Mortal6Wounded: 0             })         }     }); }); Only the first if else is applied.  Any ideas?
1465251717
Lithl
Pro
Sheet Author
API Scripter
var attrs = { LightlyWounded: 0,     SeriouslyWounded: 0,     CriticallyWounded: 0,     Mortal0Wounded: 0,     Mortal1Wounded: 0,     Mortal2Wounded: 0,     Mortal3Wounded: 0,     Mortal4Wounded: 0,     Mortal5Wounded: 0,     Mortal6Wounded: 0 }; if (values.Wound >= 1.001) { attrs.LightlyWounded = 1; } if (values.Wound >= 1.011) { attrs.SeriouslyWounded = 1; } if (values.Wound >= 2.001) { attrs.CriticallyWounded = 1; } setAttrs(attrs);
1465252503
Kryx
Pro
Sheet Author
API Scripter
Order matters. Put the higher cases first.
Brian : It works.  Thank you, yet again! Kryx : ...I have no idea what you are talking about.
1465253444
Kryx
Pro
Sheet Author
API Scripter
If else is evaluated in order. So you did an if, if that passes then no else works. So 2.001 will never be reached because it will always end in the 1.001 block as anything greater than 2.001 is also greater than 1.001. So order matters and you'd have to swap them to use else if.
Basically, an if-else will only go down one possible path. Since everything that could go down the 2.001 path would already go down the 1.001 path, the 2.001 path will never be used.
Ah.  Thank you both for the explaination.