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

msg.content.set

I have been working with the chat event. My question is whether it is possible to have one part of the script read the chat message and then modify the content of the message before passing it along for evaluation by another part of the script. What I am trying to do is pass a parameter at the beginning that must be stripped out before the content is "translated" When I tried the following I received an error response of "TypeError: Object !Dwarven Hello there has no method 'set'" if(msg.content.indexOf('!Dwarven ') == 0) { unknownAlpha = 'Dwarven'; var msgTrim = msg.content.replace("!Dwarven ", "!"); msg.content.set(msgTrim); }
1437359830
Lithl
Pro
Sheet Author
API Scripter
set() is a function of Roll20 objects, such as characters, attributes, handouts, etc. It accepts either two parameters (a property name and a value) or one parameter (an object with keys of the property names to set). msg.content is not a Roll20 object, it is simply a string. I don't believe it's read-only either, so you could be able to simply use msg.content = msgTrim .
Yes, that did it. That was the missing piece to an enhancement to the "What Did He Say?" script. Once I got the script fully tweaked, I'll post it.
1437396673
The Aaron
Pro
API Scripter
Msg objects are passed to handlers in the order the handlers are registered. You may run into a problem since there isn't a way to explicitly control load order, other than manual concatenation. What problem are you trying to solve with this? There may be a more robust way of solving it.
In implementing Stephen S.' "What did he say?" script, it occurred to me I would want it to address a scenario such as this: The party encounters a creature speaking a language that none of them recognize (Language name = Unknown) The creature speaks using the Language(Unknown) sheet which renders it speech as a gibberish as per Stephen's script The party's bard succeeds on a local knowledge check and recognizes the language as Ignan. The GM then begins using the Language(Ignan) sheet for the creature now that the party as identified the type of language. To optimize the realism I realized that if I hardcoded a character set for Language(Unknown) , say the Dwarven character set, then when the GM switches to Language(Ignan), which uses the Draconic character set, the players would notice a difference in the characters of the gibberish. I've been working on an enhancement that allows me to select speaking As: Language(Unknown) and then pass in "!Draconic " in front of the body of the chat message. The script strips out the parameter string and sets the character set for Language(Unknown) to Draconic. Here is the current code block I have created. It does it successfully, but may not be the most elegant way of doing it: // Derek K. -- Check for Unknown alpha parameter if(msg.saidAs == "Language(Unknown)") // Must return a folse condition so the script will correctly assess the first legitimate character set if(msg.content.indexOf('!NeverUse ') == 0) unknownAlpha = 'NeverUse'; //Create one of these if statements and procedure sets for each character set if(msg.content.indexOf('!Common ') == 0) { unknownAlpha = 'Common'; var msgTrim = msg.content.replace("!Common ", "!"); msg.content = msgTrim; } if(msg.content.indexOf('!Dwarven ') == 0) { unknownAlpha = 'Dwarven'; var msgTrim = msg.content.replace("!Dwarven ", "!"); msg.content = msgTrim; } if(msg.content.indexOf('!Elven ') == 0) { unknownAlpha = 'Elven'; var msgTrim = msg.content.replace("!Elven ", "!"); msg.content = msgTrim; } if(msg.content.indexOf('!Draconic ') == 0) { unknownAlpha = 'Draconic'; var msgTrim = msg.content.replace("!Draconic ", "!"); msg.content = msgTrim; } if(msg.content.indexOf('!Infernal ') == 0) { unknownAlpha = 'Infernal'; var msgTrim = msg.content.replace("!Infernal ", "!"); msg.content = msgTrim; } I have worked with various syntaxes of the if statements and tried using switch. Any suggestions for improvement?
1437434132
The Aaron
Pro
API Scripter
I'm still not clear on exactly what you're doing, but something like this is what I'd probably write: var languages = ['elven','common','draconic','dwarven','infernal','neveruse'], // escape all the characters in a string that need to be escaped for Regular Expressions esRE = function (s) { var escapeForRegexp = /(\\|\/|\[|\]|\(|\)|\{|\}|\?|\+|\*|\||\.|\^|\$)/g; return s.replace(escapeForRegexp,"\\$1"); }, language = (function(){ var re = new RegExp('^!('+_.map(languages,esRE).join('|')+') ','i'); return function(s){ var res={ lang: 'unknown', msg: s }; res.msg=s.replace(re,function(c,m){ res.lang=m.toLowerCase(); if(res.lang === 'neveruse') { return c; } return '!'; }); return res; }; }()); It has the benefit of only having the languages specified in one place (and one special case for the neveruse language). I used lowercase for the language names (but the regular expression matches either case) because it's easier to get to a uniform key that way. Calling the language() function on a message context will return an object with lang and msg: language('!Dwarven foo bar baz'); { lang: "dwarven", msg: "!foo bar baz" } language('!neveruse foo bar baz'); { lang: "neveruse", msg: "!neveruse foo bar baz" } language('!banana foo bar baz'); { lang: "unknown", msg: "!banana foo bar baz" }
Ok. I will work with this in the script.