Game Development Community

2 grenade questions.

by Justin Morris · in Torque Game Engine · 12/10/2004 (9:41 am) · 17 replies

Ok, im working on implementing grenades into my lil game im making, I set the script up like a weapon script but left out the weapon image data because there wont be a gun for it, it will be hand tossed.

I want it so that when you press G regardless of what weapon your currently using its spawns a nade projectile. now im not sure how to do this... do i need to write a spawn projectile function in my nade.cs and then call it from default.bind.cs when G is pressed? ive never made hand thrown grenades so im not positive...

Question 2 how can i make projectiles explode at the end of their lifetime instead of just going away. i want my grenade to last 5500 MS then explode whether its in the air or on the ground... i know i could set it to detonate on maxbounce but then thats not exactly timed and it would not blow up in the air....

thanks for any help.

#1
12/10/2004 (1:27 pm)
1) yes a key will need to be binded, personally I would "throw" an "item" rather than use a projectile.

2)schedule the item or explode after X amount of seconds
#2
12/11/2004 (7:14 am)
1) do i set the item up just like say, a health patch with explosion/dmg data then throw it and schedule it to explode after X seconds?

2)how would i call the objects explosion code?
#3
12/11/2004 (7:45 am)
datablock ItemData(Grenade)
{
   category = "Mine";
   shapeFile = "fps/data/shapes/somefile.dts";
   mass = 1;
   friction = 1;
   elasticity = 0.3;//use for bounce
   repairAmount = 50;
   maxDamage = 0.2;
   directDamage = 125;
   damageRadius = 20;
   radiusDamage = 150;
   dynamicType = $TypeMasks::DamagableItemObjectType;
       explosion = homingExplosion;
         fadeIn = 0;
};
function Grenade::Explode( %data, %obj, %col )
{

   %obj.setDamageState(Destroyed);
   %obj.schedule(999, "delete");
}

function Grenade::onDestroyed(%data,%obj)
{
   radiusDamage(%obj,%pos,%data.damageRadius,%data.radiusDamage,"Mine",0);
}
function ShapeBase::throwItem(%this,%data,%client){
   // Construct item to throw from the given datablock
   // (with a random rotation around the z axis)
   %item = new Item() {
      dataBlock    = %data;
      rotation     = "0 0 1 " @ (getRandom() * 360);
      sourceObject   = %client.player;
      client       = %client;
   };
   MissionCleanup.add(%item);
    %item.schedule(5555, "explode"); //<------- explode in 5555 miliseconds
   // Call the workhorse throw method...
   %this.throwObject(%item);
}
function ShapeBase::throwObject(%this,%obj){
   // Throw the given object in the direction the shape is
   // looking.  The force values are hardcoded...
   %eye = %this.getEyeVector();
   %vec = vectorScale(%eye, 20);
   // Add a vertical component to give the item a better arc
   
   %dot = vectorDot("0 0 1",%eye);
      if (%dot < 0)
            %dot = -%dot;
      %vec = vectorAdd(%vec,vectorScale("0 0 7",1 - %dot));
      // Add the shape's velocity
      %vec = vectorAdd(%vec,%this.getVelocity());
      // Set the object's position and initial velocity
      %pos = getBoxCenter(%this.getWorldBox());
      %obj.setTransform(%pos);
      %obj.applyImpulse(%pos,%vec);
      // Since the object is thrown from the center of the
      // shape, the object needs to avoid colliding with it&#180;s
      // thrower.
      %obj.setCollisionTimeout(%this);
}


function serverCmdthrowGrenade(%client){
  %client.player.throwItem(MineGrenade,%client);
}
moveMap.bind(keyboard, "g", throwGrenade);

I am just too nice today
#4
12/11/2004 (9:32 pm)
Wow thanks a ton :)
#5
01/19/2005 (2:49 am)
MY ERROR
throwGrenade: Unknown command.
throwGrenade: Unknown command.
throwGrenade: Unknown command.
throwGrenade: Unknown command.
throwGrenade: Unknown command.
throwGrenade: Unknown command.
PLZ HELP ME
#6
01/19/2005 (11:46 am)
Opps replace last line with this one
moveMap.bindCmd(keyboard, "g", "commandToServer(\'throwGrenade\');", "");

it was a binded command
#7
01/19/2005 (5:10 pm)
Way to earn that Associates tag, Anthony. :)
#8
01/23/2005 (7:03 am)
Loading compiled script Dodgeball/server/scripts/grenade.cs.
Object 'homingExplosion' is not a member of the 'ExplosionData' data block class


I get this message when I put in the code abouve in the FPS demo
server/scripts as grenade.cs

In game.cs I use exec for this grenade.cs file to activate it.


Whats wrong?


cheers

Lee
#9
01/24/2005 (6:47 am)
You have to do an explosion datablock named homingExplosion... search explosion in GG for examples
#10
01/24/2005 (6:51 am)
HomingExplosion was a Explosion datablock I made for my game. you'll have to make one.
#11
01/25/2005 (1:01 am)
Replace this
explosion = homingExplosion;
at this
explosion = CrossbowExplosion;
or create Explosion datablock named HomingExplosion
#12
01/26/2005 (3:32 am)
Excuse the newbie question :) I have 2 problems.

1. I cannot get the bind to work.

default.bind.cs
moveMap.bindCmd(keyboard, "g", "commandToServer(\'throwGrenade\');", "");
It always says 'throwGrenade:command unknown' but if i enter 'commandToServer('throwGrenade');' in the console it executes!

2. I think this is a model issue but an pointers would be appreciated :)

Error at scenegraph.cc @ 927
Error: No zones found, should find root at least!

any ideas where to start (bear in mind I'm a complete noob :oD )

EDIT: More info....

I have created a simple grenade object in GS. I have set up the code above and use this dts as the grenade. When it gets created I get that error message. I am using stock 1.3, and the stronghold mission.
#13
02/11/2005 (3:06 pm)
Thanks for the info, Anthony.

As I was reading through this, though, I remembered an issue that I've been wondering about for a while...

In ShapeBase::throwItem, a new datablock is created and added to the mission cleanup:

function ShapeBase::throwItem(%this,%data,%client){
   // Construct item to throw from the given datablock
   // (with a random rotation around the z axis)
   [b]%item = new Item() {
      dataBlock    = %data;
      rotation     = "0 0 1 " @ (getRandom() * 360);
      sourceObject   = %client.player;
      client       = %client;
   };
   MissionCleanup.add(%item);
   [/b] %item.schedule(5555, "explode"); //<------- explode in 5555 miliseconds
   // Call the workhorse throw method...
   %this.throwObject(%item);
}

My concern is this: There is the possibility of hundreds if not thousands of grenades being thrown during a mission, with each creating a new Item object. There could possibly be other objects being continually created dynamically as well, depending on the game. If these new Item objects are not being cleaned up until the end of the mission, are there potential issues with limited memory resources and possible heap fragmentation? (I wish I knew a little more about TGE's memory management...)

Anyone have any thoughts or clarification on this?
#14
02/24/2005 (3:31 pm)
@kevin,
notice that in explode a delete is scheduled
%obj.schedule(999, "delete");

so the object will delete itself when it explodes, and when it is deleted it automatically removes itself from the missioncleanup group

so when you throw it, it will explode in 5 second, then 1 second later it will delete itself.

any particular reason for the interesting numbers 999 5555 :)
-c
#15
02/25/2005 (10:10 am)
@Anthony
Quote:1) yes a key will need to be binded, personally I would "throw" an "item" rather than use a projectile.

whats the reason behind that? taste or something else?

I just used a projectile for a similar thing and it seemed to work fine, although the bouncy physics are a little wanting, but it works.
#16
02/25/2005 (10:31 am)
I'd be interested in seeing a projectile based version, care to share yor code Clint?
#17
03/10/2005 (8:58 am)
FruitBat, just modify the throw function to toss a projectile datablock instead of an item datablock. The projectile should handle itself from there.

function ShapeBase::throwItem(%this,%data,%client){
   // Construct item to throw from the given datablock
   // (with a random rotation around the z axis)
   %projectile = new projectile() {
      dataBlock    = %data;
      rotation     = "0 0 1 " @ (getRandom() * 360);
      sourceObject   = %client.player;
      client       = %client;
   };
   MissionCleanup.add(%projectile);

   // Call the workhorse throw method...
   %this.throwObject(%projectile);
}