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

Evaluate the max and min of a roll

1600114193

Edited 1600116929
Marco M.
KS Backer
Sheet Author
API Scripter
Compendium Curator
Hi,  is there an internal function to evaluate the max and min of a roll? I need it to handle criticals in my character sheet, I come up with an option that doesn't use eval, but I noticed that it doesn't work even if I use Function. The alternative is splitting m1 and m2 and looking for the operators and doing the math following the order of operation, but there should be a better way Note: for whomever is interested, yes the logical ('||') or return the first value between 2, if the first value is null, I wasn't aware of it, but it works. *update:* To be clear, when I said min and max I mean, the max and possible value a string 'xDy+z' (or more complex) can have. Eval and Function, allow me to do it, but they allowed in roll20. I was looking for a function already provided but I cannot find any. *update 2: thanks for GiGs for finding the bug. This is the version of the code that works* on("change:san_success change:san_failure",function(v){             getAttrs(['san_success','san_failure'], function(v){             let maxsan=v.san_failure;        let minsan=v.san_success;     let update={};         maxsan="("+maxsan+")";         minsan="("+minsan+")";         let m1 = minsan.toLowerCase().replace(/[\\+\\-\\*\\/]/gi,")$&(").replace(/[dD]/gi,'||');         let m2 = maxsan.toLowerCase().replace(/[\\+\\-\\*\\/]/gi,")$&(").replace(/[dD]/gi,'*');         update['maxsanloss']=Function('"use strict";return (' + m2 + ')')();         update['minsanloss']=Function('"use strict";return (' + m1 + ')')();         console.log(m1+"="+Function('"use strict";return (' + m1 + ')')());         console.log(m2+"="+Function('"use strict";return (' + m2 + ')')());         setAttributes(update,true);             }); });
1600115478
GiGs
Pro
Sheet Author
API Scripter
Is this a sheet worker? The syntax isnt correct. The start should be on("change:san_success change:san_failure",function(){     getAttrs(['san_success', 'san_failure'], function(v) { You seem to be saving actual functions to attributes (the maxsanloss and minsanloss lines), and I'm not sure what you are going for there, or how that is supposed to work. Also the setAttributes line - is that a custom function, or a typo of setAttrs? Syntax aside, what I'm understanding is you are entering a dice string value in san_failure and san_success. You can alculate a max and min from those, but it's not a good way to go about it. What you should be doing is creating your character sheet inputs so you dont need to do this. Instead of entering a string for san failure for example, have three inputs: one for number of dice, one for die size, and one for modifier. Then calculating the max or minimum is easy: your inputs are all numerical, you have to do no string manipulatiion.
1600116576
Marco M.
KS Backer
Sheet Author
API Scripter
Compendium Curator
oh god, thank you so much, I'm a dummy. I should have made the change you said. Now it works (the minsanloss and maxsanloss will be hidden variables). So the SAN loss in this game is given as 'roll1/roll2'. You roll a 1d100<=SAN. On a success you lose roll1 on a failure the roll2.  The problem were the criticals: if you have match dice and a success, you get the min of roll1, if you have match dice and a failure, you get the max of roll2. I found a work around to the need of using APIs to target a particular character: I had a target checkbox that the GM can use to roll vs a targeted token SAN (if they don't do it, the roll button will just show the san loss for failure and the san loss for success). Now, having a way to "parse the dice roll" with function, gives me the opportunity to allow for complex sanity loss formula (for example 1d10+(3d4)/2) that are unlikely, but possible. Thanks again for finding my bug :)
1600156522
GiGs
Pro
Sheet Author
API Scripter
You're welcome :)