HL2 Style Weapon Selection

By Steve Acaster

Creating a weapon selection system similar to that deployed in "HalfLife2", were 2 weapons are bound to a single keybind which then toggles which weapon to use (as long as the player has them in their inventory of course).

The theory being:

  1. Receive the command and then check for availalble weapons.
  2. If only one of the two keybound weapons are available select it.
  3. If both are available select the first (if not already holding it)
  4. Toggle between the two if already holding one.

For this tutorial, weapons are sectioned by type to a key. eg: melee=1, shotguns=2, closequarters=3, rifles=4, etc.

In this script example it's displaying "closequarters" - abridged to'' CQB'' in the script functions - (key 3) and the two weapons are called Weapon1 and Weapon2.

When creating other weapon key functions make sure that you replace the correct names with your weapon names in the correct order or they won't toggle.

And don't forget to update config.cs with the new keybind (or hit deletePrefs.bat and let the engine do it itself) or your new keybind(s) won't be available.

Now make sure that you've given the player the weapons you want to use in their inventory, or they won't be able to toggle between them!

If you want to maintain standard T3D server/client script integrity, the first part should be placed in game/scripts/client/default.bind.cs, the second part in game/scripts/server/commands.cs and the final part in game/scripts/client/client.cs- or just put all of it in default.bind.cs if you're not that bothered (but it's good to get used to the idea that client and server are different).

//default.bind.cs - the keybind command
function closequarterweapons(%val)
{
   if(%val)
   {
       commandToServer('ToggleCQW');
   }
}
 
moveMap.bind(keyboard, "3", closequarterweapons);
 
//commands.cs - the command to have the server check whether the weapons are available and which one to use
function serverCmdToggleCQW(%client)  
{  
   if(%client.player.getState() $= "Dead")
      return;
 
   if(%client.player.getinventory(Weapon1) ==0 && %client.player.getinventory(Weapon2) ==0)
      return;
 
   %curWeapon = %client.player.getMountedImage($WeaponSlot);
   %weaponName = %curWeapon.item.getName();
 
   if(%weaponName !$="Weapon1" &&  %client.player.getinventory(Weapon1) !=0)
      commandToClient(%client,'ToggleCQW1',%client);
 
   if(%weaponName $="Weapon1" &&  %client.player.getinventory(Weapon2) !=0)
      commandToClient(%client,'ToggleCQW2',%client);
}
 
//client.cs - the final commands to actually use/arm the player with the appropriate 
 
function clientcmdToggleCQW1(%client)
{
   commandToServer('use',"Weapon1");
}
 
function clientcmdToggleCQW2(%client)
{
   commandToServer('use',"Weapon2");
}

The script in "plain speech":

Use keybind for CQB weapons.
If client/player is dead exit function.
If client/player does not have either keybind weapon exit function.
Get the name of the client/player's currently mounted weapon (if any).
If mounted weapon is NOT ''weapon1'' and client/player has weapon1, equip ''weapon1''.
If mounted weapon IS ''weapon1'' and client/player has ''weapon2'', equip ''weapon2''.

Lastly, if you haven't already, remember to add the ability to pickup and hold the weapons/ammo in the player's inventory (using the player's datablock) and exec any new weapon files that you had previously made.

eg: in game/art/datablocks/player.cs

datablock PlayerData(DefaultPlayerData)
{
//...
   maxInv[Weapon1] = 1;
   maxInv[Weapon1Ammo] = 5;
 
   maxInv[Weapon2] = 1;
   maxInv[Weapon2Ammo] = 10;
};
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License