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

Set bar attribute in API?

1401814044

Edited 1401814220
DXWarlock
Sheet Author
API Scripter
Is it possible to set what attribute a bar represent in the API? I see I can: see what attribute its set to, the value of it, the max of it, and set the values, set if people can see it, and if they can edit it.. But nothing to pick what attrib its linked to. Our 'bar3' is ammo for their current weapon. when they switch weapons I have a script that reads the macro for the name and changes the values of that attribute, trying to auto change the bar also. If not, does someone know if its possible to allow the players to see the 'attribute' dropdown on bars?
Yes it is. Here is an exert from my combat resolution script that gets the value of bar3 (my HP) and then applies the damage to it. Also finds if its 0 and applies a status effect dead. if (success == true) { //Get min and max weapon damage values. //Min value is the number of dice rolled and max is the number of faces on the dice var minweapondam = parseInt(getAttrByName(who.id, 'damage')); var maxweapondam = parseInt(getAttrByName(who.id, 'damage', 'max') * minweapondam); //Simulate the damage roll var damageroll = randomInteger(maxweapondam); if (damageroll == 1) { damageroll = minweapondam; } var totaldamage = damageroll + strmod; var targetObj; var tokenPossibles = findObjs({_type: "graphic", _id: targetId}); if (tokenPossibles.length > 0) { targetObj = tokenPossibles[0]; } //Find out what the target's current HP is var currentHP = targetObj.get("bar3_value"); //Now reduce the current HP by the amount of damage roll from above currentHP = targetObj.set("bar3_value", parseInt(targetObj.get("bar3_value")) - damageroll); //If the target's HP is 0 or less... if (targetObj.get("bar3_value") <= 0) { //...set the Dead icon status on the token targetObj.set("status_dead", true); //and set the HP to 0 so the bar stays clean and neat with no over hanging negative damage currentHP = targetObj.set("bar3_value", 0); //Emote the killing blow! sendChat(msg.who, "/emas " + whoName + " has inflicted a mortal blow upon " + whoTargName + " and has slain them!"); } else { //If the target's HP isn't at or below 0 yet, emote how much damage was dealt. sendChat(msg.who, '/emas '+ whoName +' inflicts ' + damageroll + ' points of damage on ' + whoTargName + '!'); } }
1401818145

Edited 1401818412
Oh you know.. I read your post wrong. You can do this... double click the token, set the represents drop down to the character. Then set the drop downs for the bars to the Attribute you want to use. Here is the screen you are looking for:
1401818258

Edited 1401818507
DXWarlock
Sheet Author
API Scripter
Yea, but trying to do that automatically :) I've got my script doing everything else, finding the right attribute, subtracting one, if 0 announce out of ammo, etc..just setting the bar3 to that one Im not sure if its possible with it. Seems we can do everything else to a bar with API but change what it represents..was hoping I just missed that callback. opening each token, then changing the bars on 8 PC's each time they change a weapon slows the game down a bit..hehe
1401818445

Edited 1401818543
Oh I see. I will fiddle with my meager skills and see if I can find a solution.
bar#_link is what would be used, but it is read only attribute. Looks like we can not set the link via API.
1401820537

Edited 1401820595
Lithl
Pro
Sheet Author
API Scripter
You might want to double-check that it is in fact read-only. I wrote up the tables on the API:Objects page back in August, noting "Read Only" on the properties beginning with an underscore, such as _id -- that is the naming convention for protected fields in Roll20 objects. In the October update, a number of properties were changed from read-only to read-write, and Riley updated most of the documentation. Checking the history of the page, one of the changes he made was _bar#_link to bar#_link, but he did not change the description in the Notes column of the table.
I am actually trying it as we speak.
1401821817

Edited 1401822455
Here is what I came up with... but nothing is happening, and no errors. on('chat:message', function(msg) { if (msg.type != 'api') return; var parts = msg.content.split(' '); var command = parts.shift().substring(1); if (command == 'test') { var selectedId = parts[0]; //Get the token.id as tokens var selectedToken = getObj('graphic', selectedId); //Get the Name of tokens var whoName = selectedToken.get('name'); var who = getObj('character', selectedToken.get('represents')); if (!who) { who = selectedToken.get('name'); } var targetObj; var tokenPossibles = findObjs({_type: "graphic", _id: selectedId}); if (tokenPossibles.length > 0) { targetObj = tokenPossibles[0]; } targetObj.set("bar1_link", getAttrByName(who.id, "hit-points")); } });
1401822166

Edited 1401822175
Lithl
Pro
Sheet Author
API Scripter
getAttrByName gets the value of "hit-points", not the attribute which is named "hit-points". You need to use findObjs.
Yea... I realized this.
1401830442

Edited 1401831249
DXWarlock
Sheet Author
API Scripter
Crap i had my hopes up when you said it wasn't read only anymore..but seems the functionality doesn't work :\ (snippet) var curPageID = findObjs({_type: "campaign"})[0].get("playerpageid"); var tokens = findObjs({_type: "graphic", layer:"objects", _pageid: curPageID, name: msg.who}); _.each(tokens, function(id) { var who = getObj('character', id.get("_represents")); var aSet = findObjs({_type: "attribute",name: ammoType,_characterid: who.id}); aSet = aSet[0].get("name") id.set("bar3_link", aSet); }); doesn't seem to do anything if I change aSet to "current" and to bar3_value , it changes the value, same for _max, but _link does nothing :\
1401832325
Lithl
Pro
Sheet Author
API Scripter
Use aSet.id, not aSet.get('name')
1401832909
DXWarlock
Sheet Author
API Scripter
Thanks! that did it. So much less work during battles for me now..thanks to both of you are in order :)
Cool, I've been outside grilling for the family. Nice to know this works!