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

[Question] Populate character's avatar in chat when using /as or /emas, if an avatar exists?

If you have a Character in the Journal named "Paladin" with an avatar, you can manually set the chat dropdown to Paladin and then type to get the Paladin's avatar to display. However, if you type /as "Paladin" Testing No avatar displays, even if there is a Journal entry for "Paladin". Is there a way to use the API to do this?: 1) When using /as or /emas, check to see if a Journal entry for the character exists. If it does, speak using that character's avatar. 2) If no Journal entry exists, speak normally (no avatar) with /as. Thanks!
1404334537
Lithl
Pro
Sheet Author
API Scripter
The API will only consume API messages (those which begin with an exclamation mark), not other types of message. You can process information for other message types, but you can't prevent them from being posted to the chat. In other words, while you could detect which character the GM is trying to speak or emote as, it's already too late to make the avatar show up. You could create the API commands !as and !emas which work similarly to the /as and /emas commands; so long as you set the first parameter to sendChat as 'character|'+character.id the avatar should show up.
Ah ok, that makes sense. So I'd have to replace any instances of "as" and "emas" in my macros with !as and !emas, and then add an API script that sends any text entered with those commands. I'm a js & API newbie, I'm just now taking the courses on codeacademy. This is what I started with and I keep getting an unidentified error -- any idea how such an script for !as and !emas ought to look? on("chat:message", function (msg) { if (msg.type != "api") return; var command = msg.content { if (command == "!as") var n = msg.content sendChat("character|+character.id", n) } }
1404345245

Edited 1404345313
The Aaron
Roll20 Production Team
API Scripter
You should mark that as code (upper left corner, click the paragraph sign ¶ and select code ) so it's easier to understand what you are writing. This is your code, reformatted: on("chat:message", function (msg) { if (msg.type != "api") return; var command = msg.content { if (command == "!as") var n = msg.content sendChat("character|+character.id", n) } } Your primary problem is the missing ); which would close the on() function call. A few pointers. You should always use ; on the end of your lines. You should always put { } around statements following an if(). Don't put { } around things that aren't blocks controlled by conditionals like if(). You should also shun the == and != in favor of === and !== . The shorter versions do type coercion which can be unpredictable and is not commutative. You probably intended the following code: on("chat:message", function (msg) { if (msg.type !== "api") { return; } var parts=msg.content.split(' '); var command = parts[0]; if (command === "!as") { var character_name = parts[1]; var characters=findObjs({ type: 'character', name: character_name }, {caseInsensitive: true}); if(characters.length) { var saying = _.rest(parts,2).join(' '); sendChat("character|"+characters[0].id, saying); } else { log("!as -- couldn't find character named '"+character_name+"'!"); } } }); You need to split the command out of the front of the contents, or find it and check the offset. I prefer the split method, so that's what I added. You also need to lookup the character by the name passed to !as as the second parameter. You can pass an object describing what you want to findObjs() to get an array of matching characters. If there are any, this uses the first one to speak, otherwise it logs the lack of a matching character. When I tried this, it worked, but I didn't get the portrait of the speaker the way I do with changing my speaking as. I highly recommend Javascript: the Good Parts, by Douglas Crockford [1] . I just read it and it really opened my eyes to Javascript's foibles. Additionally, JSLint can be a godsend for finding issues in your code. Hope that helps, and feel free to PM me with any JS issues or scripting help! =D [1] Javascript: the Good Parts, by Douglas Crockford