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

Heal on Character Sheet

Hi, I'm new to the API but I was able to get both of these lines to individually work in chat without the API and was wondering if there was anything I could do to make this work. Basically I want to add hit-points to a player character sheet using chatsetattr. on("chat:message", function(msg) {     if (msg.content == "HealingPotion") {         var a = randomInteger(4) + randomInteger(4) + 2;         sendChat("@{character_id}", "/em uses a Healing potion to Recover " + a + " Hit Points");         sendChat("@{character_id}", "!setattr --sel --mod --HP" + a);     } }); The main issue with this is that I can't get it to reference a specific character. Is there anyway I can do this? I would even be willing to create a seperate API for each individual character sheet although Ideally it would just heal the selected tokens character sheet.
1593148172
GiGs
Pro
Sheet Author
API Scripter
msg.selected[0] will give you the selected character (or the first character if you have a group), and you can get the character id from that. If using a script you dont need to use chatsetattr. You can just set the value in the script itself.
What would be the proper syntax to use msg.selected[0] with my current script?
I made some changes but the main issue I've been running into is getMyCharacter coming back undefined even if I have a character selected and I am playing as that character in game. If I could get this variable to return reliably or find some other work around it would make it so a character's sheet is directly edited by this script so that they have additional hit points. as a side note, is there any way to keep the hp from increasing past their maximum. on("chat:message", function(msg) {     if (msg.content == "HealingPotion") {                  var character = getMyCharacter();                  var a = randomInteger(4) + randomInteger(4) + 2;                  sendChat("example", "character.id")                  sendChat(character.id, "/em uses a Healing potion to Recover " + a + " Hit Points");         sendChat(character.id,"!setattr --name " + character.id + " --mod --HP|" + a);              } });
1593153676
GiGs
Pro
Sheet Author
API Scripter
You can get the character object, from the selected, but only if the token has its represents set. These three commands will do that:             const selected = msg.selected[0];             const token = getObj('graphic', selected._id);             const char = getObj('character', token.get('represents')); You might need to have some error checking in there, to make sure a token is selected, and that token represents a character. Its a bit simpler to require the character id is supplied in the command that launches the script. Here's a quick-and-dirty script to apply healing to a character to represent that: You must call it with this format: !healingpotion @{selected|character_id} [[2d4+2]] This method allows you to change the way a character is selected, like !healingpotion @{Bob|character_id} [[?{Enter healing amount|2d4+2}]] Here's the script, with some error checking (might not be complete). on('ready', function () {     on('chat:message', function (msg) {         if ('api' === msg.type && msg.content.toLowerCase().startsWith('!healingpotion ')) {             const args = msg.content.split(/\s/);             const cid = args.length > 1 ? args[1] : 'error';             if (cid === 'error') {                 sendChat('Healing Potion', '/w GM no character selected.');                 return;             }             const roll = msg.inlinerolls && msg.inlinerolls.length > 0 ? msg.inlinerolls[0].results.total : 'error';             if (roll === 'error') {                 sendChat('Healing Potion', '/w GM no inline roll detected.');                 return;             }             const hp = findObjs({                 type: 'attribute',                 characterid: cid,                 name: 'hp'             })[0] || 'error';             if (hp === 'error') {                 sendChat('Healing Potion', '/w GM no HP attribute for Healing Potion');                 return;             }                          const current = parseInt(hp.get('current')) || 0;             const max = parseInt(hp.get('max')) || 0;             if(current === 0 || max ===0 ) {                 sendChat('Healing Potion', "/w GM the character's HP values are not valid.");                 return;             }             if (current >= max) {                 sendChat('character|' + cid, '/em tries to use a Healing potion, but as already at full health.');                     return;             }             hp.set('current', Math.min(current + roll, max));             sendChat('character|' + cid, '/em uses a Healing potion to Recover ' + roll + ' Hit Points.');             }     }); });
1593154034
GiGs
Pro
Sheet Author
API Scripter
Alex said: I made some changes but the main issue I've been running into is getMyCharacter coming back undefined even if I have a character selected and I am playing as that character in game.  I've never heard of a getMyCharacter() function, but I see now its mentioned in an example on the wiki. I wonder if that's an error.
1593179971

Edited 1593181401
Alex
Pro
Dang your good. This works exactly the way I wanted it too. This has been a huge help!!! Thanks!!!
1593194994
GiGs
Pro
Sheet Author
API Scripter
That's great :)
Hey, I had one other question. In the last line where you display:  sendChat('character|' + cid, '/em uses a Healing potion to Recover ' + roll + ' Hit Points.');   Is there anyway to display the inline roll where the + roll + is such that a player can hover over it and see the roll? If not it's just me nitpicking at this point and just wondering for my own curiosity. I tried replacing roll with msg.inlinerolls[0].results.total but that just shows the total again. 
1593213473
GiGs
Pro
Sheet Author
API Scripter
It is possible, but it involves adding a lot of css styling to the roll part (and might need changing that entire line to accommodate it, I'm not sure). That's a bit more work that I can put in.
Is there anyway to just create the inline roll in chat and then just grab that value from chat
1593213667

Edited 1593213694
Alex
Pro
Such as using  sendChat('character|' + cid, '/em uses a Healing potion to Recover ' + roll + ' Hit Points.');   Initially but replacing 'roll' with [[2d4+2]] and just grabbing that value.
1593214252
GiGs
Pro
Sheet Author
API Scripter
Not if you want to apply that roll to the character's HP. Technically you can send an inline roll to chat from the API, but it is never displayed to the user, so it's no good for this purpose. Note that the script already grabs an inline roll - the one you send in the initial command. 
1593214848

Edited 1593214888
Alex
Pro
My goal is essentially at the end of the code: sendChat('character|' + cid, '/em consumes a Healing potion to Recover + [[2d4+2]] + Hit Points.');                             const roll = msg.inlinerolls && msg.inlinerolls.length > 0 ? msg.inlinerolls[0].results.total : 'error';             if (roll === 'error') {                 sendChat('Healing Potion', '/w GM no inline roll detected.');                 return;             }             hp.set('current', Math.min(current + roll, max)); To grab the result of the 2d4+2 sent to chat in this line and set the variable roll = to whatever that result is.
1593217541
GiGs
Pro
Sheet Author
API Scripter
That doesnt actually work, because of the asynchronous nature of sendChat. See the callback section here . But if it did work, you'd be in exactly the same position you're already in: there is an inline roll in the initial !healingpotion command, and if you could see it, it would be formatted the same as one outputted to the window. But you just cant copy the roll from there. You have to create the format from scratch and apply it to your roll text.