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

Need some help with Trap+Damage Script

January 14 (9 years ago)

Edited January 20 (9 years ago)
Long time player, first time posting a question on Roll20.
First let me thank Cazra and The Aaron from whom I adapted much code.

The ultimate goal of the script is to have a character's movement trigger a trap (thanks to Cazra's script "It's A Trap!"), then have the character save vs. the trap if applicable (as yet not included), then have the trap inflict damage / effects to the character (thanks to some modified code from The Aaron's scripts "MonsterHitDice").

An issue arises when a character interacts with a trap (or traps) more than once. On first interaction with a trap, the script works fine:

First trap: Acid Cloud

However, the second and successive times that a character interacts with any trap, this happens:

Second Trap: Rat Trap;  Third trap: Acid Cloud again.

Rather than each trap inflicting its own damage, each trigger event inflicts the damage of each trap that has been triggered previously. There appears to be an array that needs to be cleared. I thought "val.length = 0" would do the trick, I was wrong.

Any ideas?

Here is a link to my script (hope I did it right): https://github.com/vorgrist/roll20/tree/master/trap

Suspect code here:

if(trap) {
   var trapName = trap.get("name");
   var trapDam = trap.get("bar1_value");
   sendChat("msg.who", obj.get("name") + " set off a trap!");
   // Inflict Trap damage
   on("chat:message", function(msg) {
      if(msg.content.indexOf("trap") !== -1) {
         sendChat(msg.who,"/r " + trapDam,function(r){
            var hp=obj.get("bar1_value") || 0;
            _.each(r,function(subr){
               var val=JSON.parse(subr.content);
               if(_.has(val,'total')) {
                  hp-=val.total;
                  sendChat("msg.who", trapName + " inflicts " + val.total + " HitPoints of damage!");
                  val.length = 0
               }
            });
            obj.set({
               bar1_value: hp
            })
         });
      }
   });


Any help would be appreciated.

Vorgrist
January 14 (9 years ago)
The Aaron
Roll20 Production Team
API Scripter
(Moved to API)
January 19 (9 years ago)
Tap... tap... tap... is this thing on?
January 19 (9 years ago)
The Aaron
Pro
API Scripter
Thanks for the reminder, I meant to take a look at this!
January 19 (9 years ago)
The Aaron
Pro
API Scripter
Ok, gave it a try.  I'm not getting the duplication, so it may be something more specific about your environment.  If you want to PM me an invite and GM me, I can jump in and try to track it down there.
January 19 (9 years ago)
Just fyi, not a good idea to post join links on the forums. Anyone can use it to join your campaign.
January 19 (9 years ago)
Thanks HoneyBadger!
January 19 (9 years ago)
The Aaron
Pro
API Scripter
I'm joined now, so GM away!  =D
January 19 (9 years ago)

Edited January 19 (9 years ago)
Wow. how did I miss you? I didn't hear or see you enter. Blast. Sorry.
January 20 (9 years ago)

Edited January 20 (9 years ago)
Ada L.
Marketplace Creator
Sheet Author
API Scripter
Here's what's wrong:
You are registering your event handler to dish out the damage EVERY time you land in a trap. So, the first time, the handler goes off only once. Next time, your handler has been registered twice, so it goes off twice. The third time it is registered a third time, so it goes off three times, and so on.

A solution:
Don't process the damage in an event callback. Instead, process it in the callback function to the original sendChat.


if(trap) {
  var trapName = trap.get("name");
  var trapDam = trap.get("bar1_value");
  sendChat("msg.who", obj.get("name") + " set off a trap!", function() {

    // Inflict Trap damage
    sendChat(msg.who,"/r " + trapDam,function(r) {
      var hp=obj.get("bar1_value") || 0;
      _.each(r,function(subr){
        var val=JSON.parse(subr.content);
        if(_.has(val,'total')) {
          hp-=val.total;
          sendChat("msg.who", trapName + " inflicts " + val.total + " HitPoints of damage!");
          val.length = 0
        }
      });
      obj.set({
        bar1_value: hp
      });
    });
  });
}

January 20 (9 years ago)

Edited January 20 (9 years ago)
Stephen, Thanks for your response. Both you and The Aaron indicated this solution, I just hadn't a chance to post what The Aaron (he really is an arcane scriptomancer) explained and helped me solve.

+1 for The Aaron
+1 for Stephen L.

In The Aaron's solution, we just took the the sendChat observation out of the equation; it was redundant.

if(trap) {
var trapName = trap.get("name");
var trapDam = trap.get("bar1_value");
sendChat('', obj.get("name") + " set off a trap!");
// Inflict Trap damage
sendChat('',"/r " + trapDam,function(r){
var hp=obj.get("bar1_value") || 0;
_.each(r,function(subr){
var val=JSON.parse(subr.content);
if(_.has(val,'total'))
{
hp-=val.total;
sendChat('', trapName + " inflicts " + val.total + " HitPoints of damage!");
}
});
obj.set({
bar1_value: hp
})
}); moveTokenToTrap(obj, trap);
if(trap.get("status_bleeding-eye")) {
trap.set("layer","objects");
toBack(trap);
}
}