Game Development Community

Weapon scripting questions

by Patrick Clifford · in Torque Game Engine · 06/18/2007 (4:17 am) · 17 replies

I am new to torque and I have been working on weapons for a bit now. I have manged to locate resources troubleshoot and learn most of what i know to get my weapons to work, but i cant figure out a few things.

1) I have 10 different classes all with different weapons. I would like to make keybinds like , 1 is for pistols 2 is for rifles, ect, ect but i cant figure out how. I figured an if statement in default.bind.cs might work but i can figure out how to write it. Im thinking something similar to;

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

function equippistol(%pistoltoequip)
{
if (classid = 1)
%pistoltoequip = deserteagle;

else if (classid = 2)
%pistoltoequip = socom;
}

moveMap.bindCmd(keyboard, "1", "commandToServer('use',\"%pistoltoequip\");", "");

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


2) Damage locations. I know i seen damage location in one of the default scripts (maybe player.cs) but i cant get them to work. Are damage locations built in torque 1.5.2 and if so how do i script them?


3)Recoil. I know there is a built in camera shake feature, how can i use it to simulate a realistic recoil? I currently have my weapons script with a projectile spread, but i know there is a way to do MINspread, MAXspread, spread rate, refresh rate ect ect, but cant get it to work. I found a resource to add recoil similar (along with a crosshair mod i dont want) but its old and i think this can be done without engine mods.


I know im asking for alot of information, but i have spent alot of time trying to learn all the weapon scripts but cant figure out these three things. Any help is greatly appreciated.

#1
06/18/2007 (4:37 am)
Here is what i seen in player.cs that leads me to think there is a built in damage location system;

// Damage location details
boxNormalHeadPercentage = 0.83;
boxNormalTorsoPercentage = 0.49;
boxHeadLeftPercentage = 0;
boxHeadRightPercentage = 1;
boxHeadBackPercentage = 0;
boxHeadFrontPercentage = 1;

How do i use this stuff?
#2
06/19/2007 (11:00 am)
The boxes are calculated with the boundingbox height * percentage set in the player.cs
like you have above.
If you have a boundingBox = "1.2 1.2 2.3";
The engine calculate 2.3 * 0.83 = 1.909 everything above this you hit the head.
And some calculation for front,back,sides.

So for some setup depending on what you want todo with it.
You can use it both in player.cs and in your weapon scripts like crossbow.cs.
In the player.cs try this in
function Armor::damage(..)
{
    ..
    %damagelocation = %obj.getDamageLocation(%position);
    error("Detail Damage Location = " SPC %damagelocation);
   %damagelocation = firstWord(%obj.getDamageLocation(%position));
    error("Damage Location = " SPC %damagelocation);
    ..
}

or in your weapon script , this is for testing in crossbow.cs
function CrossbowProjectile::onCollision(..)
{

    // Apply damage to the object all shape base objects
  if (%col.getType() & $TypeMasks::ShapeBaseObjectType)
     {
         %damageLoc = firstWord(%col.getDamageLocation(%pos));
          error("Target DamageLocation: " SPC %damageLoc);
          if(%damageLoc $= "head")
          {
             %damagemod = 2;
          }
          if(%damageLoc $= "torso")
          {
		 %damagemod = 1;
          }
          if(%damageLoc $= "legs")
           {
             %damagemod = 0.5;
           }
         %col.damage(%obj,%pos,%damagemod * %this.directDamage,"CrossbowBolt");
      }
}
This is some examples you need to tweak them for your needs !!
#3
06/19/2007 (12:48 pm)
For your first question, I'd say you were on the right track with what you have there, but it wouldnt work correctly because %pistoltoequip variable is a local variable in the equippistol function, so you can't reference it outside the function like you're doing.

I'd do something like this:

//$classid is a global variable set when the player first chooses a class

function bindWeapons()
{
       if ($classid == 1)
       {
           moveMap.bindCmd(keyboard, "1", "commandToServer('use',\"deserteagle\");", "");
           ....
       }

       else if ($classid == 2)
       {
           moveMap.bindCmd(keyboard, "1", "commandToServer('use',\"socom\");", "");
           ....
       }
}


don't forget to actually call bindWeapons() at some point before the game starts.
#4
06/19/2007 (12:51 pm)
I would create completely different keyboard mappings for each class. Then when the player enters the game you just make one push call and everything is there.
#5
06/19/2007 (1:15 pm)
Well i learned my bounding box was way too big for my avatar (the soldier pack) I now have the bounding box at (".5 .5 1.5") but i have a problem.

Now that ive decreased the box to actualy fix my avatar (it was WAY too big before) my weapon does not show up in 1st person veiw. If it set (one of) the first two values back up it will show again. How can i keep this current box size and get my weapon to show in first person.

And thanks a million for teaching me how to ustilize this feature.
#6
06/19/2007 (2:02 pm)
No matter how i change the values in

// Damage location details
boxNormalHeadPercentage = 0.83;
boxNormalTorsoPercentage = 0.49;
boxHeadLeftPercentage = 0;
boxHeadRightPercentage = 1;
boxHeadBackPercentage = 0;
boxHeadFrontPercentage = 1;

nothing changes. Head shots count too low and same with torso. Ive changed the

boxNormalHeadPercentage = 0.83;
boxNormalTorsoPercentage = 0.49;

to the extreams and still no change in damage location. I got the damage multiplier working ok, its just the avatars head and torso locations wont change.
#7
06/19/2007 (6:06 pm)
If works for me , but my models are like the default ones.
I get legs,torso and head without any problems.
Try lower the percentage on the torso and see what you get.
#8
06/20/2007 (6:48 am)
Like i said, ive changed those values to every possible configuration, they arent making a difference at all. Thats not my main concern though;

"Now that ive decreased the boundingbox to actualy fix my avatar (it was WAY too big before) my weapon does not show up in 1st person veiw. If it set the second value back up it will show again. How can i keep this current box size and get my weapon to show in first person."
#9
06/20/2007 (7:07 am)
The bounding box has to take into account any protruding objects, such as a weapon. Make it bigger.
#10
06/20/2007 (9:13 pm)
Right now i have the second value large enough for the hands, but now the box is huge and you can hit my player without actualy hitting him, just hitting the way too big box.
#11
06/20/2007 (9:41 pm)
That's pretty much how player collision in Torque works. It's an old system (bounding boxes) and it has its drawbacks, such as being able to shoot players without actually hitting them and players that float a little above the ground when on sloped terrain.
#12
06/21/2007 (12:04 am)
Alright, i guess ill have to search for a good collision resource. I think ive seen one before. Thanks for the help!

What about recoil? Is there a way to do this without engine mods? Ive found a link to what sounds like a good recoil resource, but the link is for torque1.4 owners only and i only have access to 1.5.
#13
06/21/2007 (12:46 am)
Myself & a few others came up with some scripted recoil solutions in this forum thread: Weapon recoil/kickback

Season to taste:
//-----------------------------------------------------------------------------
// Basic Recoil Functions (Client)
// Usage: CommandToClient(%obj.client, 'Recoil', 'RecoilType');
//-----------------------------------------------------------------------------
function clientCmdRecoil(%type) 
{
   switch$ (%type)
   {
      case "Light":
         $mvPitch += -0.0050;
      case "Medium":
         $mvPitch += -0.01;
         $mvYaw += getRandom() ? 0.008 : -0.008;
      case "Heavy":
         $mvPitch += -0.05;
         $mvYaw += getRandom() ? 0.02 : -0.02;
      case "ExtraHeavy":
         $mvPitch += -0.10;
         $mvYaw += getRandom() ? 0.1 : -0.1;
   }
}
//-----------------------------------------------------------------------------
To use, just place this in your weapon's "onFire" function:
CommandToClient(%obj.client, 'Recoil', "Heavy");
#14
06/21/2007 (3:44 am)
Tim Heldna you are a god!

Thanks alot for this, im going to try it out right now.
#15
06/21/2007 (6:48 am)
I tried out your recoil script and it is awesome! I also tried another one from that topic you linked and i like yours better. It would be nice it after the crosshair "jump" it fell back in place, but thats not a big deal since im going for a realism type fps.

Only question is does it work over network yet? If not cant i place the weaponfx.cs file on the client side and call it from there? I realize it will make it easily hackable, but thats not a worry of mine. Is this possible and if so can you explain realy quick on how to do it?
#16
06/21/2007 (7:15 am)
It works over a network as is - just make sure the "clientCmdRecoil" function script is client side.

Disregard what was written in that post about networking, the solution I provided you with is the end result, that works across a network.
#17
06/21/2007 (9:14 am)
Oops, i put the weaponfx.cs in the server. Its in the client now, thanks again for this great resource.