Dropping empty magazines (GoldSrc): Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
m (Code snippet correction, code readability and spaces instead of tabs for weapon script section)
m (for weapons featured in various Valve games)
 
(4 intermediate revisions by 2 users not shown)
Line 3: Line 3:
{{note|<code>...</code> amidst the code means that there are other, unrelated lines there.}}
{{note|<code>...</code> amidst the code means that there are other, unrelated lines there.}}


== Ejecting the clip ==
= Ejecting the clip =


To do this in [[Half-Life]] (The Specialists apparently did something similar for their clip dropping), we will use the <code>EjectBrass</code> function but with the clip model as the model instead of a bullet. <code>EjectBrass</code> in [[Source]] is a little different, taking a model index as the argument. For this purpose, we have to create our own function.
To do this in [[Half-Life]] (The Specialists apparently did something similar for their clip dropping), we will use the <code>EjectBrass</code> function but with the clip model as the model instead of a bullet. <code>EjectBrass</code> in [[Source]] is a little different, taking a model index as the argument. For this purpose, we have to create our own function.


Add the following to <code>c_te_legacytempents.cpp</code>.
Add the following to '''c_te_legacytempents.cpp''':
 
<source lang=cpp>
<source lang=cpp>
void CTempEnts::EjectClip( const Vector &pos1, const QAngle &angles, const QAngle &gunAngles, model_t *pModel )
void CTempEnts::EjectClip( const Vector &pos1, const QAngle &angles, const QAngle &gunAngles, model_t *pModel )
Line 15: Line 14:
return;
return;


C_LocalTempEntity *pTemp = TempEntAlloc( pos1, ( model_t * ) pModel );
C_LocalTempEntity *pTemp = TempEntAlloc( pos1, (model_t *)pModel );


if ( pTemp == NULL )
if ( pTemp == NULL )
Line 24: Line 23:
pTemp->flags |= ( FTENT_COLLIDEWORLD | FTENT_FADEOUT | FTENT_GRAVITY | FTENT_ROTATE );
pTemp->flags |= ( FTENT_COLLIDEWORLD | FTENT_FADEOUT | FTENT_GRAVITY | FTENT_ROTATE );


pTemp->m_vecTempEntAngVelocity[0] = random->RandomFloat(-1024,1024);
pTemp->m_vecTempEntAngVelocity[0] = random->RandomFloat( -1024, 1024 );
pTemp->m_vecTempEntAngVelocity[1] = random->RandomFloat(-1024,1024);
pTemp->m_vecTempEntAngVelocity[1] = random->RandomFloat( -1024, 1024 );
pTemp->m_vecTempEntAngVelocity[2] = random->RandomFloat(-1024,1024);
pTemp->m_vecTempEntAngVelocity[2] = random->RandomFloat( -1024, 1024 );


//Face forward
// Face forward
pTemp->SetAbsAngles( gunAngles );
pTemp->SetAbsAngles( gunAngles );


pTemp->SetRenderMode( kRenderNormal );
pTemp->SetRenderMode( kRenderNormal );
pTemp->tempent_renderamt = 255; // Set this for fadeout
pTemp->tempent_renderamt = 255; // Set this for fadeout


Vector dir;
Vector dir;


AngleVectors( angles, &dir );
AngleVectors( angles, &dir );
Line 40: Line 39:
dir *= random->RandomFloat( 150.0f, 200.0f );
dir *= random->RandomFloat( 150.0f, 200.0f );


pTemp->SetVelocity( Vector(dir[0] + random->RandomFloat(-64,64),
pTemp->SetVelocity( Vector(dir[0] + random->RandomFloat( -64, 64 ),
dir[1] + random->RandomFloat(-64,64),
dir[1] + random->RandomFloat( -64, 64 ),
dir[2] + random->RandomFloat(  0,64) ) );
dir[2] + random->RandomFloat(  0,64 ) ) );


pTemp->die = gpGlobals->curtime + 20;
pTemp->die = gpGlobals->curtime + 20;
Line 48: Line 47:
</source>
</source>


Make sure you add the prototype to the <code>CTempEnts</code> class, found in <code>c_te_legacytempents.h</code>.
Make sure you add the prototype to the <code>CTempEnts</code> class, found in '''c_te_legacytempents.h''':
 
<source lang=cpp>
<source lang=cpp>
class CTempEnts : public ITempEnts
class CTempEnts : public ITempEnts
Line 59: Line 57:
</source>
</source>


Then, add it to <code>ITempEnts</code>, found in the same file.
Then, add it to <code>ITempEnts</code>, found in the same file:
 
<source lang=cpp>
<source lang=cpp>
class ITempEnts
class ITempEnts
Line 70: Line 67:
</source>
</source>


== Reloading ==
= Reloading =
 
Now, this will have to be added to one of your guns. Find one that will suit the effect, such as a pistol. In this tutorial, we will use the Falcon 2 pistol from [[User:Draco|Draco]]'s mod.


Now, this will have to be added to one of your guns. Find one that will suit the effect, such as a pistol. In this tutorial, we will use the Falcon 2 pistol from [[User:Draco|Draco]]'s mod:
<source lang=cpp>
<source lang=cpp>
bool CWeaponFalcon2::Reload()
bool CWeaponFalcon2::Reload()
Line 90: Line 86:
That will spawn a clip model that bounces, collides, falls and stays alive for 20 seconds.  
That will spawn a clip model that bounces, collides, falls and stays alive for 20 seconds.  


== Loading the model with the weapon script ==
= Loading the model with the weapon script =


Another thing Source has over HL1 is the weapon script parser. We shall add another token to the scripts that you can use to set a model for <code>EjectClip</code>.
Another thing Source has over HL1 is the weapon script parser. We shall add another token to the scripts that you can use to set a model for <code>EjectClip</code>.


Find the parser files. In the blank sdk it is called <code>sdk_weapon_parse</code>. Open <code>sdk_weapon_parse.h</code> (or equivalent for your sdk) and add the following to <code>CSDKWeaponInfo</code>:
Find the parser files. In the blank SDK it is called '''sdk_weapon_parse.h/cpp'''. Open '''sdk_weapon_parse.h''' (or equivalent for your SDK) and add the following to <code>CSDKWeaponInfo</code>:
 
<source lang=cpp>
<source lang=cpp>
class CSDKWeaponInfo : public FileWeaponInfo_t
class CSDKWeaponInfo : public FileWeaponInfo_t
{
{
...
...
char szClipModel[MAX_WEAPON_STRING]; // Clip model of this weapon
char szClipModel[MAX_WEAPON_STRING]; // Clip model of this weapon
...
...
};
};
</source>
</source>


Then, open <code>sdk_weapon_parse.cpp</code> and add this:
Then, open '''sdk_weapon_parse.cpp''' and add this:
 
<source lang=cpp>
<source lang=cpp>
void CSDKWeaponInfo::Parse( KeyValues *pKeyValuesData, const char *szWeaponName )
void CSDKWeaponInfo::Parse( KeyValues *pKeyValuesData, const char *szWeaponName )
Line 117: Line 111:


Now you can simply find the model path in the code by accessing <code>szClipModel</code>, like so:
Now you can simply find the model path in the code by accessing <code>szClipModel</code>, like so:
 
<source lang=cpp>
const CSDKWeaponInfo &pWeaponInfo = GetSDKWpnData();
const CSDKWeaponInfo &pWeaponInfo = GetSDKWpnData();
model_t *pModel = (model_t *)engine->LoadModel( pWeaponInfo.szClipModel );
model_t *pModel = (model_t *)engine->LoadModel( pWeaponInfo.szClipModel );
</source>


It is easy to make new tokens for anything in your weapon script.
It is easy to make new tokens for anything in your weapon script.


Now let's add that to the script itself. Find your weapons script file. In this tutorial, it's <code>PerfectDark/scripts/weapon_falcon2.cpp</code>.
Now let's add that to the script itself. Find your weapons script file. In this tutorial, it's '''PerfectDark/scripts/weapon_falcon2.txt''':
 
<source lang=ini>
<source lang=ini>
// Weapon data is loaded by both the Game and Client DLLs.
// Weapon data is loaded by both the Game and Client DLLs.
Line 135: Line 129:


If you have any problems you can PM the original author at [[User_talk:Draco|his talk page]] or you can attempt to contact him on IRC at <code>#ausbg</code> on GameSurge.
If you have any problems you can PM the original author at [[User_talk:Draco|his talk page]] or you can attempt to contact him on IRC at <code>#ausbg</code> on GameSurge.
{{DISPLAYTITLE:Dropping empty magazines}}
[[Category:GoldSrc]]


[[Category:Tutorials]]
[[Category:Tutorials]]
[[Category:Weapons programming]]

Latest revision as of 04:05, 11 April 2023

A small visual effect can add a lot of polish to your game. One such effect is actually dropping your empty clips/magazines when you reload a suitable gun. If you time it right, players will see other players drop the empty clip when the player animation does so. For this tutorial, it is recommended that you use a blank multiplayer SDK, for it will yield greater results in the long run.

Note.pngNote:... amidst the code means that there are other, unrelated lines there.

Ejecting the clip

To do this in Half-Life (The Specialists apparently did something similar for their clip dropping), we will use the EjectBrass function but with the clip model as the model instead of a bullet. EjectBrass in Source is a little different, taking a model index as the argument. For this purpose, we have to create our own function.

Add the following to c_te_legacytempents.cpp:

void CTempEnts::EjectClip( const Vector &pos1, const QAngle &angles, const QAngle &gunAngles, model_t *pModel )
{
	if ( pModel == NULL )
		return;

	C_LocalTempEntity *pTemp = TempEntAlloc( pos1, (model_t *)pModel );

	if ( pTemp == NULL )
		return;

	pTemp->m_nBody	= 0;

	pTemp->flags |= ( FTENT_COLLIDEWORLD | FTENT_FADEOUT | FTENT_GRAVITY | FTENT_ROTATE );

	pTemp->m_vecTempEntAngVelocity[0] = random->RandomFloat( -1024, 1024 );
	pTemp->m_vecTempEntAngVelocity[1] = random->RandomFloat( -1024, 1024 );
	pTemp->m_vecTempEntAngVelocity[2] = random->RandomFloat( -1024, 1024 );

	// Face forward
	pTemp->SetAbsAngles( gunAngles );

	pTemp->SetRenderMode( kRenderNormal );
	pTemp->tempent_renderamt = 255; // Set this for fadeout

	Vector dir;

	AngleVectors( angles, &dir );

	dir *= random->RandomFloat( 150.0f, 200.0f );

	pTemp->SetVelocity( Vector(dir[0] + random->RandomFloat( -64, 64 ),
						dir[1] + random->RandomFloat( -64, 64 ),
						dir[2] + random->RandomFloat(  0,64 ) ) );

	pTemp->die = gpGlobals->curtime + 20;
}

Make sure you add the prototype to the CTempEnts class, found in c_te_legacytempents.h:

class CTempEnts : public ITempEnts
{
	...
	virtual void EjectClip( const Vector &pos1, const QAngle &angles, const QAngle &gunAngles, model_t *pModel );
	...
};

Then, add it to ITempEnts, found in the same file:

class ITempEnts
{
	...
	virtual void EjectClip( const Vector &pos1, const QAngle &angles, const QAngle &gunAngles, model_t *pModel ) = 0;
	...
};

Reloading

Now, this will have to be added to one of your guns. Find one that will suit the effect, such as a pistol. In this tutorial, we will use the Falcon 2 pistol from Draco's mod:

bool CWeaponFalcon2::Reload()
{
	...
#ifdef CLIENT_DLL
	model_t *pModel = (model_t *)engine->LoadModel( pWeaponInfo.szClipModel );
	tempents->EjectClip( pPlayer->GetLocalOrigin(), pPlayer->GetLocalAngles(), pPlayer->GetAbsAngles(), pModel );
#endif
	...
}

As you can see this is all on the client. First you load a model into a model info pointer for use as the clip model. The weapon scripts are used to specify the path to model, as is demonstrated shortly.

That will spawn a clip model that bounces, collides, falls and stays alive for 20 seconds.

Loading the model with the weapon script

Another thing Source has over HL1 is the weapon script parser. We shall add another token to the scripts that you can use to set a model for EjectClip.

Find the parser files. In the blank SDK it is called sdk_weapon_parse.h/cpp. Open sdk_weapon_parse.h (or equivalent for your SDK) and add the following to CSDKWeaponInfo:

class CSDKWeaponInfo : public FileWeaponInfo_t
{
	...
	char	szClipModel[MAX_WEAPON_STRING]; // Clip model of this weapon
	...
};

Then, open sdk_weapon_parse.cpp and add this:

void CSDKWeaponInfo::Parse( KeyValues *pKeyValuesData, const char *szWeaponName )
{
	...
	V_strncpy( szClipModel, pKeyValuesData->GetString( "clipmodel" ), MAX_WEAPON_STRING );
	...
}

Now you can simply find the model path in the code by accessing szClipModel, like so:

const CSDKWeaponInfo &pWeaponInfo = GetSDKWpnData();
model_t *pModel = (model_t *)engine->LoadModel( pWeaponInfo.szClipModel );

It is easy to make new tokens for anything in your weapon script.

Now let's add that to the script itself. Find your weapons script file. In this tutorial, it's PerfectDark/scripts/weapon_falcon2.txt:

// Weapon data is loaded by both the Game and Client DLLs.
"printname"                     "#PD_FALCON2"
"viewmodel"                     "models/v_falcon2.mdl"
"playermodel"                   "models/w_falcon2.mdl"
"clipmodel"                     "models/w_falconclip.mdl"
"PlayerAnimationExtension"      "Pistol"

If you have any problems you can PM the original author at his talk page or you can attempt to contact him on IRC at #ausbg on GameSurge.