Non-Exploding grenades!
by FruitBatInShades · in Torque Game Engine · 01/27/2005 (2:33 am) · 60 replies
My grenade don't explode, they just sit there! Any idea as to what i've missed :)
datablock ItemData(Grenade)
{
category = "Mine";
shapeFile = "starter.fps/data/shapes/grenade.dts";
mass = 1;
friction = 1;
elasticity = 0.3; //how much bounce 0.1 = none, 1 = funny
repairAmount = 50;
maxDamage = 0.2;
directDamage = 125;
damageRadius = 20;
radiusDamage = 150;
dynamicType = $TypeMasks::DamagableItemObjectType;
explosion = CrossbowExplosion;
fadeIn = 0;
};
function Grenade::Explode( %data, %obj, %col )
{
echo("Grenade::Explode called");
%obj.setDamageState(Destroyed);
%obj.schedule(999, "delete");
}
function Grenade::onDestroyed(%data,%obj)
{
echo("Grenade::onDestroyed called");
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)
echo("ShapeBase::throwItem grenade, creating item");
%item = new Item() {
dataBlock = %data;
rotation = "0 0 1 " @ (getRandom() * 360);
sourceObject = %client.player;
client = %client;
};
echo("ShapeBase::throwObject grenade, setting explode time to 100ms");
%item.schedule(3000, "explode"); //<------- explode in 5555 miliseconds
// Call the workhorse throw method...
echo("ShapeBase::throwItem grenade, calling throwObject");
%this.throwObject(%item);
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);
%obj.applyImpulse(%pos,%vec);
// Since the object is thrown from the center of the
// shape, the object needs to avoid colliding with it´s
// thrower.
%obj.setCollisionTimeout(%this);
}
#22
01/27/2005 (12:46 pm)
@anthony: latest version...datablock ItemData(Grenade)
{
category = "Mine";
shapeFile = "starter.fps/data/shapes/grenade.dts";
mass = 1;
friction = 1;
elasticity = 0.3; //how much bounce 0.1 = none, 1 = funny
repairAmount = 50;
maxDamage = 0.2;
directDamage = 125;
damageRadius = 20;
radiusDamage = 150;
dynamicType = $TypeMasks::DamagableItemObjectType;
explosion = CrossbowExplosion;
fadeIn = 0;
};
function Grenade::Explode(%dataBlock, %obj)
{
echo("Grenade::Explode called");
%pos = %obj.getPosition;
%obj.dump();
radiusDamage(%obj,%pos,%dataBlock.damageRadius,%dataBlock.radiusDamage,"Grenade",0);
%obj.setDamageState(Destroyed);
%obj.schedule(999, "delete");
}
function Grenade::onDestroyed(%data,%obj)
{
echo("Grenade::onDestroyed called");
}
function ShapeBase::throwItem(%this,%dataBlock,%client){
// Construct item to throw from the given datablock
// (with a random rotation around the z axis)
echo("ShapeBase::throwItem grenade, creating item");
%item = new Item() {
dataBlock = %dataBlock;
rotation = "0 0 1 " @ (getRandom() * 360);
sourceObject = %client.player;
client = %client;
};
echo("ShapeBase::throwObject grenade, setting explode time to 100ms");
%item.schedule(3000, "Explode"); //<------- explode in 5555 miliseconds
// Call the workhorse throw method...
echo("ShapeBase::throwItem grenade, calling throwObject");
%this.throwObject(%item);
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");
%obj.applyImpulse(%pos,%vec);
// Since the object is thrown from the center of the
// shape, the object needs to avoid colliding with it´s
// thrower.
%obj.setCollisionTimeout(%this);
echo("ShapeBase::throwObject grenade, Attempting to set schedule again");
%item.schedule(3000, "Explode");
}
#23
@ Fruitbat - I think you have a %item in your last function that should be a %obj??
01/27/2005 (12:46 pm)
I have tried (in many combinations including all the above) and cannot get schedule to call explode either.@ Fruitbat - I think you have a %item in your last function that should be a %obj??
#24
01/27/2005 (12:52 pm)
True, just copied it in the vain hope, lol, but it makes no difference anyway! Thanks for trying David :) I've looked through the docs,
#25
It looks like you throw the item before you add it to the world.
A hint is to first add the item then throw it after that set the explode schedule.
and the radiusdamage should be called before the damagestate(Destroyed).
I have a working Dynamite somewhere so i grabbing this from the air right now.
01/27/2005 (12:58 pm)
A fast look at you script Fruit !It looks like you throw the item before you add it to the world.
A hint is to first add the item then throw it after that set the explode schedule.
and the radiusdamage should be called before the damagestate(Destroyed).
I have a working Dynamite somewhere so i grabbing this from the air right now.
#26
01/27/2005 (1:00 pm)
NEWB ALERT! How do I add it to the world? The grenade appears and bounces along, it just doesn't explode
#27
%item.getDatablock().schedule(100, "Explode");
function Grenade::Explode(%this ){ echo("Grenade::Explode called");
}
Explode is a Genade ( ie a datablock ) method, you were calling the schedule on the obj not it's datablock.
01/27/2005 (1:03 pm)
Ahh found i t%item.getDatablock().schedule(100, "Explode");
function Grenade::Explode(%this ){ echo("Grenade::Explode called");
}
Explode is a Genade ( ie a datablock ) method, you were calling the schedule on the obj not it's datablock.
#28
Shouldn't
%item.schedule(3000, "Explode");
be
%this.schedule(3000, "Explode");
01/27/2005 (1:07 pm)
Getting ready to go to work so I just glanced at your code.Shouldn't
%item.schedule(3000, "Explode");
be
%this.schedule(3000, "Explode");
#29
01/27/2005 (1:08 pm)
@Anthony:Did you actually get it working?
#30
01/27/2005 (1:14 pm)
I've tried both the suggestions and still no bang!
#31
like this
%item.getDatablock().schedule(100, "Explode", %item);
....
function Grenade::Explode( %this , %obj){
}
01/27/2005 (1:14 pm)
Correction. . .be sure to pass the obj along otherwise you will not have it in the on explodelike this
%item.getDatablock().schedule(100, "Explode", %item);
....
function Grenade::Explode( %this , %obj){
}
#32
01/27/2005 (1:15 pm)
YES it works in Lore
#33
I though that a %DataBlock was a class type not an object. When the %dataBlock is passed to these functions is that the instantiated object from that dataclass?
01/27/2005 (1:18 pm)
I tried this and it works:-function ShapeBase::throwItem(%this,%dataBlock,%client){
// Construct item to throw from the given datablock
// (with a random rotation around the z axis)
echo("ShapeBase::throwItem grenade, creating item");
%item = new Item() {
dataBlock = %dataBlock;
rotation = "0 0 1 " @ (getRandom() * 360);
sourceObject = %client.player;
client = %client;
};
// Call the workhorse throw method...
echo("ShapeBase::throwItem grenade, calling throwObject");
%this.throwObject(%item);
echo("ShapeBase::throwObject grenade, setting explode time to 100ms");
[b]%dataBlock.schedule(300, "Explode",%item);[/b]
MissionCleanup.add(%item);
}Before I run back to my tree does anyone understand whats been going on? I assume its to do with scope but i'd like to understand what the problem was.I though that a %DataBlock was a class type not an object. When the %dataBlock is passed to these functions is that the instantiated object from that dataclass?
#34
Sorta like using a math function in JavaScript:
Math.abs(x);
That call is not tied to THAT particular object at all it is just a function assoicated with that datablock. So to make it usefull we pass in the actual object with the 3rd parameter.
%item.getDatablock().schedule(100, "Explode", %item);
that will then be recieved when the schedule executes
function Grenade::Explode( %this , %obj){
It isn't pretty, and probably other ways to do it, but it should work.
01/27/2005 (1:28 pm)
%dataBlock or %item.getDataBlock() returns MineGrenade . . .the object whose method ( a function that is associated with an object) is Explode. You are schedualing a call to that method. Sorta like using a math function in JavaScript:
Math.abs(x);
That call is not tied to THAT particular object at all it is just a function assoicated with that datablock. So to make it usefull we pass in the actual object with the 3rd parameter.
%item.getDatablock().schedule(100, "Explode", %item);
that will then be recieved when the schedule executes
function Grenade::Explode( %this , %obj){
It isn't pretty, and probably other ways to do it, but it should work.
#35
I am getting explode called but I dont seem to have a valid object to refer to in the explode function.
EDIT
Thanks Anthony - works fine now (even though the first one that did work killed me!)
01/27/2005 (1:28 pm)
@ Fruitbat - Did you actually get it to explode? And, if so, which explode function are you using (I have lost track of which of the above is the one to use now :(I am getting explode called but I dont seem to have a valid object to refer to in the explode function.
EDIT
Thanks Anthony - works fine now (even though the first one that did work killed me!)
#36
Many thanks.
01/27/2005 (1:57 pm)
I'd just like to say thanks guys. You've been great and I understand a little more now :)Many thanks.
#37
01/27/2005 (2:04 pm)
Quote:(even though the first one that did work killed me!)Sheesh! I don't get hurt! they explode nicely but no damage!
#38
01/27/2005 (2:13 pm)
Did you add my %pos = %obj.getPosition() to the explode function?
#39
01/27/2005 (2:21 pm)
@Anthony: Yes and during the many edits I lost it :). Is it okay with you two if i tidy this up and post it as a resource?
#40
01/27/2005 (2:27 pm)
But of course have at it.
Torque Owner FruitBatInShades