Game Development Community

Bad Guy Machine Gun

by Jon Jorajuria · in Torque Game Builder · 12/12/2006 (12:56 pm) · 2 replies

I am trying to schedule out five round machine gun burst from a character. On the first pass the schedule works fine, after that things go crazy and the machine gun floods the game. Can anybody tell me what I am doing wrong here?

function myBadGuyClass::fireBullet(%this)
{
   if(!%this.isDead)
   { 
    %this.isFiring = true;
    %this.bulletFiring = %this.schedule(100, fireBullet);
    %this.schedule(500, cancelFirebullet);
    %this.enemyBadGuyBullet = new t2dStaticSprite()
    {
       scenegraph = %this.scenegraph;
       class = enemyBadGuyBullet;
       bulletSpeed = %this.bulletSpeed;
       enemy = %this;
    }
    %this.enemyBadGuyBullet.fireBadGuyBullet02();
  }
}

function myBadGuyClass::cancelFireBullet(%this)
{
   cancel(%this.bulletFiring);
   %this.isFiring = false;
   %this.schedule(1000, fireBullet);
}

#1
12/12/2006 (2:05 pm)
I think the problem is that you schedule a whole bunch of cancelFireBullet's every time you shoot...which will all cause another bullet to be fired in a second. I've found that using isEventPending can help a ton with schedules. It will tell you if a schedule (by ID) is still upcoming, so you can cancel them if you want. I would change the code like so:

function myBadGuyClass::fireBullet(%this)
{
   if(!%this.isDead)
   { 
    %this.isFiring = true;
    %this.bulletFiring = %this.schedule(100, fireBullet);
    [b]if( !isEventPending( %this.cancelFireSchedule )
       %this.cancelFireSchedule = %this.schedule(500, cancelFirebullet);[/b]
   
    %this.enemyBadGuyBullet = new t2dStaticSprite()
    {
       scenegraph = %this.scenegraph;
       class = enemyBadGuyBullet;
       bulletSpeed = %this.bulletSpeed;
       enemy = %this;
    }
    %this.enemyBadGuyBullet.fireBadGuyBullet02();
  }
}

function myBadGuyClass::cancelFireBullet(%this)
{
   cancel(%this.bulletFiring);
   %this.isFiring = false;
   %this.schedule(1000, fireBullet);
}

This way, you'll only have one cancel event at a time, so they won't be all calling their own fire events. Also, since you should only have one fire schedule running at any moment, you could also ID that one (just assigning the return value (the ID) from a schedule to a variable and then using that to make sure you don't make another). That should make the code more robust.
Hope that helps fix it ;)
#2
12/12/2006 (5:00 pm)
Thanks Tom I will try that.