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
Now go to inventory.cs and replace
the
function ShapeBase::pickup(%this,%obj,%amount) codeblock with this
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
stay in weapon.cs find the
function Weapon::onUse(%data,%obj) codeblock and replace it with this
add
$WeaponSlot = 0;
now go to client/scripts/default.bind.cs and add this at the bottom
and this if you don't have it (many people should)
you should probably add the next bit of code to client/config.cs
go into the weapon file(s) for which you want dual wielding and find
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
also copy the entire
block
rename it
and find mountPoint = 0; in the new one replace that with
then find
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
find
datablock ItemData()
put
under
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!!).
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
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!!).
About the author
Recent Blogs
• Lots of Buildings (with lots of pics)• TimerBar
• AI cinematics
• Grenade Code
• land mines
#2
10/16/2005 (1:20 am)
I can't download your zip file, so colud you fix this please
#3
10/16/2005 (10:38 am)
reuploaded it now it should work fine.
#4
(dont have torque source)
good job!
10/16/2005 (11:51 am)
wow nice! glad to see that someone has improved on it. too bad i cant use it tho(dont have torque source)
good job!
#5
commands.cs
function serverCmdpreUse(%client, %data)
{
if ($Pickitup == 2){
%client.player.pickupsecondary(%data)
}else{
%client.player.pickupsecondary(%data)
}
}
now in config.cs something that would look like this
moveMap.bindCmd(keyboard, "1", "commandToServer(\'use\',\"crossbow\");", "");
should now look like this
moveMap.bindCmd(keyboard, "1", "commandToServer(\'preUse\',\"crossbow\");", "");
no go off and do the main resource but DO NOT CHANGE ANYTHING THAT IS IN PLAYER.CS AND DO NOT CHANGE WEAPON::ONPICKUP. I will go back and mark the ones you shouldn't change in the tutorial with this
NOTE: if you are running the backpacked version do not enter this portion
10/16/2005 (7:48 pm)
hmm for you I think I can change it so non sourcers can run it. I was planning on adding this later though. This will make it so you can dual wield with the backpack esque code (the current dual wielding code only allows you to have the weapons you are carrying in your hands) this makes it so when you pick up a weapon you need to throw one which non source can't do (with the exception of 1.4 I believe). So if you want to have a backpack add this.commands.cs
function serverCmdpreUse(%client, %data)
{
if ($Pickitup == 2){
%client.player.pickupsecondary(%data)
}else{
%client.player.pickupsecondary(%data)
}
}
now in config.cs something that would look like this
moveMap.bindCmd(keyboard, "1", "commandToServer(\'use\',\"crossbow\");", "");
should now look like this
moveMap.bindCmd(keyboard, "1", "commandToServer(\'preUse\',\"crossbow\");", "");
no go off and do the main resource but DO NOT CHANGE ANYTHING THAT IS IN PLAYER.CS AND DO NOT CHANGE WEAPON::ONPICKUP. I will go back and mark the ones you shouldn't change in the tutorial with this
NOTE: if you are running the backpacked version do not enter this portion
#6
10/16/2005 (7:56 pm)
I hope the two versions aren't too confusing for everybody if they are just post. I tried to make good use of bold and CAPS to make it a little easier though.
#7
10/17/2005 (1:07 pm)
Thanks for the resource this will look sweet in any game
#8
where each hand can have a different weapon
08/25/2006 (10:33 am)
a quick question, will this allow for hand independent guns? like in golden eye for the game cube?where each hand can have a different weapon
#9
08/25/2006 (5:22 pm)
yes it is made to be like Halo 2
#10
04/28/2007 (11:22 am)
nice :)
#11
Whenever I go to pick up the second weapon, the console reads that the command useSecondary can't be found.
I'm pretty sure I made all the correct changes. Heres the script thats giving me trouble.
Thanks in advance
(oh yeah, i deleted the dso's too)
07/22/2007 (5:21 pm)
I know this resource is pretty old, but hopefully someone can help me. Whenever I go to pick up the second weapon, the console reads that the command useSecondary can't be found.
I'm pretty sure I made all the correct changes. Heres the script thats giving me trouble.
function Weapon::onUse(%data,%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) != %data.image.getId()) {
serverPlay3D(WeaponUseSound,%obj.getTransform());
%obj.mountImage(%data.image, $WeaponSlot);
if (%obj.client)
messageClient(%obj.client, 'MsgWeaponUsed', '\c0Weapon selected');
}
}
function Weapon::onUseSecondary(%data,%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) != %data.image.getId()) {
serverPlay3D(WeaponUseSound,%obj.getTransform());
%obj.mountImage(%data.secondaryimage, $dualWeaponSlot);
if (%obj.client)
messageClient(%obj.client, 'MsgWeaponUsed', '\c0Weapon selected');
}
}
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);
}
%shape.use(%this);
}
}
else{
if (%shape.getClassName() $= "Player"){
if (%secondaryimage == 0) {
%amount = %shape.getInventory(%secondaryimage.ammo);
%shape.throw(%secondaryimage.ammo,%amount);
%shape.throw(%secondaryimage.item);
}
%shape.useSecondary(%this);
}
}
}
}Thanks in advance
(oh yeah, i deleted the dso's too)
#12
07/26/2007 (11:05 am)
sorry I am not in the states right now and my time online is limited Ill get an answer to you soon
#13
07/26/2007 (4:03 pm)
No problem, I'm just glad you responded.
#14
try this
in Weapon::onPickup
change
to
and
to
I am not quite sure what I was thinking there. I remember I made sure it worked before I posted it but I think that may be an error. Well try that and see how it works. I will try implementing this resource on a dry copy and check for any other bumps.
edit: I also modified the resource to recognize these changes.
07/29/2007 (10:25 am)
yes I figured you would, I had about 45 seconds to type my last comment before the computer I was on kicked me off, I was at an internet cafe. Now I have far more time.try this
in Weapon::onPickup
change
%shape.use(%this);
to
%shape.onUse(%this);
and
%shape.usesecondary(%this);
to
%shape.onUseSecondary(%this);
I am not quite sure what I was thinking there. I remember I made sure it worked before I posted it but I think that may be an error. Well try that and see how it works. I will try implementing this resource on a dry copy and check for any other bumps.
edit: I also modified the resource to recognize these changes.
#15
I made the changes you posted but it seemed to make things worse. The player couldn't pick the first weapon up with %shape.onUse, so I changed it back to %shape.use and it worked. The onUseSecondary doesn't work no matter what I do. I think the engine has trouble recognizing new weapon methods, even after I compile.
Did it work for you?
Thanks again.
07/30/2007 (11:12 am)
Glad your back Master Treb. I made the changes you posted but it seemed to make things worse. The player couldn't pick the first weapon up with %shape.onUse, so I changed it back to %shape.use and it worked. The onUseSecondary doesn't work no matter what I do. I think the engine has trouble recognizing new weapon methods, even after I compile.
Did it work for you?
Thanks again.
#16
07/31/2007 (9:56 am)
I thought I had torque on my lap top but I guess not. I am not in the states yet and Im running off of a dial up connection so downloading torque would be a pain. It could have to do something with engine changes I have mostly been programming in straight C++ and not in Torque, so I do not know all the changes that they have made. I do own 1.5 (is that the version you are running?) so when I get home I will download the latest version and try it out. I leave tommorow and I will probably be able to work it out in a couple days. I will post ASAP.
#17
if that doesn't work there might be some other stuff that you have to change but that should be it.
08/05/2007 (10:14 pm)
try thesefunction 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');
}
}
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);
}
}
}
}if that doesn't work there might be some other stuff that you have to change but that should be it.
#18
It still seems unable to find the new function. (but thanks for trying)
If you have time i would appreciate it if you would look at this thread I started
[url] http://www.garagegames.com/mg/forums/result.thread.php?qt=65541[/url]
(this could help keep your resource page clean too)
08/07/2007 (2:10 pm)
Sorry for not replying right away.It still seems unable to find the new function. (but thanks for trying)
If you have time i would appreciate it if you would look at this thread I started
[url] http://www.garagegames.com/mg/forums/result.thread.php?qt=65541[/url]
(this could help keep your resource page clean too)
#19
08/09/2007 (6:05 pm)
Anything?
#20
08/10/2007 (12:14 pm)
Got it to work perfectly now! Thanks for the great resource and for helping me so much. I guess I just made a stupid error somewhere, six times :) 
Torque Owner Dracola