Weapon Cycling in Torque
by Julian Ridley · 06/23/2004 (7:45 am) · 41 comments
Ok, for some reason, this isnt in torque to begin with, and there are no resources on it. After some struggling I figured it out, using some tweaked code from the forums. To start:
open Default.Binds.cs in client/scripts. Add this after the bind for fire:
next:
open optionsDLG.cs in client/scripts. Put this where all the others like it are:
The rest of this is done in inventory.cs in server/scripts. Open it up and at the top put:
open Default.Binds.cs in client/scripts. Add this after the bind for fire:
function prevWeapon( %val )
{
if ( %val )
commandToServer( 'cycleWeapon', "prev" );
}
function nextWeapon( %val )
{
if ( %val )
commandToServer( 'cycleWeapon', "next" );
}
moveMap.bind(keyboard, "ctrl q", prevWeapon);
moveMap.bind(keyboard, q, nextWeapon);The above simply sets up the default binds and tells them what to do. q and ctrl q can be whatever you want.next:
open optionsDLG.cs in client/scripts. Put this where all the others like it are:
$RemapName[$RemapCount] = "Previous Weapon"; $RemapCmd[$RemapCount] = "prevWeapon"; $RemapCount++; $RemapName[$RemapCount] = "Next Weapon"; $RemapCmd[$RemapCount] = "nextWeapon"; $RemapCount++;That allows the user to bind the weapon cycling to whatever they want.
The rest of this is done in inventory.cs in server/scripts. Open it up and at the top put:
$weaponInSlot[0] = "Crossbow"; $weaponInSlot[1] = "UberCrossbow"; $weaponInSlot[2] = "RocketLauncher"; $weaponInSlot[3] = "FragMine"; $maxWeaponSlot = 3;However, fill those in with your weapon names instead, and maxWeaponSlot should equal the highest number in your list, NOT the actual number of weapons. Next add this at the bottom of the file:
function ShapeBase::hasInventory(%this, %data)
{
return (%this.inv[%data] > 0);
}
function serverCmdCycleWeapon( %client, %data )
{
%client.getControlObject().cycleWeapon( %data );
}
function ShapeBase::cycleWeapon( %this, %data )
{
%slot = -1;
if ( %this.getMountedImage($WeaponSlot) != 0 ) {
%curWeapon = %this.getMountedImage($WeaponSlot).item.getName();
if(%curWeapon $= "Crossbow")
%slot = 0;
else if(%curWeapon $= "UberCrossbow")
%slot = 1;
else if(%curWeapon $= "RocketLauncher")
%slot = 2;
else if(%curWeapon $= "FragMine")
%slot = 3;
}
if ( %data $= "prev" ) {
// Previous weapon...
if ( %slot == 0 || %slot == -1 ) {
%i = $maxWeaponSlot;
%slot = 0;
}
else
%i = %slot - 1;
}
else {
// Next weapon...
if ( %slot == $maxWeaponSlot || %slot == -1 ) {
%i = 0;
%slot = $maxWeaponSlot;
}
else
%i = %slot + 1;
}
%newSlot = -1;
while ( %i != %slot ) {
if ( $weaponInSlot[%i] !$= ""
&& %this.hasInventory( $weaponInSlot[%i] )) {
// player has this weapon and it has ammo or doesn't need ammo
%newSlot = %i;
break;
}
if ( %data $= "prev" ) {
if ( %i == 0 )
%i = $maxWeaponSlot;
else
%i--;
}
else {
if ( %i == $maxWeaponSlot )
%i = 0;
else
%i++;
}
}
if ( %newSlot != -1 )
%this.use( $weaponInSlot[%newSlot] );
}Replace the series of if statements(with the weapons), with your weapons and the slots they occupy. The rest is pretty straight forward.
Torque Owner JD
$RemapName[$RemapCount] = "Previous Weapon";
$RemapCmd[$RemapCount] = "prevWeapon";
$RemapCount++;
$RemapName[$RemapCount] = "Next Weapon";
$RemapCmd[$RemapCount] = "nextWeapon";
$RemapCount++;
optionsDLG.cs in client/scripts does not exist in my directories. Where are these kept in Torque 3D? Or is there a better resource directly for Torque3d?
Update:
I put all of the above in default.bind.cs and it didn't work. However I changed my game.cs weapons to reflect the below:
case 3:
%player.setInventory(Kralmok, 1);
%player.setInventory(KralmokAmmo, %player.maxInventory(KralmokAmmo));
%player.mountImage(KralmokWeaponImage, 0);
%player.setInventory(Kral, 1);
%player.setInventory(KralClip, %player.maxInventory(KralClip));
%player.mountImage(KralWeaponImage, 0);
%player.addToWeaponCycle(Kralmok);
%player.addToWeaponCycle(Kral);
Notice I added the weapons
Everything is smoking hot now.