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)
Line 9: Line 9:
Add the following to <code>c_te_legacytempents.cpp</code>.
Add the following to <code>c_te_legacytempents.cpp</code>.


<source lang=cpp>void CTempEnts::EjectClip( const Vector &pos1, const QAngle &angles, const QAngle &gunAngles, model_t *pModel )
<source lang=cpp>
void CTempEnts::EjectClip( const Vector &pos1, const QAngle &angles, const QAngle &gunAngles, model_t *pModel )
{
{
if ( pModel == NULL )
if ( pModel == NULL )
Line 44: Line 45:


pTemp->die = gpGlobals->curtime + 20;
pTemp->die = gpGlobals->curtime + 20;
}</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 <code>c_te_legacytempents.h</code>.


<source lang=cpp>class CTempEnts : public ITempEnts
<source lang=cpp>
class CTempEnts : public ITempEnts
{
{
...
...
Line 54: Line 57:
...
...
};
};
</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>
class ITempEnts
class ITempEnts
{
{
Line 62: Line 67:
virtual void EjectClip( const Vector &pos1, const QAngle &angles, const QAngle &gunAngles, model_t *pModel ) = 0;
virtual void EjectClip( const Vector &pos1, const QAngle &angles, const QAngle &gunAngles, model_t *pModel ) = 0;
...
...
};</source>
};
</source>


== Reloading ==
== Reloading ==
Line 68: Line 74:
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>bool CWeaponFalcon2::Reload()
<source lang=cpp>
bool CWeaponFalcon2::Reload()
{
{
...
...
Line 76: Line 83:
#endif
#endif
...
...
}</source>
}
</source>


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.
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.
Line 88: Line 96:
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 <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>:


<source lang=cpp>class CSDKWeaponInfo : public FileWeaponInfo_t
<source lang=cpp>
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 <code>sdk_weapon_parse.cpp</code> and add this:


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


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:
Line 113: Line 125:
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 <code>PerfectDark/scripts/weapon_falcon2.cpp</code>.


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


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.

Revision as of 13:39, 10 February 2020

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

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