Game Development Community

Grenade not exploding

by Nathan Snell · in Torque Game Engine · 09/02/2005 (9:09 am) · 4 replies

I had a more humorous title, but I decided that I wouldn't use it as I am not entirely sure of the company that is present... with that irrelevant statement aside.

I'm having issues implementing grenades into my game.

I've looked over both:
1.) http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=7075

and

2.) http://www.garagegames.com/mg/forums/result.thread.php?qt=23892

Now, I tried implementing the #1. What occured there was the grenade would detonate before exiting the player body (Which led me to be a human bomb... simply running up to players and throwing grenades damaging myself and them...). Reading through there, I went to change the source as it specified. I came to find the source was already set that way (or I had done it previously by a stroke of random brilliance and simply forgotten- unlikely). With that, I decided to move on to #2.

Now, I went and implemented #2. This "worked" (quotes for a reason), however, I had to change the throw command to simply throw Grenade instead of MineGrenade (or GrenadeMine, whichever it was). At that point, I could throw the grenade, but I can into a new problem. It won't detonate... ever. I have no idea why. I've looked over the code, put echo's all throughout to see what's going on. But for whatever reason, Destroyed is never called.

To make things even more odd, I decided to add an onCollision so when I run into the nade with my player, it will explode. This got the Destroyed function to activate, however, it wouldn't activiate unless a single grenade had passed its detonation time (5 seconds). And when it did call the Destroy command because of the Collision, no damage was done as it should have been (That whole thing might also be irrelevant, but I felt i'd still explain in case it provided some useful detail I'm not aware of).

Suggestions? Comments? My code at the moment is basically the same as #2 (as I don't generally change things unless it's actually working). I'm at a loss here (and even more so being the torque nub I am).

Thanks in advance :)

#1
09/05/2005 (2:31 pm)
Well, with much testing and toying, and some help from Galdor, I managed to solve the issues I was having with both implementations of the grenade code (thx again Galdor). Some of the changes I made to make it work I get, and others I don't, but i'll share it anyways :)

#1 was really quite simple. In that code implementation you'll notice the %DelayToExplode is the number the schedule is using as the time before activating. Well, without thinking, I had gone and put what I wanted in seconds, so the grenade was blowing up way too fast. To fix it, i simply added %DelayToExpldoe*1000 so i could still put seconds (In case I didn't remember in the future that It had to be miliseconds, i figured i'd save myself some trouble by just having the function convert it).

#2 had a little more involved. The biggest problem in the code was the schedule for the explode wasn't even occuring. After much toying, I figured out why (that is to say, the solution, not the reason).

In the below code there was 2 things wrong with the schedule
%item = new Item() {
	      dataBlock    = %data;
	      rotation     = "0 0 1 " @ (getRandom() * 360);
	      sourceObject   = %client.player;
	      client       = %client;
	   };
	   MissionCleanup.add(%item);
	    %item.schedule(55555, "Explode"); //<------- explode in 5555 miliseconds

Those two things were, 1, %item needed to be %data (so the datablock ID was being scheduled, i assume is the reason). By doing that, the Explode function wold finally be ran... but Destroy wasn't called.

function Grenade::Explode(%this, %obj)
{
   %obj.setDamageState(Destroyed);
   %obj.schedule(999, "delete");
}

The second problem with the schedule, was it also needed to pass a second argument, that is, %item which is then passed to %obj. The first argument, %this, was already being passed with the %item.schedule part. But without %item being passed as the second variable, %obj was null, which is the reason why setDamageState(Destroyed) wasn't being correctly called.

So by changing the schedule line to: %data.schedule(55555, "Explode", %item);
The grenade would correctly explode and damage a player.

Another thing is that if you increase the mass of the object, you can increase the impulse value.

Having gotten the two pieces of grenade code working, though, I ran into a new problem. When making it so you don't have inifite grenades, for some reason by adding a line that checks the players inventory (to see if they have grenades to throw) to throwItem(), the player can pick up any grenade before it goes off. I still have yet to figure out a solution for these new issues.
#2
09/05/2005 (5:27 pm)
Set a flag on the object when it's created; ie,
%foo = new Item() { params of various sorts; isTicking=true;  };

Then in your pickup code check to see if isTicking is true, and if it is don't grab it.

Thanks for the write-up on what you've learned!
#3
09/05/2005 (5:37 pm)
I don't get why people don't use the Projectile class for grenades.... always with the Tribes 2 Item way....
#4
09/06/2005 (7:37 am)
@Ben; That's a good idea, and it worked :) Thank you.

@Josh; Maybe it's just like a lolipop. The world may never know. Perhaps i'll try that method with the other 'nade like thing I'll have to make.