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

change:token

1381078115

Edited 1381081583
Stephen Koontz
Forum Champion
Marketplace Creator
Sheet Author
API Scripter
Compendium Curator
So, I'm stumped. I run the following script and when I change a token it gets the status pummeled but the three logs come back as 'undefined' and I don't know why function(obj) doesn't allow me to see the id, type, or subtype of the token object. on("change:token", function(obj) { log(obj._id) log(obj._type) log(obj._subtype) obj.set({"status_pummeled": true}); });
1381083126

Edited 1381083160
Riley D.
Roll20 Team
You need to do obj.get("_id"), obj.get("_type"), obj.get("_subtype"), etc. If that still doesn't work let me know.
1381089850
Lithl
Pro
Sheet Author
API Scripter
on('change:token', function(obj, prev) { log(obj.id + ' ' + prev._id); log(obj.get('type') + ' ' + prev._type); log(obj.get('subtype') + ' ' + prev._subtype); }); `obj' is an actual reference to the Roll20 object, and as such you need to access its properties with the get method. `_id' is a special exception to this rule; while you can access it via get , you can also access it with ".id". It's also worth noting that the get method will add the leading underscore to properties if needed. `prev', on the other hand, is a normal javascript object containing the values that used to be in the Roll20 object. All of its properties must be accessed directly, and you have to remember your underscores.
1381102702
Stephen Koontz
Forum Champion
Marketplace Creator
Sheet Author
API Scripter
Compendium Curator
Ok. Thanks guys. I figured it out. Thank you for the explanation of how the Roll20 objects differ. Random side question. Is it possible to have a function call another roll20 script or trigger an event like 'change:token' on referenced token?
1381105026
Lithl
Pro
Sheet Author
API Scripter
All of the scripts in a campaign are running in the same global context, so you can certainly call one function from "another" script. The change event will trigger any time an alteration is made to one of the token's properties.
1381282715
Stephen Koontz
Forum Champion
Marketplace Creator
Sheet Author
API Scripter
Compendium Curator
I'm not sure I understand then. For example I have an API script that is triggered by a msg that changes a value on the attached token's character sheet and then adds a status icon to the token. I have a separate script that triggers on token change. However, the first script adding the status icon doesn't trigger the 2nd script. I want the first script to call/force the 2nd script.
1381284057
Lithl
Pro
Sheet Author
API Scripter
Hmm... could you post your code? Also, while it shouldn't matter in this instance since we're talking about event handlers on two different event types, could you make note of which script is in which tab (the leftmost tab is executed first, followed down the line to the rightmost tab).
1381285722
Stephen Koontz
Forum Champion
Marketplace Creator
Sheet Author
API Scripter
Compendium Curator
Sure. So, I did a damage indicator based on your !fly script. Bashing is to the left of the Wounded script. When Bashing is called it changed the Bashing attribute on the sheet and it applies a status marker to the token. The Wounded script is adjusting the markers if the stat is updated on the sheet and applying a pummeled status if the number of wounds adds up to the health attribute -2 or more. However, when calling the Bashing (there is also a Lethal and Aggravated script), it doesn't trigger the Wounded script. So, the pummeled icon doesn't appear until after you move or adjust the token manually. Bashing on('chat:message', function(msg) { if(msg.type != 'api') return; var parts = msg.content.toLowerCase().split(' '); var command = parts.shift().substring(1); var selected = msg.selected; if(command != 'b' || !selected) return; // use the !b command, and have one or more things selected var damage = +parts[0]; if(!damage) damage = 0; // if no damage is returned, treat as 0 _.each(selected, function(obj) { if(obj._type != 'graphic') return; // only damage graphics var tok = getObj("graphic", obj._id); if(tok.get('subtype') != 'token') return; // don't try to damage cards var oCharacter = getObj("character", tok.get("represents")); if (oCharacter != undefined ) { var oHealth = findObjs({name: "Bashing",_type: "attribute", _characterid: oCharacter.id}, {caseInsensitive: true})[0]; } if (oHealth != undefined ) { oHealth.set('current', damage) } tok.set('status_fist', false); var dmgicon = ''; while(damage > 0) { // get current digit, starting from ones var digit = damage / 10; digit -= Math.floor(digit); digit = Math.round(digit * 10); // shift damage damage = Math.floor(damage / 10); dmgicon += 'fist@'+digit+','; } if(dmgicon.length > 0) dmgicon = dmgicon.substring(0, dmgicon.length - 1); // trim trailing comma var markers = tok.get('statusmarkers'); if(markers != '') markers += ','; markers += dmgicon; tok.set('statusmarkers', markers); }); }); Wounded on('change:token', function(obj) { //if(obj.get('_type') != 'graphic') return; var tok = getObj('graphic', obj.get('_id')); //if(tok.get('subtype') != 'token') return; // don't try to damage cards if(tok.get('represents') == '') return; // don't damage tokens without attached characters var oCharacter = getObj('character', tok.get('represents')); var oBashing = findObjs({name: 'Bashing',_type: 'attribute', _characterid: oCharacter.id}, {caseInsensitive: true})[0]; if(oBashing == undefined) return; if(oBashing.get('current')>0 && oBashing.get('current')<10) {obj.set('status_fist', oBashing.get('current'))} else if (oBashing.get('current')<10) {obj.set('status_fist', false)}; var oLethal = findObjs({name: 'Lethal',_type: 'attribute', _characterid: oCharacter.id}, {caseInsensitive: true})[0]; if(oLethal == undefined) return; if(oLethal.get('current')>0 && oLethal.get('current')<10) {obj.set('status_skull', oLethal.get('current'))} else if (oLethal.get('current')<10) {obj.set('status_skull', false)}; var oAggrivated = findObjs({name: 'Aggrivated',_type: 'attribute', _characterid: oCharacter.id}, {caseInsensitive: true})[0]; if(oAggrivated == undefined) return; if(oAggrivated.get('current')>0 && oAggrivated.get('current')<10) {obj.set('status_chemical-bolt', oAggrivated.get('current'))} else if (oAggrivated.get('current')<10) {obj.set('status_chemical-bolt', false)}; var oHealth = findObjs({name: 'Health',_type: 'attribute', _characterid: oCharacter.id}, {caseInsensitive: true})[0]; if(oHealth == undefined) return; var oEffHealth = parseInt((oHealth.get('current')),10) - (parseInt((oBashing.get('current')),10) + parseInt((oLethal.get('current')),10) + parseInt((oAggrivated.get('current')),10)) if(oEffHealth == 2) {obj.set('status_pummeled', '1');} else if(oEffHealth == 1) {obj.set('status_pummeled', '2');} else if(oEffHealth <= 0) {obj.set('status_pummeled', '3');} else obj.set('status_pummeled', false); });
1381343272
Stephen Koontz
Forum Champion
Marketplace Creator
Sheet Author
API Scripter
Compendium Curator
So, I solved my problem by turning the Wounded script into a function and passing the object to the function whenever I run the Bashing script.
1381453554
Lithl
Pro
Sheet Author
API Scripter
Huh. I wouldn't have expected calls to set to not trigger change events,but that appears to be the case. Your solution is the same one I see.