Game Development Community

Problem with powerup to increase Rate of Fire.

by a.johnson465 · in Torque Game Engine · 11/19/2009 (7:32 pm) · 7 replies

In our game, we have one of our pickups increase the rate of fire of the gun for a short time. I have it set up to switch to a different weapon image datablock that has the increased weapon rate of fire. And then after 5 seconds, it switches back to the original dateblock.

The problem I have run into is that if the datablock switches while the gun is firing, when I fire the gun again, it won't fire anything at all. Anyone know of a way to get this to work without it glitching like this?

Here is the code I have for the onCollision of the pickup and the time to have it switch back.

function triggerhappyitem::onCollision( %thisDB, %thisObj, %otherObj )
{
    if(%otherObj == $player)
    {
         %otherObj.mountImage(RailgunIMAGE2, 0);
         %otherObj.setImageAmmo(0, true);
         //%otherObj.GunAmmoAmount += 20;
         echo("Db 2");
         thTimer();
         %thisobj.delete();
    }
}

function thTimer()
{
   $thTime = 0;
   thTime();   
}

function thTime()
{
   $thTime++;
   echo($thTime);
   $thschedule = schedule(1000,0, thTime);  
   if($thTime >= 5)
      {
           $player.mountImage(RailgunIMAGE, 0);
           $player.setImageAmmo(0, true);
           $player.GunAmmoAmount += 20; 
           cancel($thschedule);
      }
}

#1
11/30/2009 (6:42 pm)
Can anyone help?

I've figured out that the problem lies in the fact that if the the fire button is down making the weapon be in onFire, it won't switch properly. I have noticed that if I am firing when I get the pickup, it won't fire when it is supposed to be in the second datablock, and then will begin firing again when it enters the old datablock.

Does anyone know of any way I can try and get this to work? Does it require making the game think that the fire button isn't pressed and then letting the datablock switch? If that is the case, how I do I get that working?

Thanks.
#2
11/30/2009 (7:04 pm)
You are right, if you are firing, you cannot change it like that without problems. What's happening is the onFire is active and being actively used, i can't switch what car i'm driving while i'm driving, gotta take a time out.

What you can do is, make a function that switches the fire rate, and have that function pause the shooting, set the weapon state from firing to ready. What that should do is cause a hiccup in the firing when it is changed, which is the same thing as waiting to shoot until after you pick up the powerup.

You could also try having the fire rate be a variable that you set dynamically, I'm not sure if that would yeild the same problem that you were already having though. Maybe someone more knowledgeable than myself will chime in for you.
#3
11/30/2009 (8:54 pm)
One solution would be to instead of using two weapon images you could add a variable to your weapon that toggles when the powerup is picked up.

Here is how i would handle this:
1. I would add this resource into the engine
www.garagegames.com/community/resources/view/9141

2. I would change the weapon states to check your fire variable to decide if we should fire at regular rate or increased rate

// Instead of going directly to fire, check and see if we should fire with increased rate
   stateName[2]                     = "Ready";
   stateTransitionOnNoAmmo[2]       = "NoAmmo";
   stateSequence[2]                 = "idle";
   stateTransitionOnTriggerDown[2]  = "CheckFire";

   // - Add your check fire state in here, and make sure you set the state numbers right
   stateName[3]                     = "CheckFire";
   stateScript[3]                   = "onCheckFire";


// ==================================================================

// Now add the function into the WeaponImage class so it works for all weapons
function WeaponImage::onCheckFire(%this, %obj)
{
   // See if we should increase the fire
   if(%obj.incFire)
   {
      %obj.setManualImageState($WeaponSlot, HighFire);
   }
   else //or just fire regular
   {
      %obj.setManualImageState($WeaponSlot, fire);
   }
}
Next post comming soon
#4
11/30/2009 (9:00 pm)
Post #2

Now i would change the powerup datablock

// This way you wont need to have duplicate images
function triggerhappyitem::onCollision( %thisDB, %thisObj, %otherObj )
{
    if(%otherObj == $player)
    {
         %otherObj.incFire = true; //Set our dynamic variable to true
         echo("Fire Faster!!!!");
         thTimer();
         %thisobj.delete();
    }
}

function thTimer()
{
   $thTime = 0;
   thTime();   
}

function thTime()
{
   $thTime++;
   echo($thTime);
   $thschedule = schedule(1000,0, thTime);  
   if($thTime >= 5)
   {
      $player.incFire = false; // Stop us from firing faster
      cancel($thschedule);
   }
}
#5
11/30/2009 (9:07 pm)
Thanks for the help, but I would like to see if I can do this without messing with the engine code. This is a group project and I am only one of the people working on code. so I'd rather not do something to the engine and then make the others programmers have to make the same changes.

#6
11/30/2009 (9:22 pm)
Quote:so I'd rather not do something to the engine and then make the others programmers have to make the same changes.
Just send them the EXE ;)

Anyway - I think there's a parameter in the state machine such as allowImageChange that prevents the image from being switched when in that state. See if setting that to false in onFire will affect anything.
#7
11/30/2009 (9:32 pm)
Thanks, Daniel. That did exactly what we were looking for. I didn't even see that in the state machine.

Thanks again everyone.