Delayed Attacks: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
 
(9 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{Multiple issues|
{{Dead end|date=January 2024}}
{{Orphan|date=January 2024}}
}}
== Overview ==
== 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.
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 ==
== Header ==
Line 19: Line 24:
If your Weapon doesn't have a constructor, add this after the '''DECLARE_CLASS'''.
If your Weapon doesn't have a constructor, add this after the '''DECLARE_CLASS'''.
<pre>
<pre>
WeaponClassBlah();
CWeaponDelayed();
</pre>
</pre>


== Weapon Script ==
== Weapon Script ==
Add two more headers within your weapon script if you haven't already.
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
<pre>
<pre>
#include "player.h"
#include "player.h"
Line 32: Line 37:
Add the new variables with in your constructor.
Add the new variables with in your constructor.
<pre>
<pre>
WeaponClassBlah::WeaponClassBlah()
CWeaponDelayed::CWeaponDelayed()
{
{
         m_bDelayedAttack = false;
         m_bDelayedAttack = false;
Line 38: Line 43:
}
}
</pre>
</pre>
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.  
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|If you are planning to add a preparing animation, you may uncomment the animation comments and comment out the other '''m_flDelayedAttackTime'''}}
{{note|If you are planning to add a preparing animation, you may uncomment the animation comments and comment out the other '''m_flDelayedAttackTime'''}}
<pre>
<pre>
void WeaponClassBlah::ItemPostFrame( void )
void CWeaponDelayed::ItemPostFrame( void )
{
{
CBasePlayer *pOwner = ToBasePlayer( GetOwner() );
CBasePlayer *pOwner = ToBasePlayer( GetOwner() );
Line 49: Line 54:
return;
return;


if ( (pOwner->m_nButtons & IN_ATTACK) && !m_bDelayedAttack)
if ( (pOwner->m_afButtonPressed & IN_ATTACK) && !m_bDelayedAttack)
{
{
//Animation Comments
//Animation Comments
Line 56: Line 61:
m_flDelayedAttackTime = gpGlobals->curtime + 1.0f;
m_flDelayedAttackTime = gpGlobals->curtime + 1.0f;
m_bDelayedAttack = true;
m_bDelayedAttack = true;
DelayedAttack();
}
}
DelayedAttack();
BaseClass::ItemPostFrame();
BaseClass::ItemPostFrame();
}
}
Line 64: Line 69:
{{Note|Though if you are doing a melee weapon, do not replace anything and leave as is.}}
{{Note|Though if you are doing a melee weapon, do not replace anything and leave as is.}}
<pre>
<pre>
void WeaponClassBlah::DelayedAttack( void )
void CWeaponDelayed::DelayedAttack( void )
{
{
         if (m_bDelayedAttack && gpGlobals->curtime > m_flDelayedAttackTime)
         if (m_bDelayedAttack && gpGlobals->curtime > m_flDelayedAttackTime)
Line 74: Line 79:
}
}
</pre>
</pre>
{{Note|Remember to replace '''WeaponClassBlah''' to your weapon class otherwise it won't compile.}}
{{Note|Remember to replace '''CWeaponDelayed''' to your weapon class otherwise it won't compile.}}
 
[[Category:Weapons programming]]
[[Category:Programming]]

Latest revision as of 18:59, 12 November 2024

Wikipedia - Letter.png
This article has multiple issues. Please help improve it or discuss these issues on the talk page. (Learn how and when to remove these template messages)
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

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.

Note.pngNote:If you have an ItemPostFrame function already, do not add the ItemPostFrame void. Also if you have a PrimaryAttack void already, then replace with the one below.
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.

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 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.

Note.pngNote:Though if you are doing a melee weapon, do not replace anything and leave as is.
void CWeaponDelayed::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 CWeaponDelayed to your weapon class otherwise it won't compile.