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

First basic utility script

1465155688

Edited 1465157087
I want to write a script that watches for a token to be moved, and any time it is moved it makes sure the bars are set. It doesn't seem to be working. Since I'm not familiar with the API, it's difficult to tell if I'm not properly packaging my instructions, or if my instructions are incorrect. Any ideas? on("change:graphic",function(obj) {     if (obj.get("bar1_link")==""){         obj.set("bar1_link","hp_temp");         obj.set("bar2_link","ac");         obj.set("bar3_link","hp");     } }) EDIT: I'm also trying to get the script to check to see if gender is set on the linked character. Since that is a custom attribute that I'm adding, how do I if () test for whether an attribute exists? for this, I imagine:    having to API test to see if gen1 exists on the character that the selected token represents    call a macro to pull up a dialog to ask what the gender should be    have the macro run a chat string that would be parsed to set the gender of the token.
1465160486
Lithl
Pro
Sheet Author
API Scripter
The values for barN_link need to be the ID values of an Attribute object, not the name of an attribute.
1465165324

Edited 1465172785
So, this should work, then? This did not work on("change:graphic",function(obj) {     if (obj.get("represents")!=""){         var character = getObj("character", obj.get("represents"));         if (obj.get("bar1_link")==""){             obj.set("bar1_link",getAttrByName(character.id,"hp_temp"));             obj.set("bar2_link",getAttrByName(character.id,"ac"));             obj.set("bar3_link",getAttrByName(character.id,"hp"));         }     } })
Looks like it.
I have it working now, It links the bar with the stat correctly, but the values are not updating correctly.  on("change:graphic",function(obj) {     var character = getObj("character", obj.get("represents")); //successfully retrieves character ID for current model     if (obj.get("represents")!=""){        if (obj.get("bar1_link")==""||obj.get("bar2_link")==""||obj.get("bar3_link")==""){             sendChat(character.get("name"),"/w GM My bars were not correctly linked, so I fixed that.")             obj.set("bar1_link","sheetattr_hp_temp");             var armorClass= findObjs({                 _type: "attribute",                 _characterid: character.id,                 name: "ac"             });             obj.set("bar2_link",armorClass[0].id);             var hitPoints= findObjs({                 _type: "attribute",                 _characterid: character.id,                 name: "hp"             });             obj.set("bar3_link",hitPoints[0].id);         }        if (obj.get("bar1_value")==""||obj.get("bar2_value")==""||obj.get("bar3_value")==""){             sendChat(character.get("name"),"/w GM Help! My bar values are not correctly set: Bar1: "+obj.get("bar1_value")+" Bar2: "+obj.get("bar2_value")+" Bar3: "+obj.get("bar3_value"));                   }     }