Delayed Attacks
January 2024
You can help by adding links to this article from other relevant articles.
January 2024
Overview
This tutorial will show you how to set up the Delayed Attack function to your weapon whether it's melee or fire arms. This should work with both Multiplayer and Singleplayer mods.
Header
So add these new voids with in your weapon class.
void DelayedAttack( void ); void ItemPostFrame( void ); void PrimaryAttack( void ) { return;}
Now you may want to add these two in your weapon class.
private: bool m_bDelayedAttack; float m_flDelayedAttackTime;
If your Weapon doesn't have a constructor, add this after the DECLARE_CLASS.
CWeaponDelayed();
Weapon Script
Add two more headers within your weapon script if you haven't already. player and in_buttons.h will give you access to which buttons are being depressed by player
#include "player.h" #include "basecombatcharacter.h" #include "in_buttons.h"
Add the new variables with in your constructor.
CWeaponDelayed::CWeaponDelayed() { m_bDelayedAttack = false; m_flDelayedAttackTime = 0.0f; }
Next add the ItemPostFrame function if you don't already have one, if you do, just copy the content and place it with in your existing ItemPostFrame function.
void CWeaponDelayed::ItemPostFrame( void ) { CBasePlayer *pOwner = ToBasePlayer( GetOwner() ); if ( pOwner == NULL ) return; if ( (pOwner->m_afButtonPressed & IN_ATTACK) && !m_bDelayedAttack) { //Animation Comments //SendWeaponAnim( ACT_VM_HAULBACK ); //m_flDelayedAttackTime = gpGlobals->curtime + SequenceDuration() + 1.0f; m_flDelayedAttackTime = gpGlobals->curtime + 1.0f; m_bDelayedAttack = true; } DelayedAttack(); BaseClass::ItemPostFrame(); }
The rest should be easy to follow, all you have to do is place your primary fire code with in it.
void CWeaponDelayed::DelayedAttack( void ) { if (m_bDelayedAttack && gpGlobals->curtime > m_flDelayedAttackTime) { //Replace this Comment with Weapon Attack Function BaseClass::PrimaryAttack(); m_bDelayedAttack = false; } }