Delayed Attacks: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 14: Line 14:
bool  m_bDelayedAttack;
bool  m_bDelayedAttack;
float m_flDelayedAttackTime;
float m_flDelayedAttackTime;
</pre>
If your Weapon doesn't have a constructor, add this after the '''DECLARE_CLASS'''.
<pre>
WeaponClassBlah();
</pre>
</pre>


== Weapon Script ==
== Weapon Script ==
Add two more headers within your weapon script if you haven't already.
<pre>
#include "player.h"
#include "basecombatcharacter.h"
#include "in_buttons.h"
</pre>
Add the new variables with in your constructor.
Add the new variables with in your constructor.
<pre>
<pre>

Revision as of 19:49, 15 January 2009

Overview

If you were wandering how to make one of your weapons have a delayed attack this may help you out. 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 this new void with in your weapon class.

Note.pngNote:If you have an ItemPostFrame function already, do not add the ItemPostFrame void.
void	DelayedAttack( void );
void	ItemPostFrame( void );

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.

	WeaponClassBlah();

Weapon Script

Add two more headers within your weapon script if you haven't already.

#include "player.h"
#include "basecombatcharacter.h"
#include "in_buttons.h"

Add the new variables with in your constructor.

WeaponClassBlah::WeaponClassBlah()
{
        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.

Note.pngNote:If you are planning to add a preparing animation, you may uncomment the animation comments and comment out the other m_flDelayedAttackTime
void WeaponClassBlah::ItemPostFrame( void )
{
	CBasePlayer *pOwner = ToBasePlayer( GetOwner() );
	
	if ( pOwner == NULL )
		return;

	if ( (pOwner->m_nButtons & 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.

Note.pngNote:Though if you are doing a melee weapon, do not replace anything and leave as is.
void WeaponClassBlah::DelayedAttack( void )
{
         if (m_bDelayedAttack && gpGlobals->curtime > m_flDelayedAttackTime)
         {
		//Replace this Comment with Weapon Attack Function
		BaseClass::PrimaryAttack();
		m_bDelayedAttack = false;
         }
}
Note.pngNote:Remember to replace "WeaponClassBlah" to your weapon class otherwise it won't compile.