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 Worker double condition

1707143602

Edited 1707144248
Evilbram
Pro
Marketplace Creator
Sheet Author
Hello, I come to you to obtain information that I struggle to find by consulting the various wiki and forum posts. I'm stuck on a sheet worker script that I'm trying to make work. My question will perhaps lead to a simple answer like "no, it's not possible" or a miraculous answer which will unlock lots of new coding horizons for me!! So, is it possible in a script to use a double condition: IF “this_value” = x AND “this_other_value” = y SO I haven't found any examples using a double condition in my searches and I'm just wondering if this is possible. I am attaching my coding (which I know is not functional but which allows you to visualize what I want to do) <script type="text/worker"> on("sheet:opened change:quality_courage change:default_courage", function() { getAttrs(["quality_courage", "default_courage"], function(values) { let quality_courage = parseInt(values["quality_courage"]) || 0;     let default_courage = parseInt(values["default_courage"]) || 0; if (quality_courage >= default_courage ) /* first condition */ { /* second condition */ if (quality_courage == 1 ) D_courage_roll = "[[1D6]]"; else if (quality_courage == 2 ) D_courage_roll = "[[1D6]] [[1D6]]"; else if (quality_courage == 3 ) D_courage_roll = "[[1D6]] [[1D6]] [[1D6]]"; else if (quality_courage == 4 ) D_courage_roll = "[[1D6]] [[1D6]] [[1D6]] [[1D6]]"; else if (quality_courage == 5 ) D_courage_roll = "[[1D6]] [[1D6]] [[1D6]] [[1D6]] [[1D6]]"; else D_courage_roll = "[[1D6]]"; } else /* first condition */ { /* second condition */ if (default_courage == 1 ) D_courage_roll = "[[1D6]]"; else if (default_courage == 2 ) D_courage_roll = "[[1D6]] [[1D6]]"; else if (default_courage == 3 ) D_courage_roll = "[[1D6]] [[1D6]] [[1D6]]"; else if (default_courage == 4 ) D_courage_roll = "[[1D6]] [[1D6]] [[1D6]] [[1D6]]"; else if (default_courage == 5 ) D_courage_roll = "[[1D6]] [[1D6]] [[1D6]] [[1D6]] [[1D6]]"; else D_courage_roll = "[[1D6]]"; } setAttrs({ "D_courage_roll": D_courage_roll }); }); }); </script> In this example I want to compare the value of quality_courage and default_courage initially, and depending on which of the values ​​is the highest, do this or that type of comparative function.
1707173710

Edited 1707173979
vÍnce
Pro
Sheet Author
There are probably 1001 and ways to handle this, but here's an option that compares quality_courage and default_courage, uses the highest one, and then updates the D_courage_roll with the appropriate number of dice rolls. (untested) ;-P on('sheet:opened change:quality_courage change:default_courage', function () { getAttrs(['quality_courage', 'default_courage'], function (values) { const quality_courage = parseInt(values.quality_courage) || 0; const default_courage = parseInt(values.default_courage) || 0; let D_courage_roll = '';     // use the highest one const highestCourage = Math.max(quality_courage, default_courage);     // match and assign the appropriate # of rolls switch (highestCourage) { case 1: D_courage_roll = '[[1D6]]'; break; case 2: D_courage_roll = '[[1D6]] [[1D6]]'; break; case 3: D_courage_roll = '[[1D6]] [[1D6]] [[1D6]]'; break; case 4: D_courage_roll = '[[1D6]] [[1D6]] [[1D6]] [[1D6]]'; break; case 5: D_courage_roll = '[[1D6]] [[1D6]] [[1D6]] [[1D6]] [[1D6]]'; break; default: return; } setAttrs({ D_courage_roll, }); }); Not sure if you need to include sheet:opened in there if you are already setting default values for quality_courage, default_courage and D_courage_roll in the html.
1707179237

Edited 1707179880
GiGs
Pro
Sheet Author
API Scripter
In sheet workers, you can use && (AND) to combine two tests and test both are true. For example: IF (“this_value” === x && “this_other_value” === y) { Vince's code looks like a good solution, (I had something erlse here, but had misread what it's doing).
1707180404
GiGs
Pro
Sheet Author
API Scripter
In code like this, you should always consider what happens if someone enters a value, then changes it. For example, if someone has default_courage set to 1, then changes it to 0. In this case no further changes happen, so someone could have default_courage 0, and D_courage_roll '[[1D6]] [[1D6]] [[1D6]] [[1D6]] [[1D6]]'. So you probably want another else statement, like else { D_courage_roll = '[[1D6]]'; } or whatever the value would be on default_courage 0. Here's a more compact version of Vince's worker, which assumes Courage is never higher than 5 - but you can set it higher on ( 'sheet:opened change:quality_courage change:default_courage' , function () {             getAttrs ([ 'quality_courage' , 'default_courage' ], function ( values ) {                 const quality_courage = parseInt ( values . quality_courage ) || 0 ;                 const default_courage = parseInt ( values . default_courage ) || 0 ;                 // use the highest one                 const highestCourage = Math . min ( 5 , Math . max ( quality_courage , default_courage ));                 let roll_array = [];                 for ( let i = 0 ; i < highestCourage ; i ++ ) {                     roll_array . push ( '[[1D6]]' );                 }                 let D_courage_roll = roll_array . length ? roll_array . join ( ' ' ) : '[[1D6]]' ; // change that '[[1D6]]' to whatever the value should be when both courages = 0                 setAttrs ({                     D_courage_roll ,                 });             });         });
1707239320
Evilbram
Pro
Marketplace Creator
Sheet Author
Thank you for your answers and for the time you took. I'm going to look at all of this and resolve my last little script problem to finalize the character sheet!! It's always nice to get help and move forward.