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

Sending message to Chat after function being used on dice results

I have been working on a piece of code to add "focus" to dice rolls which are smaller than a given threshold.  The code below seems to parse through the result of a roll correctly and add focus to any dice that is rolled too low (using 7 as a debug number since I am rolling d6's). The only problem is that the message doesn't go to the chat, even though I don't get any errors in the console and I can get the value of the dice roll and templateStr before and after the addition of focus to the dice roll. Can anyone tell me what is wrong with the code below, even though it is lacking some context for what is going on. sendChat(character.get('name'), templateStr, function(msg) {             if(skill.focus != 0) {                 var focus_left = skill.focus;                 var cnt = parseInt(skill.training) - 1 + parseInt(attrValue);                 for(; cnt >= 0; --cnt) {                     while(msg[0].inlinerolls[0].results.rolls[0].rolls[0][0].results[cnt].v < 7 && focus_left != 0) {                         msg[0].inlinerolls[0].results.rolls[0].rolls[0][0].results[cnt].v += 1;                         focus_left -= 1;                     }                     if(focus_left == 0) {                         return;                     }                 }             }         });
1591031778

Edited 1591032373
GiGs
Pro
Sheet Author
API Scripter
When you use sendChat with a callback, the message doesnt get displayed in chat. It gets sent to the roll processor, and returns an object you can use in the body of the callback. Also this is an asynchronous operation, so whatever result you get and processing you do with it, you need to construct your script in a particular way to be able to do anything with it.  Can you describe what you are doing, and how this fits in the context of your larger script? I'm wondering if you really need a sendchat with a callback to do this. If the script is constructed to be called with a roll, you can do this in the body of the script, and don't need a separate sendChat until you're ready to send the final processed result.
A very short description of what I am doing 1) Pull some numbers from the character sheet to calculate the number of dice to roll 2) Roll "Xd6s" to get the "pure" dice roll result 3) Use Y "focus" to improve the dice which are currently not above a certain threshold Z, i.e. as written in a previous post " Each level of focus results in the ability to add +1 to a single dice in the roll, so for instance a character with 2 focus would get the following results of a skill check with a difficulty of 4 with 5 dice. (1,3,3,4,6) = four succeses (+1 to each of the 3's) (1,1,2,4,6) = three succeses (+2 to the 2) " 4) Check if the dice value is above a certain threshold and report how many dice were (currently just handled by having 2) be written as "Xd6s>Z" ) 5) Send the result back to the chat where it is processed through a rolltemplate  for output
1591059131
GiGs
Pro
Sheet Author
API Scripter
From that description, there's no need to use a callback. Just do your processing in the script body, get the output you want, then send it using a sendChat without a callback. It doesnt look like the fucntion you have described above does exactly what you need - calculating how best to spend the focus could be a tricky calculation, at least if the focus number gets very high. If you post the full script in a gist.github.com or pastebin.com or similar site, we can make suggestions on how to make the script work.
1591059551

Edited 1591059637
How do I call the dice roller without using a sendChat function then? And the number of focus doesn't get higher than three, so it is not that complicated
1591060571
GiGs
Pro
Sheet Author
API Scripter
You include the dice roll in the command that initiates the script.  If that doesnt make sense, post the command you use to activate the script, and I can show you.
But I cannot include the dice roll in the command, since the command is like this "!r #skillname" and then it will fetch the values to determine how to make the dice roll during the script. The code I am working on is based on the Roleplaying is Magic e4 character sheet:&nbsp; <a href="https://github.com/Cazra/roll20-character-sheets/blob/master/RoleplayingIsMagic_4E/mlp_rim4.html" rel="nofollow">https://github.com/Cazra/roll20-character-sheets/blob/master/RoleplayingIsMagic_4E/mlp_rim4.html</a> &nbsp;and dice roller:&nbsp; <a href="https://github.com/Roll20/roll20-api-scripts/tree/master/RoleplayingIsMagic_4E_Dice/1.1" rel="nofollow">https://github.com/Roll20/roll20-api-scripts/tree/master/RoleplayingIsMagic_4E_Dice/1.1</a> Which is rather old, but I thought it was a good starting point for altering small things slowly to make it work for my purposes.
After trying many things I finally found the solution. It turns out that since the dice roller is asynchronous I need to encapsulate the roll seperately and wait for it to be resolved as detailed in the following thread&nbsp; <a href="https://app.roll20.net/forum/post/8157739/extract-roll-result-from-sendchat-callback" rel="nofollow">https://app.roll20.net/forum/post/8157739/extract-roll-result-from-sendchat-callback</a> Only thing I am wondering if it is possible to have the result still be displayed as an inline roll, right now I can only return the result (number of succeses) as an int, which isn't so nice.