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

[Help] Can't figure out how to set an attribute with API

1537134705

Edited 1537135223
Hey Roll20 API forum, I'm new to scripting, so new as to never had used javascript until today and also evidently new to the Roll20 API as well. Never the less I have programmed for a while so I have a fundamental understanding for it. The issue I'm having is I can read and create an object of an attribute from a sheet that is connected to a token on the board using the findObj function but I fail to manage to then alter the value with something new. I have provided how I'm reading this attribute and tried to change it below hopefully someone can help me. <a href="https://gist.github.com/banebu13/45cb3bbd7cedc7378ab9adebd466c4c7" rel="nofollow">https://gist.github.com/banebu13/45cb3bbd7cedc7378ab9adebd466c4c7</a> I'm working on a script that allows for temp hp to work with the token without adding it to the bars as long as its on the the sheet connected to the token. The script is supposed to check for temporary hit points if there is any remove those first on the sheet and then remove hit points. I.e. I have a token that's connected to my character sheet. My character currently benefits from 5 temporary hit points and is hit by an attack. Lets say the attack does 3 damage, I press my token and in the corresponding bar for hp I deduce 3. I now have 2 temporary hit points and my hp is still full. If I take another attack of 3 my temporary hit points go down to 0 and I take 1 point of damage to my main hit point pool. I've got most of that functionality working except I can't get it to actually decrease the temporary hit points so I'm hoping someone here can help. I intend to post the whole script publicly when I finish it. Edit: Also how do people get their code into this post rather than linking to elsewhere haha
1537138186
keithcurtis
Forum Champion
Marketplace Creator
API Scripter
Hi banebu, Welcome to the world of API scripting. Before you bang your head too much on this problem, a very similar script was just released last week. I don't know if yours would do more than this, but you can at least look at it to see how one user solved that very problem. As for posting code, for shorter scrupts, you can post in the body itself, just use the formatting bar to apply the "code" style: This is example code Other scripters put their code on github. I'm sure one of the regulars will be by momentarily to show you the ropes. They are a helpful bunch.
1537138383
The Aaron
Pro
API Scripter
Yup. What he said. :) I looked at the linked code, but it’s incomplete which makes it difficult to determine what the issue is. Using the log() function can help you track down when something doesn’t have the values you expected.&nbsp;
1537138814

Edited 1537139058
keithcurtis said: Hi banebu, Welcome to the world of API scripting. Before you bang your head too much on this problem, a very similar script was just released last week.&nbsp; Oh I guess my work was redundant. I don't mind the head bashing tho it's part of the programming charm after all. I believe all of us that are involved with code secretly love that aspect. And thanks for the kind welcome! I did search for a couple hours tho, surprised I didn't stumble upon this but I guess cause it's new it hasn't gone around much yet it does exactly everything I was trying to do just with dependencies on other API's. That's quite fascinating. :P I'll still post my code cause I want to know the issue tho. I thank you both for taking the time to respond especially you Aaron considering I messaged you personally even haha. var hitPointBarNum = 3, tempHitPointsIn = 'hp_temp', currentHPLocation = 'bar' + hitPointBarNum + '_value', maxHPLocation = 'bar' + hitPointBarNum + '_max', BloodiedMarker = 'status_half-heart', UnconsciousMarker = 'status_sleepy', DyingMarker = 'status_dead', DeadMarker = 'status_broken-skull'; on("change:token",function(obj,prev){ if (obj.get("isdrawing")){ return; } var HP = { now: parseInt(obj.get(currentHPLocation),10)||0, old: parseInt(prev[currentHPLocation],10)||0, max: parseInt(obj.get(maxHPLocation),10)||0, tmp: 0 }; if(HP.max===0){ return; } if(obj.get('represents')){ var temporaryHPObject = findObjs({_type:'attribute',_characterid:obj.get('represents'),name:tempHitPointsIn})[0]; if(temporaryHPObject){ // log("Has temp hp"); HP.tmp = parseInt(getAttrByName(obj.get('represents'),tempHitPointsIn),10)||0; } var id = temporaryHPObject.get("_id"); var name = temporaryHPObject.get("name"); var characterId = temporaryHPObject.get("_characterid") log("id: "+id+" name: "+name); } // log("HP now"+HP.now+" HP old"+HP.old+" HP max"+HP.max+" HP tmp"+HP.tmp); HP.delta=HP.old-HP.now; HP.tmpremaining = HP.tmp - HP.delta; HP.bloodied = Math.floor(HP.max/2) || 0; HP.dead = -HP.max; if(obj.get('represents')){ if(HP.delta &gt;= 0){ if(HP.delta &lt;= HP.tmp){ HP.now = HP.old; obj.set(currentHPLocation,HP.old); temporaryHPObject.set(tempHitPointsIn,HP.tmpremaining); } else{ HP.now = HP.old + HP.tmpremaining; obj.set(currentHPLocation,HP.now) temporaryHPObject.set(tempHitPointsIn,0); } } else if(HP.now &gt; HP.max){ obj.set(currentHPLocation,HP.max); } } if(HP.now &lt;= HP.dead){ HP.now = HP.dead; obj.set(DeadMarker,true); obj.set(DyingMarker,false); obj.set(BloodiedMarker,false); obj.set(UnconsciousMarker,false); } else if(HP.now &lt;= 0 &amp;&amp; !obj.get('represents')){ obj.set(DeadMarker,false); obj.set(DyingMarker,true); obj.set(BloodiedMarker,false); obj.set(UnconsciousMarker,false); } else if(HP.now &lt;= 0 &amp;&amp; obj.get('represents')){ obj.set(DeadMarker,false); obj.set(DyingMarker,false); obj.set(BloodiedMarker,false); obj.set(UnconsciousMarker,true); } else if(HP.now &lt;= HP.bloodied){ obj.set(DeadMarker,false); obj.set(DyingMarker,false); obj.set(BloodiedMarker,true); obj.set(UnconsciousMarker,false); } else{ obj.set(DeadMarker,false); obj.set(DyingMarker,false); obj.set(BloodiedMarker,false); obj.set(UnconsciousMarker,false); } }); Edit: Sorry I forgot to remove the testing logs for my own personal use from the code bear with it please :)&nbsp; Also is there any way to test scripts without having pro?
1537144363
keithcurtis
Forum Champion
Marketplace Creator
API Scripter
I don't think anyone has built a testing environment. If you have a buddy on Roll20 with Pro, they can create a game and promote you to GM status. That would allow you to develop, I believe, but you would be limited to that particular game.
Yeah that is what I've been doing currently, well I was making this script specifically for our game anyways so he doesn't mind so far. Bit of a shame that Roll20 hasn't made a platform tho.
1537153665
The Aaron
Pro
API Scripter
That’s funny, I only just saw your PM. =D So, you’re working on a modification of this script:&nbsp; <a href="https://github.com/shdwjk/Roll20API/blob/master/TempHPAndStatus/TempHPAndStatus.js" rel="nofollow">https://github.com/shdwjk/Roll20API/blob/master/TempHPAndStatus/TempHPAndStatus.js</a> I’ll have to compare the two tomorrow and see if I can spot the above problem. It’s funny, but last week when I was writing the one Keith linked to I kept thinking “this problem seems so familiar...”. When I was done, I realized I’d written something similar years earlier. =D
Yeah I did use that as a guideline to teach myself how to work with javascript but that script doesn't work so I was trying to write my own and hopefully get it working but I got stuck on the temp HP issue not being able to have the script change it in the sheet.
I've been away for a while but in the meantime I couldn't figure out the issue, I'm guessing nobody else has since no answer?