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

Stuck on chat message parse

From Chat Events section in the wiki: "Triggered whenever a new chat message is received. Note that if the message is of type "rollresult", you will need to call  JSON.parse()  on the content of the message to get an object which contains information on the roll results." Any chance of an example with this statement and the subsequent use of the parsed "rollresult" message? The logic of the script I want to write is easy but if I have the syntax of this bit wrong I will not get far. I need the number of sides, the compare point value and the actual roll from the message.
Try doing:     if (msg.type == "rollresult") {         log(msg);     } That'll show you how a roll is constructed and you can look for what you need.
There are two examples provided on the wiki. You have click the little "Expand" buttons next to them, though.
I saw those. It shows what the message contains. My problem as a novice javascript programmer is working out how to use it. Do I need to write a JSON reviver function? Or is the parse enough? I can trap the appropriate events but I need to pull out the necessary properties.
It would look something like: var foo = JSON.parse(msg.content); Then your "foo" variable will be an object with each of those properties. The parse function is the "reviver".
Ryan L said: It would look something like: var foo = JSON.parse(msg.content); Then your "foo" variable will be an object with each of those properties. The parse function is the "reviver". Yep! A complete example might be: on("chat:message", function(msg) { var rollinfo = JSON.parse(msg.content); //Do something with rollinfo, it will be similar to what's shown int he wiki examples. You could also do: log(rollinfo); //...to get an idea of what properties it contains. });
Thanks guys.  That is what I thought was meant. I am not doing anything complicated - just testing the roll result for D100 rolls against the thresholds for 'special success' , 'critical success' and 'fumble' Just some if statements. Now to find the bugs.
before 'parse' {"content":"{\"type\":\"V\",\"rolls\":[{\"type\":\"G\",\"rolls\":[[{\"type\":\"R\",\"dice\":1,\"sides\":100,\"mods\":{},\"results\":[{\"v\":77}]}]],\"mods\":{\"success\":{\"comp\":\"<=\",\"point\":75}},\"resultType\":\"sum\",\"results\":[{\"v\":77}]}],\"resultType\":\"success\",\"total\":0}","origRoll":"{1d100}<75","playerid":"-Iq2KiGM4NklQAq-UoLX","type":"rollresult","who":"LochaberAxe (GM)"} after 'parse' to rollinfo object? {"type":"V","rolls":[{"type":"G","rolls":[[{"type":"R","dice":1,"sides":100,"mods":{},"results":[{"v":77}]}]],"mods":{"success":{"comp":"<=","point":75}},"resultType":"sum","results":[{"v":77}]}],"resultType":"success","total":0} rollinfo.sides does not = 100. Should I have something to indicate that this is the first in an array of 1 dice? Given that the 'total' is zero when I have a compare point do I need to do something similar for rollinfo.results?
I'm not totally sure if I understand the question, but: rollinfo.rolls is the array that contains each type of roll. In this case it's a bit more complicated since you did a "group" roll. A group object is like a rollinfo object nested inside a rollinfo object, basically. So in this case: rollinfo.rolls[0].type == "G", telling you this was a group, which means you have to do: rollinfo.rolls[0].rolls to get the "real" rolls object for the group.  rollinfo.rolls[0].rolls.length == 1, telling you there was 1 type of dice rolled rollinfo.rolls[0].rolls[0].sides == 100, telling you the first set of dice rolled (and the only one in this case) is a d100 rollinfo.rolls[0].rolls[0].dice == 1, telling you it was a "1d100" (as opposed to, for example, 4d100) rollinfo.rolls[0].rolls[0].results is an array of the results of the roll, rollinfo.rolls[0].rolls[0].results.length === rollinfo.rolls[0].rolls[0].dice Hope that helps.