Drop-in Grenades.
by FruitBatInShades · 01/30/2005 (1:08 am) · 43 comments
This little resource started life out as this thread but did not work for me. With the help of Craig Ball, Anthony Rosenbaum(who also wrote the original code) and David Barr I got the basic stuff working. This is my first attempt at torquescript so bear with me but it works well apart from the two issues mentioned at the end.
Save this as Grenades.cs in your server scripts directory:-
Known issues
There are a few issues, if you can solve them please let me know so I can update this resource.
1. Grenades just sometimes disappear. Usually the first one thrown and then randomly after that. Still trying to figuire out why.
2.
Save this as Grenades.cs in your server scripts directory:-
//-----------------------------------------------------------
// Grenades.cs - Class to add grenades to torque via script
//-----------------------------------------------------------
// This script file is an easy way to add grenades to your
// FPS. Just exec this script from game.cs and bind a key
// to call the throwGrenadeDB function
//-----------------------------------------------------------
datablock ItemData(Grenade)
{
category = "Grenades";
shapeFile = "starter.fps/data/shapes/items/healthkit.dts";
mass = 0.7;
friction = 1;
elasticity = 0.3; //how much bounce 0.1 = none, 1 = funny
repairAmount = 50;
maxDamage = 0.2;
directDamage = 1;
damageRadius = 20;
radiusDamage = 1;
areaImpulse = 1000;
dynamicType = $TypeMasks::DamagableItemObjectType;
explosion = CrossbowExplosion;
fadeIn = 0;
};
function Grenade::Explode(%dataBlock, %obj)
{
echo("Grenade::Explode called");
%pos = %obj.getPosition();
echo("Grenade::Explode Doing Damaged");
radiusDamage(%obj,%pos,%dataBlock.damageRadius,%dataBlock.radiusDamage,"Grenade",%dataBlock.areaImpulse);
%obj.setDamageState(Destroyed);
%obj.schedule(99, "delete");
}
function Grenade::Damage(%this,%obj,%sourceObject, %position, %damage, %damageType)
{
echo("Grenade::Damage called");
//Grenade has recieved damage so detonate it
cancel(%obj.ExplodeEventID);//Cancel previous explode timer
%obj.setDamageState(Destroyed);
%obj.schedule(99,"delete");
}
function Grenade::onDestroyed(%data,%obj)
{
echo("Grenade::onDestroyed called");
}
function ShapeBase::throwGrenadeDB(%this,%GrenadeTypeDB,%client,%DelayToExplode)
{
//%GrenadeTypeDB = the datablock type for this grenade
//%client = client object
//%time = time in milliseconds before explode
//Check time is not silly or negative
if (%DelayToExplode < 1)
%DelayToExplode = 500;
echo("ShapeBase::throwGrenade grenade, creating item");
%item = new Item()
{
dataBlock = %GrenadeTypeDB;
rotation = "1 0 0 " @ (getRandom() * 360);
sourceObject= %client.player;
client = %client;
ExplodeEventID=0;
};
//call the more general throwObject
%this.throwObject(%item);
//set detonantion delay
%item.ExplodeEventID = %GrenadeTypeDB.schedule(%DelayToExplode, "Explode",%item);
//make sure we tidy up :)
MissionCleanup.add(%item);
}
function ShapeBase::throwObject(%this,%obj){
// Throw the given object in the direction the shape is
// looking. The force values are hardcoded...
echo("ShapeBase::throwObject grenade, getting direction");
%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);
echo("ShapeBase::throwObject grenade, Applying impulse");
// Since the object is thrown from the center of the
// shape, the object needs to avoid colliding with it's
// thrower.
%obj.setCollisionTimeout(%this);
%obj.applyImpulse(%pos,%vec);
}If you know what your doing, put the binding stuff anywhere you like :)Known issues
There are a few issues, if you can solve them please let me know so I can update this resource.
1. Grenades just sometimes disappear. Usually the first one thrown and then randomly after that. Still trying to figuire out why.
2.
About the author
#22
I am using RW 1101 and i have the problem that one time the grenades explode another time not (both thrown at the same place).
Now i have infinite grenades, but perhaps it will be a little better, if i have to pick up first the grenades and have a limit of grenades.
How can i make this?
02/08/2005 (9:50 am)
Congratulation, nice script. :)I am using RW 1101 and i have the problem that one time the grenades explode another time not (both thrown at the same place).
Now i have infinite grenades, but perhaps it will be a little better, if i have to pick up first the grenades and have a limit of grenades.
How can i make this?
#23
02/08/2005 (11:37 am)
There is a problem with the grenades sometime just dissapearing. I am unsure why at the moment (something to do with collision) but am looking into it. You need to add the grenade to your inventory system. I'll upload that as soon as I get it working ok.
#24
i.e. I throw the object and count to 2 and then it pops up where (I think) it should have landed from the scripting.
anyone else having this problem?
03/07/2005 (8:37 pm)
I'm having problems with the grenaids not being drawn flying to their destination. they just magically appear there some seconds later.i.e. I throw the object and count to 2 and then it pops up where (I think) it should have landed from the scripting.
anyone else having this problem?
#25
Good luck
03/07/2005 (8:43 pm)
I had that same problem but was able to fix it once I recompiled the engine with the changed item.cc . If that doesn't work follow the link above in Donald's post to his grenade.cs to make sure you don't have a small script error.Good luck
#26
03/08/2005 (8:36 pm)
How do explosions and damage work with this? Any ideas?
#27
The only change I saw mentioned was to take the trigger condition out of the collision mask right? did I miss something else?
anyway I'm having two major problems with this and I don't know if they are related or not.
1: Like I said in the previous post, the objects just pop where they would have landed after a couple of seconds. no drawing of inbetween conditions. (this happens whether I add a real impulse or just a fake one where I'm really just dropping it) so I drop the object, back up a bit, and then see it appear where i was standing a second ago. This happens with the original grenade script. The one I tried to create from using that as a template. Donalds script. And the script I wrote for dropping weapons to pick up different ones. I don't really understand the impulse thing, so it could easily be a problem there.
2: With the grenades and explosions. They cause a crash 40-60% of the time. When I ran the debugger on it, it told me that there where NULL ghosts. I'm not really sure what a ghost is in the context of this engine (I can think of it as either something that just needs to be cleaned up, or as something that is actively doing something that needs to be processed on.) (both of which are probably wrong). This could be similar to the problem Jeff Jones was having, but I'm not running out of memory, looks like its just pointing to bad memmory or something.
Any thoughts?
03/09/2005 (9:33 am)
Thanks for the reply! but unfortunatly still no luck. :(The only change I saw mentioned was to take the trigger condition out of the collision mask right? did I miss something else?
anyway I'm having two major problems with this and I don't know if they are related or not.
1: Like I said in the previous post, the objects just pop where they would have landed after a couple of seconds. no drawing of inbetween conditions. (this happens whether I add a real impulse or just a fake one where I'm really just dropping it) so I drop the object, back up a bit, and then see it appear where i was standing a second ago. This happens with the original grenade script. The one I tried to create from using that as a template. Donalds script. And the script I wrote for dropping weapons to pick up different ones. I don't really understand the impulse thing, so it could easily be a problem there.
2: With the grenades and explosions. They cause a crash 40-60% of the time. When I ran the debugger on it, it told me that there where NULL ghosts. I'm not really sure what a ghost is in the context of this engine (I can think of it as either something that just needs to be cleaned up, or as something that is actively doing something that needs to be processed on.) (both of which are probably wrong). This could be similar to the problem Jeff Jones was having, but I'm not running out of memory, looks like its just pointing to bad memmory or something.
Any thoughts?
#28
03/17/2005 (9:31 pm)
I got the script to working by starting with a clean version of the Torque engine. The problem may have been on my end.
#29
Problem: when i throw a 'nade, it gets a connection error, Invalid Packet GameBase::UnpackUpdate(). Is this the same problem as the freeze described earlier? This is all the info i have on the problem, it's all there was in the console.log.
04/16/2005 (8:01 pm)
Hey guys,Problem: when i throw a 'nade, it gets a connection error, Invalid Packet GameBase::UnpackUpdate(). Is this the same problem as the freeze described earlier? This is all the info i have on the problem, it's all there was in the console.log.
#30
05/16/2005 (8:23 pm)
this is a great idea and i hope it keeps on growing but as for now it dosnt seem stable nuff for me to keep it in the game without doing some work on it... ill look into it more when i get a chance
#31
delete all instances of
%obj.schedule(99,"delete");
and then add it into the onDestroyed() call with a longer time, (I did 300).
The problem was you were deleting the grenade before the explosion was actually called... I think.
It seems like its working so far.
07/28/2005 (5:17 am)
I found out how to stop the grenades from randomly dissapearing. (I'm using Donald's datablock changes)delete all instances of
%obj.schedule(99,"delete");
and then add it into the onDestroyed() call with a longer time, (I did 300).
The problem was you were deleting the grenade before the explosion was actually called... I think.
It seems like its working so far.
#32
@All
Wondering if anyone knows how to hook this to a inventory. Endless grenades are great, but a little bit unrealistic for the game I'm working on. I'll keep messing with it, but not even sure where to begin.
I'm guessing that I ought to create it as a standard weapon, using the throw commands instead
of the onFire, create an ammo inventory amount, then decrease ammo with each throw.
Any suggestions would be appreciated!
08/12/2005 (8:00 pm)
@FruitBatInShades Works great, thanks for sharing. @All
Wondering if anyone knows how to hook this to a inventory. Endless grenades are great, but a little bit unrealistic for the game I'm working on. I'll keep messing with it, but not even sure where to begin.
I'm guessing that I ought to create it as a standard weapon, using the throw commands instead
of the onFire, create an ammo inventory amount, then decrease ammo with each throw.
Any suggestions would be appreciated!
#33
08/31/2005 (11:54 am)
@Alan: To add it to the inventory, you should be able to do it along the same lines as how ammo for weapons are reduced. Simply set a maxInv for Grenades, then when you throw the grenade, do a %this.decInventory(Grenade,1);
#34
09/18/2005 (11:31 pm)
getting a error remote command error command must be a tag ang help wpild be nice
#35
HTH.
Actually I'm only here looking at a completely different issue - I'm having the same problem with thrown objects as Leo has above - Invalid Packet GameBase::UnpackUpdate().
10/13/2005 (6:43 am)
@Dylan - Possibly you have eg commandToClient(%player.client,"nameOfFunction", etc) - if so make sure you use single quotes around the function name; the remote command must be a tagged string. eg: commandToClient(%player.client,'nameOfFunction', etc)HTH.
Actually I'm only here looking at a completely different issue - I'm having the same problem with thrown objects as Leo has above - Invalid Packet GameBase::UnpackUpdate().
#36
10/15/2005 (5:05 am)
how do i change this to fire at mouse 1 It just wont wont???
#37
10/20/2005 (12:36 am)
This is a really nood question but how do I bind a key for the grenade script?
#38
No grenade, just errors such as
Any ideas?
10/24/2005 (12:57 pm)
I've added the .cs file (from Donald), made the changes stated and the change thats stated in the link, but I can't get it working... No grenade, just errors such as
Quote:
ShapeBase::throwObject grenade, getting direction
starter.fps/server/scripts/grenades.cs (141): Unable to find object: '0' attempting to call function 'setTransform'
ShapeBase::throwObject grenade, Applying impulse
starter.fps/server/scripts/grenades.cs (147): Unable to find object: '0' attempting to call function 'setCollisionTimeout'
starter.fps/server/scripts/grenades.cs (148): Unable to find object: '0' attempting to call function 'applyImpulse'
starter.fps/server/scripts/grenades.cs (115): Unable to find object: 'Grenade' attempting to call function 'schedule'
Any ideas?
#39
Thanks
11/30/2005 (5:13 pm)
How can I bind this to a key? To actually make it work? And it wont really execute when I put it in game.cs. Any help?Thanks
#40
Big up!
02/20/2006 (10:06 am)
This is a very cool resource. I really appriciate the way people take time to share their hard work and creativity in these forums.Big up!

Torque Owner Jeff Jones
You guys just skipped over this comment. Am I alone with this one?