Lowering your weapon on sprint: Difference between revisions
Jump to navigation
Jump to search
Note:This has only been tested with the latest HL2MP OB engine code.
Note: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: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
Note:Download the Studio Compiler here [1] (only necessary if your weapon don't got the ACT_VM_IDLE_LOWERED animation!)
Note:In this Tutorial you will do this by coding only, so open up your basecombatweapon_shared.cpp and basecombatweapon_shared.h
Note:We make ProcessAnimationEvents virtual so that we can override it in classes which inherit this class.
No edit summary |
No edit summary |
||
Line 4: | Line 4: | ||
{{note|1=This has only been tested with the latest HL2MP OB engine code.}} | {{note|1=This has only been tested with the latest HL2MP OB engine code.}} | ||
{{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=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 | {{note|1=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'''}} | ||
{{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|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!)}} | ||
Line 18: | Line 18: | ||
void CBaseCombatWeapon::ItemPostFrame( void ) | void CBaseCombatWeapon::ItemPostFrame( void ) | ||
</source> | </source> | ||
Then copy and paste this | Then copy and paste this above it: | ||
<source lang=cpp> | <source lang=cpp> | ||
void CBaseCombatWeapon::ProcessAnimationEvents(void) | |||
{ | { | ||
CBasePlayer *pOwner = ToBasePlayer( GetOwner() ); | |||
if (!pOwner) | |||
return; | |||
if ( | if ( !m_bWeaponIsLowered && (pOwner->m_nButtons & IN_SPEED ) ) | ||
{ | { | ||
m_bWeaponIsLowered = true; | |||
SendWeaponAnim( ACT_VM_IDLE_LOWERED ); | SendWeaponAnim( ACT_VM_IDLE_LOWERED ); | ||
m_flNextPrimaryAttack = gpGlobals->curtime + GetViewModelSequenceDuration(); | |||
m_flNextSecondaryAttack = m_flNextPrimaryAttack; | |||
} | } | ||
else if ( m_bWeaponIsLowered && !(pOwner->m_nButtons & IN_SPEED ) ) | |||
else if ( | |||
{ | { | ||
m_bWeaponIsLowered = false; | |||
SendWeaponAnim( ACT_VM_IDLE ); | 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; | |||
} | |||
} | } | ||
} | } | ||
</source> | </source> | ||
Now to make sure our code gets called we add ProcessAnimationEvents(); here: | |||
<source lang=cpp> | <source lang=cpp> | ||
void CBaseCombatWeapon::ItemPostFrame( void ) | |||
{ | |||
//Add this underneath the pOwner accessor check. | |||
ProcessAnimationEvents(); | |||
} | |||
</source> | </source> | ||
== | ===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|1=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> | </source> | ||
==Finish== | |||
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! | |||
[[Category:Weapons programming]] {{DISPLAYTITLE:Lowering your weapon on sprint}} | [[Category:Weapons programming]] {{DISPLAYTITLE:Lowering your weapon on sprint}} | ||
[[Category:Tutorials]] | [[Category:Tutorials]] |
Revision as of 02:43, 2 May 2015
This article will deal with lowering your weapon while sprinting and disable the ability to shoot at the same time. This was based on a topic on the Steam Forums




The Code

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:

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 available to shoot either until you release the button!