Game Development Community

Grenade help

by Nathan · in General Discussion · 12/26/2005 (5:45 pm) · 41 replies

I'm trying to get this grenade script to work, but I can't for some reason.
How can I get it so whenever I click the right mouse button the player throws a grenade?

Heres the link to the grenade script:
http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=7075

Thanks for the help,
Nathan
Page «Previous 1 2 3 Last »
#1
12/27/2005 (6:31 am)
This is the script:

//-----------------------------------------------------------
// 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 = "~/data/shapes/rocket_launcher/rocket.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 shapes velocity
%vec = vectorAdd(%vec,%this.getVelocity());
// Set the objects 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);
}


I tried: moveMap.bind( mouse, button1, ShapeBase.throwGrenadeDB );
But that didn't seem to work. How do I bind the right mouse button to call throwgrenade?
#2
12/27/2005 (12:15 pm)
Nathan

I think you need both a command to server call and a server side command to throw grenades properly.

This will map the RMB to a function called mousegrenade. Put the map in default.bind.cs:-
moveMap.bind( mouse, button1, mousegrenade );


The function mousegrenade also goes on default.bind.cs :-
function mousegrenade(%val)
{
   if (%val)
      commandToServer('throwGrenade');
}
That style of binding is used because the function gets called once when the key or button is pressed and once when it is released. When it is pressed, %val will be 1 and when it is released %val will be zero so you can have one function that will do something on a keydown and something else on a keyrelease. Here though we just check for a press %val being true (or 1 in this case). This function sends a command to the server to run the function serverCmdThrowGrenade


In your grenade.cs file put the serverside command :
function serverCmdThrowGrenade(%client)
{
   %client.player.throwGrenadeDB(Grenade,%client, 5000);
}

Hopefully you can now throw grenades with your RMB
#3
12/27/2005 (3:56 pm)
Thanks, I'll try that.
#4
12/27/2005 (4:43 pm)
Somethings horribly wrong! when I press the right mouse button the game crashes and saying that I have the wrong version of the game and that there was an invalid packet. Something must be pretty wrong because I have to crash my computer every time the game crashes. I did what you said, and here's my scripts - and all of your changes are at the bottom of the script files:

-----------------------------------------------------------------------------------------------------------------------------------------

default.bind.cs - in client folder



//-----------------------------------------------------------------------------
// Torque Game Engine
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------


//------------------------------------------------------------------------------
// Key bindings for the in-game action Map
//------------------------------------------------------------------------------

if ( isObject( moveMap ) )
moveMap.delete();
new ActionMap(moveMap);

//------------------------------------------------------------------------------
// Misc. in-game keys

function escapeFromGame()
{
if ( $Server::ServerType $= "SinglePlayer" )
MessageBoxYesNo( "Quit Mission", "Exit from this Mission?", "disconnect();", "");
else
MessageBoxYesNo( "Disconnect", "Disconnect from the server?", "disconnect();", "");
}

moveMap.bindCmd(keyboard, "escape", "", "escapeFromGame();");


//------------------------------------------------------------------------------
// Movement Keys

$movementSpeed = 1; // m/s

function setSpeed(%speed)
{
if(%speed)
$movementSpeed = %speed;
}

function moveleft(%val)
{
$mvLeftAction = %val * $movementSpeed;
}

function moveright(%val)
{
$mvRightAction = %val * $movementSpeed;
}

function moveforward(%val)
{
$mvForwardAction = %val * $movementSpeed;
}

function movebackward(%val)
{
$mvBackwardAction = %val * $movementSpeed;
}

function moveup(%val)
{
$mvUpAction = %val * $movementSpeed;
}

function movedown(%val)
{
$mvDownAction = %val * $movementSpeed;
}

function turnLeft( %val )
{
$mvYawRightSpeed = %val ? $Pref::Input::KeyboardTurnSpeed : 0;
}

function turnRight( %val )
{
$mvYawLeftSpeed = %val ? $Pref::Input::KeyboardTurnSpeed : 0;
}

function panUp( %val )
{
$mvPitchDownSpeed = %val ? $Pref::Input::KeyboardTurnSpeed : 0;
}

function panDown( %val )
{
$mvPitchUpSpeed = %val ? $Pref::Input::KeyboardTurnSpeed : 0;
}

function getMouseAdjustAmount(%val)
{
// based on a default camera fov of 90'
return(%val * ($cameraFov / 90) * 0.01);
}

function yaw(%val)
{
$mvYaw += getMouseAdjustAmount(%val);
}

function pitch(%val)
{
$mvPitch += getMouseAdjustAmount(%val);
}

function jump(%val)
{
$mvTriggerCount2++;
}

function mouseTrigger(%val)
{
$mvTriggerCount0++;
}

moveMap.bind( keyboard, a, moveleft );
moveMap.bind( keyboard, d, moveright );
moveMap.bind( keyboard, w, moveforward );
moveMap.bind( keyboard, s, movebackward );
moveMap.bind( keyboard, space, jump );

moveMap.bind( mouse, xaxis, yaw );
moveMap.bind( mouse, yaxis, pitch );
moveMap.bind( mouse, button0, mouseTrigger );


//------------------------------------------------------------------------------
// Camera & View functions

function toggleFreeLook( %val )
{
if ( %val )
$mvFreeLook = true;
else
$mvFreeLook = false;
}

function toggleFirstPerson(%val)
{
if (%val)
$firstPerson = !$firstPerson;
}

function toggleCamera(%val)
{
if (%val)
commandToServer('ToggleCamera');
}

moveMap.bind( keyboard, z, toggleFreeLook );
moveMap.bind(keyboard, tab, toggleFirstPerson );
moveMap.bind(keyboard, "alt c", toggleCamera);


//------------------------------------------------------------------------------
// Helper functions used by the mission editor

function dropCameraAtPlayer(%val)
{
if (%val)
commandToServer('dropCameraAtPlayer');
}

function dropPlayerAtCamera(%val)
{
if (%val)
commandToServer('DropPlayerAtCamera');
}

moveMap.bind(keyboard, "F8", dropCameraAtPlayer);
moveMap.bind(keyboard, "F7", dropPlayerAtCamera);


//------------------------------------------------------------------------------
// Key bindings for the Global action map available everywhere
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// Misc.

GlobalActionMap.bind(keyboard, "tilde", toggleConsole);
GlobalActionMap.bindCmd(keyboard, "alt enter", "", "toggleFullScreen();");


//------------------------------------------------------------------------------
// Dubuging Functions

$MFDebugRenderMode = 0;
function cycleDebugRenderMode(%val)
{
if (!%val)
return;

if (getBuildString() $= "Debug")
{
if($MFDebugRenderMode == 0)
{
// Outline mode, including fonts so no stats
$MFDebugRenderMode = 1;
GLEnableOutline(true);
}
else if ($MFDebugRenderMode == 1)
{
// Interior debug mode
$MFDebugRenderMode = 2;
GLEnableOutline(false);
setInteriorRenderMode(7);
showInterior();
}
else if ($MFDebugRenderMode == 2)
{
// Back to normal
$MFDebugRenderMode = 0;
setInteriorRenderMode(0);
GLEnableOutline(false);
show();
}
}
else
{
echo("Debug render modes only available when running a Debug build.");
}
}

GlobalActionMap.bind(keyboard, "F9", cycleDebugRenderMode);



function mousegrenade(%val)
{
if (%val)
commandToServer('throwGrenade');
}

moveMap.bind( mouse, button1, mousegrenade );
#5
12/27/2005 (4:44 pm)
Grenade.cs - in server folder



//-----------------------------------------------------------
// 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 = "~/data/shapes/rocket_launcher/rocket.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 shapes velocity
%vec = vectorAdd(%vec,%this.getVelocity());
// Set the objects 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);
}



function serverCmdThrowGrenade(%client)
{
%client.player.throwGrenadeDB(Grenade,%client, 5000);
}
#6
12/27/2005 (8:54 pm)
GlobalActionMap.bind(keyboard, "F9", cycleDebugRenderMode);



function mousegrenade(%val)
{
if (%val)
commandToServer('throwGrenade');
}

moveMap.bind( mouse, button1, mousegrenade );

should be...

GlobalActionMap.bind(keyboard, "F9", cycleDebugRenderMode);



function mousegrenade(%val)
{
if (%val)
commandToServer('ThrowGrenade');
}

moveMap.bind( mouse, button1, mousegrenade );
#7
12/28/2005 (2:12 am)
Nathan

Do you get any errors in your console.log file?

The functions I gave should work as they are the ones I use (although I do use the G key rather than the mousebutton)

Looking at the script, the Grenade item is using the rocket shape - do you have that in the right place in your folders?

shapeFile = "~/data/shapes/rocket_launcher/rocket.dts";
#8
12/28/2005 (6:09 am)
@ mr. pig - torque script is case sensitive?


@David - I'm using the rocket shape because I don't have a grenade model. But yes, the rocket is in the right location as I'm using it for other weapons too. And yes I do have errors:


Mapping string: throwGrenade to index: 3
ShapeBase::throwGrenade grenade, creating item
ShapeBase::throwObject grenade, getting direction
ShapeBase::throwObject grenade, Applying impulse
Connection error: Invalid packet..
Grenade::Explode called
Grenade::Explode Doing Damaged
radiusDamage called!
Grenade::Damage called
#9
12/28/2005 (6:56 am)
Looking at the error and when it happens I think you have this problem.
#10
12/28/2005 (7:12 am)
Um... I don't own torque yet. I can't view that thread.
#11
12/28/2005 (8:34 am)
Could you copy -> paste the thread here? I think you're allowed to do that as long as it doesn't contain engine source code.
#12
12/28/2005 (8:56 am)
Why don't you just use the Projectile class for grenades? That's what I do in Illumina. The functionality is pretty much automatic and, IMO, Projectiles act alot more like grenades than Items.
#13
12/28/2005 (9:11 am)
@Nathan - forgot.
#14
12/28/2005 (9:11 am)
I don't know how to that.
#15
12/28/2005 (9:16 am)
@ mr. pig - So torque script isn't case sensitive?
#16
12/28/2005 (9:30 am)
@ Josh moore - To use the projectile class would the player have to have a weapon mounted to fire the projectile?
#17
12/28/2005 (9:43 am)
No, of course not. If you look at the crossbow script, you'll see that the projectile is created in script just like any other object.
#18
12/28/2005 (10:07 am)
Just to expand on what Josh said, Torque projectiles can do amazing things. They even have bounce/elasticity settings, variable gravity affectors, delays, etc. In fact, just the other day, I accidentally got the Torque crossbow projectile to behave like Unreal Tournament Bio-rifle blobs -- I took damage when I stepped on one. Very cool stuff.
#19
12/28/2005 (10:50 am)
I can't get the projectile thing to work. Could you show me how you did it.
#20
12/28/2005 (11:06 am)
@Nathan - I know vars are not, not sure about functions. It's good to keep everything the same throughout the program though.
Page «Previous 1 2 3 Last »