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

How to make script print out character/as/token name

April 26 (8 years ago)

Edited April 26 (8 years ago)
So, I am using this script, it's a dice result reader basically .

It works just fine, but the thing is it prints out the Player name ( sendChat("player|"+e.playerid, "/desc...)
and I think it takes out some of the immersion of the Role Playing, because players get to see things like "Goblin attacks PlayerX and... The Master misses!"

Players can just change their names to their characters' but I, as GM, can't. I'm always using dozens of characters/tokens.

So what I wanted to know is what should I do for the script to print out the result with Character / "as" / Token name?

I appreciate your help, guys.
April 26 (8 years ago)

Edited April 26 (8 years ago)
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
From a quick read through, it looks like it will need a nearly full rewrite and most likely a change to the command syntax to accomplish that. The script only reacts to messages, and characters are not attached to messages.
The script isn't token-based. All it does is spit out the name of the player who triggered it. To get it to do otherwise, you'll have to add logic that identifies who's speaking, and put that in place of "player|"+e.playerid in the sendChat() function.

One possibility: allow the user to add a name at the end of a roll. For instance:
/roll 1d12 + 3d6 > 14 Bob the Awesome
This will roll the dice normally and the script will print the success or failure message, but the name at the end will be ignored by the roller.

You'd have to alter the script to get it to recognize whether there's anything after the target number. If there isn't, send the player's ID to sendChat() as exists already. If there is, capture it and send that text to sendChat() instead.
Or, if rolling as a Character (not a Token) is good enough, you can replace "player|"+e.playerid wherever you see e.who and then you can choose a Character to roll as in the drop-down box below the Chat window.
April 26 (8 years ago)
Lithl
Pro
Sheet Author
API Scripter
The /desc command doesn't print who sent it. The first argument to sendChat is essentially ignored in this case. The issue here is that the script is reacting to a /roll command, and printing a message after it. The script doesn't (and cannot) do anything to the output of the /roll command. If your "As" dropdown is selecting you the player, your /roll will report as such. If you select a character you have control of, the /roll command will use that character.

David T. said:
Or, if rolling as a Character (not a Token) is good enough, you can replace "player|"+e.playerid wherever you see e.who and then you can choose a Character to roll as in the drop-down box below the Chat window.
Using an arbitrary string as the first parameter to sendChat is equivalent to using /as or /emas; while it will print the name you've selected, the message isn't actually associated with a character.

If you want to actually send a message as a character, the parameter needs to be 'character|'+characterid. Here's an example of a function that will figure out who to send a message as:
function automaticSendChat(msgObj, message, callback) {
const characters = findObjs({type: 'character'});
let speaking;
characters.forEach((chr) => (chr.get('name') === msgObj.who) && (speaking = chr));

if (speaking) sendChat(`character|${speaking.id}`, message, callback);
else sendChat(`player|${msgObj.playerid}`, message, callback);
}

// usage:
on('chat:message', function(msg) {
automaticSendChat(msg, '/em said something');
});
April 26 (8 years ago)
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Oh, nice catch Brian, completely missed that he was talking about the /desc output.
I can't reply only to pieces of that; the stupid editor won't let me.

"If your "As" dropdown is selecting you the player, your /roll will report as such. If you select a character you have control of, the /roll command will use that character."

The key here is "character you have control of." If you don't explicitly give yourself control of the character, even if you're the GM, it won't work. That's what I was trying until you pointed this out. You can't rely on the default permissions.

"Using an arbitrary string as the first parameter to sendChat is equivalent to using /as or /emas; while it will print the name you've selected, the message isn't actually associated with a character."

Of course; I didn't mean to imply otherwise.
Thank you for your help, guys.



If you want to actually send a message as a character, the parameter needs to be 'character|'+characterid. Here's an example of a function that will figure out who to send a message as:
function automaticSendChat(msgObj, message, callback) {
const characters = findObjs({type: 'character'});
let speaking;
characters.forEach((chr) => (chr.get('name') === msgObj.who) && (speaking = chr));

if (speaking) sendChat(`character|${speaking.id}`, message, callback);
else sendChat(`player|${msgObj.playerid}`, message, callback);
}

// usage:
on('chat:message', function(msg) {
automaticSendChat(msg, '/em said something');
});

I think you got the point, Brian. And I really don't care if the massage relates with the character name, the "as" name or the token name, or even if it's just like an /em command, I just need the feel of it not showing "The Master" anymore.

I just don't know how to implement it now. I know it isn't just as simple as replacing "player|"+e.playerid  for 'character|'+characterid. I am really no coder though, so I'm clueless about how to edit the script.
April 27 (8 years ago)
Lithl
Pro
Sheet Author
API Scripter

David T. said:

"If your "As" dropdown is selecting you the player, your /roll will report as such. If you select a character you have control of, the /roll command will use that character."

The key here is "character you have control of." If you don't explicitly give yourself control of the character, even if you're the GM, it won't work. That's what I was trying until you pointed this out. You can't rely on the default permissions.
The GM can select any character in the campaign, without needing any permissions set on it.

João Vitor M. said:
I think you got the point, Brian. And I really don't care if the massage relates with the character name, the "as" name or the token name, or even if it's just like an /em command, I just need the feel of it not showing "The Master" anymore.
The script you linked should not be displaying any character or player name at all. The name being displayed is coming from the /roll command you're using to trigger the script. The only way to change that name is by using the "As" dropdown. The only way to eliminate the name entirely would require pretty much an entire rewrite of the script.
Have you tried the script yourself, Brian? I did, and changing the drop-down only worked when I had explicitly given myself control of a character; default permissions did not work.

The script DOES display a name. If I am David (GM) in the game, it displays things like "David (GM) succeeds!" If I give myself explicit permission to control the character Bob, then roll as Bob, it displays things like "Bob succeeds!" This is not displayed inside the roll; it is displayed inside a desc after the roll. When I get a chance I'll post a screenshot.
Okay, I take it back. After I altered the script a bit as described above that's how it worked, but as it exists in GitHub the chosen character makes no difference.

I'll shut up now. :o
David, I see what you're saying but it seems it just doesn't work this way for me.

I have a character, I have explicit permission to control it, I am using it's name on "As" dropdown (wich by the way would be the simplest way to work with the script, I believe).
But even so, it keeps on spitting "The GM" fails!
The only way I can change it, is by changing my player name on my settings. So, if you could get it done by changing your "As" dropdown, I'd like to know how you did it.

David T. said:

Okay, I take it back. After I altered the script a bit as described above that's how it worked, but as it exists in GitHub the chosen character makes no difference.

I'll shut up now. :o

Yeah, I figured as much. Could you share with us your alterations on the script?
Do what I said above: replace every instance of

"player|"+e.playerid

with

e.who

I got this result:


The first roll was done as David (my player name); the second was done as Tom (a character that has no explicitly set permissions); the third was done as Bob (a character that is explicitly set to David for viewing and control).

David T. said:

Do what I said above: replace every instance of

"player|"+e.playerid

with

e.who
Oh, I missed that before.
It works just perfect now. That's just what I needed.

Thank you David! Now the fellowship can keep the immersion rolling!
Thank you all guys, for the collaboration.