Difference between revisions of "Adding a Weapon Drop System"

From Valve Developer Community
Jump to: navigation, search
(helped implemmenting in hl2mp :), it should be pretty much the same "Porting" Way into the sdk template)
Line 1: Line 1:
 
This tutorial will cover on the implementation of a valid 'weapon drop system'. This same tutorial also bears a work around on how to implement new commands that can be edited from the keyboard menu.
 
This tutorial will cover on the implementation of a valid 'weapon drop system'. This same tutorial also bears a work around on how to implement new commands that can be edited from the keyboard menu.
  
From Filip: I helped in implementing this into hl2mp, it should be pretty much the same Porting it into the sdk template(find the exact same function except at diffrent lines and instead of HL2 its SDK)
+
From Filip: I helped in implementing this into hl2mp, it should be pretty much the same Porting it into the sdk template(find the exact same function except at diffrent lines and instead of HL2 its SDK) 13:22, 19 November 2012 (PST)
  
 
==Button & Key Definitions==
 
==Button & Key Definitions==

Revision as of 21:22, 19 November 2012

This tutorial will cover on the implementation of a valid 'weapon drop system'. This same tutorial also bears a work around on how to implement new commands that can be edited from the keyboard menu.

From Filip: I helped in implementing this into hl2mp, it should be pretty much the same Porting it into the sdk template(find the exact same function except at diffrent lines and instead of HL2 its SDK) 13:22, 19 November 2012 (PST)

Button & Key Definitions

At first, you should manage to go to the Client solution and open up 'in_buttons.h'. In this file, there is a serie of definitions macros that bear numerical identifications such as

#define IN_ATTACK		(1 << 0)
#define IN_JUMP		(1 << 1)


Under

#define IN_GRENADE2		(1 << 24)	// grenade 2


Add the following piece of code:

#define IN_DROP	(1 << 25) // Weapon drop system.


When this task is completed, direct yourself to 'in_main.cpp' in the Client repertory.

At the top of the file, there are several other declarations such as:

kbutton_t	in_speed;
kbutton_t	in_walk;


Around line 102, Under

kbutton_t	in_joyspeed;


Define the main button key of the primary drop system.

kbutton_t	in_drop;


The button declaration will need to be implemented with several functions that will calculate the current status of the key events such as if the key was pressed or released.

Scroll down around line 471 and add the following functions:

void IN_DropUp( const CCommand &args ) { KeyUp( &in_drop, args[1] ); }
void IN_DropDown( const CCommand &args ) { KeyDown( &in_drop, args[1] ); }


These two functions will be used to toggle the current state of the key presses.

Go to line 1311 and under the "CalcButtonBits" definitions, after

 CalcButtonBits( bits, IN_GRENADE2, s_ClearInputState, &in_grenade2, bResetState );


add this asset of code:

 CalcButtonBits( bits, IN_DROP, s_ClearInputState, &in_drop, bResetState );


The last implementation in this file are the main console commands that will be accessed throughout the game in order to perform the drop sequence.

Around line 1468, Under

static ConCommand startgrenade2( "+grenade2", IN_Grenade2Down );


Put these console commands:


static ConCommand enddrop( "-drop", IN_DropUp );
static ConCommand startdrop( "+drop", IN_DropDown );


The main drop key system is completed. In order for this key to be performed, this will require to be called by a function that will evaluate the main key presses during the game.

Weapon drop function implementation

You shall open up 'hl2_player.cpp' and 'hl2_player.h'. or hl2mp_player.cpp and hl2mp_player.h if you are modding multiplayer

In 'hl2_player.h', around line 124(around line 87 in hl2mp_player.h), add this function declaration:

void	DropActiveWeapon( void );


This function will be responsible for performing the main weapon 'drop' occurence.

In 'hl2_player.cpp'/hl2mp_player.cpp, add this function:

void CHL2_Player::DropActiveWeapon( void )
{
	CBasePlayer *pPlayer = ToBasePlayer( UTIL_GetCommandClient() ); // Light Kill : Pointer to player
	if (!pPlayer)
		return;

	if (pPlayer->GetVehicle()) // Light Kill : If player in vehicle - do nothing
		return;

	Vector VecForward;

	EyeVectors(&VecForward, NULL, NULL);

	VecForward *= 300.0f;

	BaseClass::Weapon_Drop( GetActiveWeapon(), NULL, &VecForward );
}

change void CHL2_Player::DropActiveWeapon( void ) to void CHL2MP_Player::DropActiveWeapon( void ) if you are modding hl2mp

This function evaluates the player's current view direction and applies a velocity of 300 to the active weapon. It as well calls the actual base function that will be handle the drop system from the 'Base' class.

At the top of 'hl2_player.cpp', around line 76(758 in hl2mp_player.cpp), add this ConVar definition:

static ConVar sv_weapon_drop_enabled("sv_weapon_drop_enabled","1" /*, FCVAR_CHEAT*/ );

Note from Light Kill : User Filip, marking this convar as cheat isn't good idea because server admin will cannot change this parameter until he change "sv_cheats" to 1 and for singleplayer mods this cheat flag may be useless, i commented this flag to avoid problems, but if somebody needs to use it with "cheat flag", this can be achieved by removing /* */ symbols. Note from Filip : FCVAR_SERVER_CAN_EXECUTE is not bad as a reasonable choice(no idiot can randomly change are weapons dropable and admins can change it

To be able to react to key presses, the 'Drop' key will need to be evaluated. In 'hl2_player.cpp', around line 553, find a function named 'void CHL2_Player::PreThink(void)'(void CHL2MP_Player::PreThink( void ) in hl2mp).

At the end of the function, at line 889, you should implement this structure:

if ( m_afButtonPressed & IN_DROP )
{
   if ( IsAlive() && 
   !IsInAVehicle() && 
   HasWeapons() && 
   sv_weapon_drop_enabled.GetBool() )
   {
     DropActiveWeapon();
   }
}


The weapon drop system is enabled and fully implemented, the last adjustment is to make it customizable in the main keyboard bind menu.

Keyboard bind menu

In the (modification) folder, ex: "steamapps\sourcemods\mymod", there should be a folder named "scripts". Scroll through this folder until you reach two files named 'kb_act.lst', 'kb_def.lst'

Open up 'kb_act.lst', and at the end of this file, add the following code:

"+drop"					"#Valve_Drop_Weapon"


Open up 'kb_def.lst', and at the end of this file, add the following code:

"m"				"+drop"


In this case, 'm' can be replaced with the key of your choice.

In order for this command to appear in the main key bind menu, you will be required to extract a particuliar file from the current Source Engine's (GCF) pack. In your Steam folder, find a file named "source 2007 binaries 2.gcf" and in it, head to the resource folder and scroll down until you managed to find a file named "valve_english.txt".

Please, keep in mind that the language of the text file should correspond to your current game language. If you play (Half-Life 2) in German, then you will extract "valve_german.txt".

You shall extract this file to the main 'Resource' folder of the current (modification). In this file, under "Valve_Console_Toggle" , add this following line:

"Valve_Drop_Weapon"		"Drop current weapon"

The weapon drop system should now be completed. Launch your and go to the keyboard customization menu. Find the main line "Drop current weapon" and if it is not already bind, assign a proper key to this new command.


Thank you for your time. Sincerely, Marc-Antoine Lortie.