Game Development Community

Dual Wielding

by Dracola · 10/16/2005 (12:31 am) · 23 comments

Download Code File

well to start it off this code is derived from Hugh "scooby" Brown's and I use code derived from Adam "TheSnowMan" Beaty's weapon and ammo drop thanks to them both. Also I think 1.4 has this update but 1.3 won't run this unless you put this small piece of code in your source. Sorry you're going to need a torque license to run this straight. And if you might want to take a back up because there will be A LOT of over writing going down and it should work but wierd things happen Now onto the code! go to something that should look like this
this resource as it is in a way deletes your backpack if you don't want to delete the backpack you can check my second post and do as it says.

NOTE: if you are running the backpacked version do not enter this portion
function Armor::onCollision(%this,%obj,%col,%vec,%speed)
in your player.cs
replace the code in that block with this
if (%obj.getState() $= "Dead")
      return;
      echo("colis"@%col.dataBlock);
   if (%col.dataBlock.className $= "Weapon"){
   if ($Pickitup == 1){
   %obj.pickup(%col);
   }
   if ($Pickitup == 2){
   %obj.pickupsecondary(%col);
   }
   if ($Pickitup == 0){
   echo(" press E");
   }
   }else if (%col.getClassName() $= "item"){
   %obj.pickup(%col);
   }
}

Now go to inventory.cs and replace
the
function ShapeBase::pickup(%this,%obj,%amount) codeblock with this


function ShapeBase::pickup(%this,%obj,%amount)
{
   // This method is called to pickup an object and add it
   // to the inventory. The datablock onPickup method is actually
   // responsible for doing all the work, including incrementing
   // the inventory.
   %data = %obj.getDatablock();
   %secondary = 0;

   // Try and pickup the max if no value was specified
   if (%amount $= "")
      %amount = %this.maxInventory(%data) - %this.getInventory(%data);

   // The datablock does the work...
   if (%amount < 0)
      %amount = 0;
      return %data.onPickup(%obj,%this,%amount,%secondary);
   return false;
}
%secondary = 0;
function ShapeBase::pickupsecondary(%this,%obj,%amount)
{
   // This method is called to pickup an object and add it
   // to the inventory. The datablock onPickup method is actually
   // responsible for doing all the work, including incrementing
   // the inventory.
   %data = %obj.getDatablock();
   %secondary = 1;

   // Try and pickup the max if no value was specified
   if (%amount $= "")
      %amount = %this.maxInventory(%data) - %this.getInventory(%data);

   // The datablock does the work...
   if (%amount < 0)
      %amount = 0;
      return %data.onPickup(%obj,%this,%amount,%secondary);
   return false;
}

No go to weapon.cs find the code that looks like this

NOTE: if you are running the backpacked version do not enter this portion

function Weapon::onPickup(%this, %obj, %shape, %amount) and replace that whole code block with this

function Weapon::onPickup(%this, %obj, %shape, %amount,%secondary)
{
   // The parent Item method performs the actual pickup.
   // For player's we automatically use the weapon if the
   // player does not already have one in hand.
   %secondaryimage = %shape.getMountedImage($dualWeaponSlot);
   %image = %shape.getMountedImage($WeaponSlot);
   if (Parent::onPickup(%this, %obj, %shape, %amount)) {
      serverPlay3D(WeaponPickupSound,%obj.getTransform());
      if (%secondary == 0){
      if (%shape.getClassName() $= "Player") {
            if (%image != 0)  {
            %amount = %shape.getInventory(%image.ammo);
            %shape.throw(%image.ammo,%amount);
            %shape.throw(%image.item);
            }
         %this.onUse(%shape);
      }
      }
      else{
      if (%shape.getClassName() $= "Player"){
            if (%secondaryimage != 0)  {
            %amount = %shape.getInventory(%secondaryimage.ammo);
            %shape.throw(%secondaryimage.ammo,%amount);
            %shape.throw(%secondaryimage.item);
            }
            %this.onUseSecondary(%shape);
      }
      }
   }
}

stay in weapon.cs find the
function Weapon::onUse(%data,%obj) codeblock and replace it with this

function Weapon::onUse(%this,%obj)
{
   // Default behavoir for all weapons is to mount it into the
   // this object's weapon slot, which is currently assumed
   // to be slot 0
   if (%obj.getMountedImage($WeaponSlot) != %this.image.getId()) {
      serverPlay3D(WeaponUseSound,%obj.getTransform());
      %obj.mountImage(%this.image, $WeaponSlot);
      if (%obj.client)
         messageClient(%obj.client, 'MsgWeaponUsed', '\c0Weapon selected');
   }
}
function Weapon::onUseSecondary(%this,%obj)
{
   // Default behavoir for all weapons is to mount it into the
   // this object's weapon slot, which is currently assumed
   // to be slot 0
   if (%obj.getMountedImage($dualWeaponSlot) != %this.image.getId()) {
      serverPlay3D(WeaponUseSound,%obj.getTransform());
      %obj.mountImage(%this.secondaryimage, $dualWeaponSlot);
      if (%obj.client)
         messageClient(%obj.client, 'MsgWeaponUsed', '\c0Weapon selected');
   }
}

add
$dualWeaponSlot = 1;
to the top under
$WeaponSlot = 0;

now go to client/scripts/default.bind.cs and add this at the bottom

function pickupitem(%val){
if (%val)
$Pickitup = 1;
else
$Pickitup = 0;
}
function pickupsecondary(%val){
if (%val)
$Pickitup = 2;
else
$Pickitup = 0;
}
moveMap.bind(keyboard, "e", pickupitem);
moveMap.bind(keyboard, "f", pickupsecondary);

and this if you don't have it (many people should)

function altTrigger(%val)
{
   $mvTriggerCount1++;
}
moveMap.bind( mouse, button1, altTrigger );

you should probably add the next bit of code to client/config.cs

MoveMap.bind(mouse0, "button0", mouseFire);
MoveMap.bind(mouse0, "button2", toggleZoom);
MoveMap.bind (mouse0, "button1", altTrigger);
MoveMap.bind(keyboard, "f", pickupsecondary);
MoveMap.bind(keyboard, "e", pickupitem);

go into the weapon file(s) for which you want dual wielding and find

datablock ShapeBaseImageData(<yourweapon>Image)

obviously would be your weapons name
copy that whole code block but you don't need to copy the onfire stuff

rename the new one

datablock ShapeBaseImageData(secondary<yourweapon>Image)

also copy the entire

<yourweapon>Image::onFire

block

rename it

secondary<yourweapon>Image::onFire

and find mountPoint = 0; in the new one replace that with

mountPoint = 1;

then find

eyeOffset = "0.1 0.4 -0.6";

those numbers are the ones used on the crossbow yours may be different anyway take the far left number and put a "-" infront of it like so

eyeOffset = "-0.1 0.4 -0.6";

find
datablock ItemData()
put

secondaryimage = secondary<yourweapon>Image;

under

image = <yourweapon>Image;


Its done now!! go in and blow people up with your dual shotguns. you should not that you have no back pack now I plan to make another resource that corresponds with this one that makes it more Halo like where you hold one weapon and press tab to switch to one in your back pack. If the way it works doesn't suit you and you don't know how it works complain and I'll fix it. Also note that your player model must have a mount point on his left hand preferably called mountpoint1. If you're left handed mount point is a different number find $dualWeaponSlot and change it to that number. I uploaded a robot type guy who has a left handed mount point (Now you suffer for my bad player modeling!!).
Page«First 1 2 Next»
#21
08/10/2007 (8:39 pm)
Well the resource did certainly need some touching up. I'm glad to help, I need a break form my projects every once and a while and helping others is a fun way to do that.
#22
02/08/2008 (6:22 pm)
I know this is an old resource but I'm stuck. I'm not even sure what I'm stuck on since I only have the demo and no license. Anyways, what I can tell is that when the mission loads, it freezes loading the 20th ghost object. It worked fine before I tried adding this code. Any ideas?
#23
04/18/2009 (11:56 am)
Well I tried it out. And for some reason when you are dual wielding and you try to pick up another weapon in the second slot you throw your first weapon. How can I get it to throw the second?

Also your Echo("Press E") doesn't work. It would be nice to have a text control to tell you when your near it to pick it up. Like Halo...hint hint ;)

Is there anyway to get the ammo and weapon drop to work with this script?
Page«First 1 2 Next»