Lowering your weapon on sprint: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
No edit summary
m (Nesciuse moved page Lowering your weapon on sprint/en to Lowering your weapon on sprint without leaving a redirect: Move en subpage to basepage)
 
(24 intermediate revisions by 11 users not shown)
Line 1: Line 1:
This article will deal with Lowering your weapon while sprinting and disable the ability to shoot at the same time.
{{LanguageBar}}
This was based on a topic on the Steam Forums located here: [http://forums.steampowered.com/forums/showthread.php?t=829259&highlight=lowering]
{{Underlinked|date=January 2024}}
{{Dead end|date=January 2024}}


{{note|1=This has only been tested with the latest HL2MP OB engine code.}}
This article provides a basic implementation of weapon lowering while sprinting, and preventing weapon fire while doing so.
{{note|1=This will not work if your weapon doesn't have the ACT_VM_IDLE_LOWERED animation in its .qc file (The Viewmodel). To fix this install Studio Compiler and decompile your Viewmodel and decompile one of your other models which has this animation, then add that animation into your decompiled .qc file. To find out how to add the animation just copy the animation from the working viewmodel .qc file.}}
 
{{note|1=If your weapon_yourweaponname.cpp has an ItemPostFrame in it overriding the base ItemPostFrame then you have to place this code in there as well, for example in the weapon_shotgun.cpp, it has an ItemPostFrame overriding the Baseweapon_Shared ItemPostFrame which means that the lowering weapon code will not work for this weapon, unless you add this code in the weapon's ItemPostFrame function!}}
{{note|This will not work if your viewmodel doesn't have ACT_VM_IDLE_LOWERED animation declared somewhere in the .qc file.}}
{{note|1=Download the Studio Compiler here [http://www.chaosincarnate.net/cannonfodder/download.php?id=StudioCompilerSetup.V0.4A.exe] (only necessary if your weapon don't got the ACT_VM_IDLE_LOWERED animation!)}}
 
{{note|If your weapon_yourweaponname.cpp is overriding ItemPostFrame then you have to place BaseClass::ProcessAnimationEvents(); in the ItemPostFrame of that class but only if you aren't calling BaseClass::ItemPostFrame();. For example in the weapon_shotgun.cpp it has an override of ItemPostFrame which means that the lowering weapon code will not be called for this weapon unless you add a call to it! '''Remember to put the Basecombatweapon_Shared.h declarations under public or protected so that you can make use of these variables in classes which inherit basecombatweapon_shared, like the weapon_shotgun.cpp's ItemPostFrame. If you don't do this you will get undeclared identifier errors'''}}


== The Code ==
== The Code ==
 
{{note|This has been tested with HL2MP, 2013 and Alien Swarm SDKS.}}
{{note|1=In this Tutorial you will do this by coding only, so open up your basecombatweapon_shared.cpp and basecombatweapon_shared.h}}


===BASECOMBATWEAPON_SHARED.CPP===
===BASECOMBATWEAPON_SHARED.CPP===
Line 18: Line 19:


void CBaseCombatWeapon::ItemPostFrame( void )
void CBaseCombatWeapon::ItemPostFrame( void )
{
CBasePlayer *pOwner = ToBasePlayer( GetOwner() );
if (!pOwner)
return;


</source>
</source>


Then copy and paste this below it:
Then copy and paste this above it:


<source lang=cpp>
<source lang=cpp>


if ( !bLowered && (pOwner->m_nButtons & IN_SPEED ) )
void CBaseCombatWeapon::ProcessAnimationEvents(void)
{
{
bLowered = true;
CBasePlayer *pOwner = ToBasePlayer( GetOwner() );
SendWeaponAnim( ACT_VM_IDLE_LOWERED );
if (!pOwner)
m_fLoweredReady = gpGlobals->curtime + GetViewModelSequenceDuration();
return;
}
else if ( bLowered && !(pOwner->m_nButtons & IN_SPEED ) )
{
bLowered = false;
SendWeaponAnim( ACT_VM_IDLE );
m_fLoweredReady = gpGlobals->curtime + GetViewModelSequenceDuration();
}


if ( bLowered )
if ( !m_bWeaponIsLowered && (pOwner->m_nButtons & IN_SPEED ) )
{
if ( gpGlobals->curtime > m_fLoweredReady )
{
{
bLowered = true;
m_bWeaponIsLowered = true;
SendWeaponAnim( ACT_VM_IDLE_LOWERED );
SendWeaponAnim( ACT_VM_IDLE_LOWERED );
m_fLoweredReady = gpGlobals->curtime + GetViewModelSequenceDuration();
m_flNextPrimaryAttack = gpGlobals->curtime + GetViewModelSequenceDuration();
        m_flNextSecondaryAttack = m_flNextPrimaryAttack;
}
}
return;
else if ( m_bWeaponIsLowered && !(pOwner->m_nButtons & IN_SPEED ) )
}
else if ( bLowered )
{
if ( gpGlobals->curtime > m_fLoweredReady )
{
{
bLowered = false;
m_bWeaponIsLowered = false;
SendWeaponAnim( ACT_VM_IDLE );
SendWeaponAnim( ACT_VM_IDLE );
m_fLoweredReady = gpGlobals->curtime + GetViewModelSequenceDuration();
m_flNextPrimaryAttack = gpGlobals->curtime + GetViewModelSequenceDuration();
        m_flNextSecondaryAttack = m_flNextPrimaryAttack;
}
 
if ( m_bWeaponIsLowered )
{
if ( gpGlobals->curtime > m_flNextPrimaryAttack )
{
SendWeaponAnim( ACT_VM_IDLE_LOWERED );
m_flNextPrimaryAttack = gpGlobals->curtime + GetViewModelSequenceDuration();
m_flNextSecondaryAttack = m_flNextPrimaryAttack;
}
}
}
return;
}
}


</source>
</source>


===BASECOMBATWEAPON_SHARED.H===
Now to make sure our code gets called we add ProcessAnimationEvents(); here:


Add these member variables under private at line 466 below '''CNetworkVar( CBaseCombatCharacterHandle, m_hOwner ); // Player carrying this weapon''':
<source lang=cpp>
<source lang=cpp>


bool                    m_bLowered;
void CBaseCombatWeapon::ItemPostFrame( void )
bool                    bLowered;
{
float                  m_fLowered;
      //Add this underneath the pOwner accessor check.
float                  m_fLoweredReady;
      ProcessAnimationEvents();
}


</source>
</source>


==Finish==
===BASECOMBATWEAPON_SHARED.H===
 
Now your weapon should be lowered when you hit the sprint button and you will not be available to shoot either until you release the button!
 


Add these declarations under public at line 211 below '''bool IsViewModelSequenceFinished( void ); // Returns if the viewmodel's current animation is finished''':
{{note|We make ProcessAnimationEvents virtual so that we can override it in classes which inherit this class.}}


<source lang=cpp>


virtual void            ProcessAnimationEvents(void);
bool                    m_bWeaponIsLowered;


</source>


==Finish==


Now your weapon should be lowered when you hit the sprint button and you will not be able to shoot until you release the button!
[[File:Sprint.gif|none|Finished Product]]


[[Category:Weapons programming]] {{DISPLAYTITLE:Lowering your weapon on sprint}}
{{Uncategorized|date=January 2024}}
[[Category:Weapons programming]]
[[Category:Tutorials]]
[[Category:Tutorials]]

Latest revision as of 08:13, 12 July 2024

English (en)Português do Brasil (pt-br)Translate (Translate)
Underlinked - Logo.png
This article needs more Wikipedia icon links to other articles to help Wikipedia icon integrate it into the encyclopedia. Please help improve this article by adding links Wikipedia icon that are relevant to the context within the existing text.
January 2024
Dead End - Icon.png
This article has no Wikipedia icon links to other VDC articles. Please help improve this article by adding links Wikipedia icon that are relevant to the context within the existing text.
January 2024

This article provides a basic implementation of weapon lowering while sprinting, and preventing weapon fire while doing so.

Note.pngNote:This will not work if your viewmodel doesn't have ACT_VM_IDLE_LOWERED animation declared somewhere in the .qc file.
Note.pngNote:If your weapon_yourweaponname.cpp is overriding ItemPostFrame then you have to place BaseClass::ProcessAnimationEvents(); in the ItemPostFrame of that class but only if you aren't calling BaseClass::ItemPostFrame();. For example in the weapon_shotgun.cpp it has an override of ItemPostFrame which means that the lowering weapon code will not be called for this weapon unless you add a call to it! Remember to put the Basecombatweapon_Shared.h declarations under public or protected so that you can make use of these variables in classes which inherit basecombatweapon_shared, like the weapon_shotgun.cpp's ItemPostFrame. If you don't do this you will get undeclared identifier errors

The Code

Note.pngNote:This has been tested with HL2MP, 2013 and Alien Swarm SDKS.

BASECOMBATWEAPON_SHARED.CPP

Hit CTRL+F and search for this code:

void CBaseCombatWeapon::ItemPostFrame( void )

Then copy and paste this above it:

void CBaseCombatWeapon::ProcessAnimationEvents(void)
{
	CBasePlayer *pOwner = ToBasePlayer( GetOwner() );
	if (!pOwner)
		return;

	if ( !m_bWeaponIsLowered && (pOwner->m_nButtons & IN_SPEED ) )
	{
		m_bWeaponIsLowered = true;
		SendWeaponAnim( ACT_VM_IDLE_LOWERED );
		m_flNextPrimaryAttack = gpGlobals->curtime + GetViewModelSequenceDuration();
	        m_flNextSecondaryAttack = m_flNextPrimaryAttack;
	}
	else if ( m_bWeaponIsLowered && !(pOwner->m_nButtons & IN_SPEED ) )
	{
		m_bWeaponIsLowered = false;
		SendWeaponAnim( ACT_VM_IDLE );
		m_flNextPrimaryAttack = gpGlobals->curtime + GetViewModelSequenceDuration();
 	        m_flNextSecondaryAttack = m_flNextPrimaryAttack;
	}

	if ( m_bWeaponIsLowered )
	{
		if ( gpGlobals->curtime > m_flNextPrimaryAttack )
		{
			SendWeaponAnim( ACT_VM_IDLE_LOWERED );
			m_flNextPrimaryAttack = gpGlobals->curtime + GetViewModelSequenceDuration();
			m_flNextSecondaryAttack = m_flNextPrimaryAttack;
		}
	}
}

Now to make sure our code gets called we add ProcessAnimationEvents(); here:

void CBaseCombatWeapon::ItemPostFrame( void )
{
      //Add this underneath the pOwner accessor check.
      ProcessAnimationEvents();
}

BASECOMBATWEAPON_SHARED.H

Add these declarations under public at line 211 below bool IsViewModelSequenceFinished( void ); // Returns if the viewmodel's current animation is finished:

Note.pngNote:We make ProcessAnimationEvents virtual so that we can override it in classes which inherit this class.
	virtual void            ProcessAnimationEvents(void);
	bool                    m_bWeaponIsLowered;

Finish

Now your weapon should be lowered when you hit the sprint button and you will not be able to shoot until you release the button!

Finished Product
Wikipedia - Letter.png
This article has not been added to any content Wikipedia icon categories. Please help out by Wikipedia icon adding categories.
January 2024