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

Converting Script to Ingame Macro

1647228584
Does anyone know if it is possible to convert the following code, from the character sheet, into a Macro? <button type="action" name="act_roll">Skill Challenge</button> <script type="text/worker">   const extractQueryResult = async function(query){     let queryRoll = await startRoll(`!{{query=[[0[response=?{${query}}]]]}}`);     finishRoll(queryRoll.rollId);     return queryRoll.results.query.expression.replace(/^.+?response=|\]$/g,'');   };   const initiateRoll = async function(event){//Async tag here to allow us to await the various rolls that we're going to do.     let queryResult = await extractQueryResult('Attack Ranks|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30');//Await the asynchronous startRoll     let queryResult2 = await extractQueryResult('Defense Ranks|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30');//Await the asynchronous startRoll const resultSwitchArray =[ [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30], [1,50,55,60,65,70,73,76,79,82,85,87,89,91,93,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110], [2,45,50,55,60,65,68,71,74,77,80,82,84,86,88,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105], [3,40,45,50,55,60,63,66,69,72,75,77,79,81,83,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100], [4,35,40,45,50,55,58,61,64,67,70,72,74,76,78,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95], [5,30,35,40,45,50,53,56,59,62,65,67,69,71,73,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90], [6,27,32,37,42,47,50,53,56,59,62,64,66,68,70,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87], [7,24,29,34,39,44,47,50,53,56,59,61,63,65,67,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84], [8,21,26,31,36,41,44,47,50,53,56,58,60,62,64,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81], [9,18,23,28,33,38,41,44,47,50,53,55,57,59,61,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78], [10,15,20,25,30,35,38,41,44,47,50,52,54,56,58,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75], [11,13,18,23,28,33,36,39,42,45,48,50,52,54,56,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73], [12,11,16,21,26,31,34,37,40,43,46,48,50,52,54,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71], [13,9,14,19,24,29,32,35,38,41,44,46,48,50,52,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69], [14,7,12,17,22,27,30,33,36,39,42,44,46,48,50,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67], [15,5,10,15,20,25,28,31,34,37,40,42,44,46,48,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65], [16,4,9,14,19,24,27,30,33,36,39,41,43,45,47,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64], [17,3,8,13,18,23,26,29,32,35,38,40,42,44,46,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63], [18,2,7,12,17,22,25,28,31,34,37,39,41,43,45,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62], [19,1,6,11,16,21,24,27,30,33,36,38,40,42,44,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61], [20,0,5,10,15,20,23,26,29,32,35,37,39,41,43,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60], [21,-1,4,9,14,19,22,25,28,31,34,36,38,40,42,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59], [22,-2,3,8,13,18,21,24,27,30,33,35,37,39,41,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58], [23,-3,2,7,12,17,20,23,26,29,32,34,36,38,40,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57], [24,-4,1,6,11,16,19,22,25,28,31,33,35,37,39,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56], [25,-5,0,5,10,15,18,21,24,27,30,32,34,36,38,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55], [26,-6,-1,4,9,14,17,20,23,26,29,31,33,35,37,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54], [27,-7,-2,3,8,13,16,19,22,25,28,30,32,34,36,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53], [28,-8,-3,2,7,12,15,18,21,24,27,29,31,33,35,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52], [29,-9,-4,1,6,11,14,17,20,23,26,28,30,32,34,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51], [30,-10,-5,0,5,10,13,16,19,22,25,27,29,31,33,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50], ];     let mod = resultSwitchArray[queryResult][queryResult2];     let output = `&{template:default} {{name=Skill Challenge}} {{Result=[[d100 - ${mod}[Mod]]]}}`     let finalRoll = await startRoll(output);     finishRoll(finalRoll.rollId);   };   on('clicked:roll',initiateRoll); </script>
It can, like so &{template:default} {{name=Skill Challenge}} {{Result=[[d100 - ([[50 - (5*{?{Attack Ranks|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30}-1,4}kl1 + 3*{?{Attack Ranks}-5,0,5}k2dh1 + 2*{?{Attack Ranks}-10,0,5}k2dh1 + {?{Attack Ranks}-15,0}k1) + (5*{?{Defence Ranks|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30}-1,4}kl1 + 3*{?{Defence Ranks}-5,0,5}k2dh1 + 2*{?{Defence Ranks}-10,0,5}k2dh1 + {?{Defence Ranks}-15,0}k1)]])[mod]]]}}
1647293900
Oosh
Sheet Author
How on earth did you find the formula for that array lookup? Anything after the first index is enough to make me give up.
It's actually quite regular. Ignoring the 0-index entries that aren't part of the output both vertical and horizontal progression is the same. 4 steps of 5, then 5 steps of 3, 5 steps of 2, then steps of 1
1647310488
nice work on the algorithm, I put a little bit into trying to work it out but ended up giving up. And thank you for your help
1647322848
Oosh
Sheet Author
Huh, so it is. I was completely thrown by the 0-index, thinking it went 1 => 50 => 45 ....  I'll go back to sleep.
1647394883
I included the 0 index column and row because when using Array's in any sort of programing they are addressed from 0 to n.  
1648503656
RainbowEncoder said: It can, like so &{template:default} {{name=Skill Challenge}} {{Result=[[d100 - ([[50 - (5*{?{Attack Ranks|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30}-1,4}kl1 + 3*{?{Attack Ranks}-5,0,5}k2dh1 + 2*{?{Attack Ranks}-10,0,5}k2dh1 + {?{Attack Ranks}-15,0}k1) + (5*{?{Defence Ranks|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30}-1,4}kl1 + 3*{?{Defence Ranks}-5,0,5}k2dh1 + 2*{?{Defence Ranks}-10,0,5}k2dh1 + {?{Defence Ranks}-15,0}k1)]])[mod]]]}} Hi again, been trying to interpret your code, I was wondering what I would look up online to find information on what you did. I don't understand the ,4}kl1 in the first part of the algorithm or the ,0,5}k2dh1 or the ,0}k1 at the end any help would be appreciated thanks in advance
1648526197
Kraynic
Pro
Sheet Author
Carl P. said: I don't understand the ,4}kl1 in the first part of the algorithm or the ,0,5}k2dh1 or the ,0}k1 at the end any help would be appreciated thanks in advance You should check out the dice reference page for various chat command functions. <a href="https://wiki.roll20.net/Dice_Reference#Math_Operators_and_Functions" rel="nofollow">https://wiki.roll20.net/Dice_Reference#Math_Operators_and_Functions</a> <a href="https://wiki.roll20.net/Dice_Reference#Drop.2FKeep" rel="nofollow">https://wiki.roll20.net/Dice_Reference#Drop.2FKeep</a>
1648529008
Kraynic said: Carl P. said: I don't understand the ,4}kl1 in the first part of the algorithm or the ,0,5}k2dh1 or the ,0}k1 at the end any help would be appreciated thanks in advance You should check out the dice reference page for various chat command functions. <a href="https://wiki.roll20.net/Dice_Reference#Math_Operators_and_Functions" rel="nofollow">https://wiki.roll20.net/Dice_Reference#Math_Operators_and_Functions</a> <a href="https://wiki.roll20.net/Dice_Reference#Drop.2FKeep" rel="nofollow">https://wiki.roll20.net/Dice_Reference#Drop.2FKeep</a> Thanks, can you tell me why this &lt;input type="text" name='attr_level_rr2' value="({@{level},5}kl1)" disabled="true" &gt; produces an output result of 151? when level is set to 1 and a result of 551 when level is set to 5 and a result of 1051 when level is set to 10? by my reading of the modifier kl1 it should select the lesser of the two values
1648530245
Kraynic
Pro
Sheet Author
You would need to post the entire macro (or at least a complete calculation) that you are using that attribute in for anyone to have a guess.
1648531106
Kraynic said: You would need to post the entire macro (or at least a complete calculation) that you are using that attribute in for anyone to have a guess. its not a macro, I am trying to create a value in a character sheet, that is the entire line of code no, i don't know javascript, (one of the various languages I never learned back in the day when i was learning fortran and cobol :) )
1648541557

Edited 1648541570
GiGs
Pro
Sheet Author
API Scripter
One thing that can happen is for numbers to be treated as strings, and concatenated (joined togetehr). So the number 5 followed by the number 51 would produce 551. In your case it looks like all the non-alphanumeric characters are being removed, leving 151, or 551, or whatever, with the 51 coming from 5}kl1) It maybe that autocalc operations (where you have disabled=true) don't support roll20 macros, but need operations that are legal in HTML calculations. It's so long since I used autocalcs that I don't remember. I'd expect that behaviout if type="number", but its strange to me that it;s happening with type="text". The first thing I'd do is check for sheet corruption - create a new character and see if it behaves differently. Note that the code above isn't using javascript - it's using roll20's native roll code (actually, I'm just realising I don't know what that's called).
1648541860

Edited 1648542611
GiGs
Pro
Sheet Author
API Scripter
I knew I remembered something about this. A long, long time ago Brian posted about how you cant use the {}kl1 function in autocalcs, and came up with a brute force way to do it in html: <a href="https://app.roll20.net/forum/permalink/917740/" rel="nofollow">https://app.roll20.net/forum/permalink/917740/</a> Perosnally, I think these days you are far better off using a sheet worker. Here's a sheet worker. Remember this must be in your HTML file, and inside a script block: on ( 'change:level sheet:opened' , () =&gt; { &nbsp; getAttrs ([ 'level' ], v =&gt; { &nbsp; &nbsp; const level = + v . level || 0 ; &nbsp; &nbsp; const adjusted = Math . min ( level , 5 ); &nbsp; &nbsp; setAttr ({ &nbsp; &nbsp; &nbsp; level_rr2 : adjusted &nbsp; &nbsp; }); &nbsp; }); }); If you aren't aware, a script block looks simply like this: &lt;script type="text/worker"&gt; &lt;/script&gt; Put all your sheet workers between those two lines. Finally, you need to change your html: &lt;input type="text" name='attr_level_rr2' value="" readonly&gt; Sheet workers don't work with disabled="true" inputs.
1648542596
GiGs
Pro
Sheet Author
API Scripter
That said, if you wanted to use Brian's method, I think you'd change your HTML line to this: &lt;input type="number" name="attr_MIN" value="((@{level} + 5) - abs(@{level} - 5)) / 2" disabled="true" /&gt;
1648602916
Thanks Gigs, that is helpful. I've decided to go take classes in javascript so hopefully I can solve some of this stuff on my own moving forward.
1648604258

Edited 1648604414
GiGs
Pro
Sheet Author
API Scripter
JavaScript in roll20 sheetworkers is a pretty small part of what Javascript can be used for generally, and there are a few fairly simple patterns - once you master those, you can use it for everything you need. They look forbidding until you have used them for a while, but the vast majority of time, you are doing the same thing over and over, and it's pretty simple.