• Register
Forum Thread
  Posts  
Source SDK 2013 Singleplayer C++ code: Secondary ammo always displays in red in HUD, but I meant it to do that only if secondary attack is not ready: (Forums : Coding & Scripting : Source SDK 2013 Singleplayer C++ code: Secondary ammo always displays in red in HUD, but I meant it to do that only if secondary attack is not ready:) Post Reply
Thread Options
Jun 2 2021 Anchor

Secondary ammo always displays in red in HUD, thought it should only if secondary attack is not readyI made a custom grenade launcher for my weapon_smg1 that:
1) fires if bool SMG1_GL_Loaded is true

2) reloads if bool SMG1_GL_Loaded is false and player has ammo for it.

I'd like to display the secondary ammo section in red if the grenade launcher is not ready to fire (i.e.bool SMG1_GL_Loadedisfalseor there's no ammo) and in regular HUD color in other cases, but for me it's always red.

Here's my src/game/client/hl2/hud.ammo.cpp

//-----------------------------------------------------------------------------
// Purpose: Displays the secondary ammunition level
//-----------------------------------------------------------------------------
class CHudSecondaryAmmo : public CHudNumericDisplay, public CHudElement
{
	DECLARE_CLASS_SIMPLE( CHudSecondaryAmmo, CHudNumericDisplay );

public:
	CHudSecondaryAmmo( const char *pElementName ) : BaseClass( NULL, "HudAmmoSecondary" ), CHudElement( pElementName )
	{
		m_iAmmo = -1;
		m_iSMG1_GL_Loaded = false;
		m_iAR1M1_GL_Loaded = false;

		SetHiddenBits( HIDEHUD_HEALTH | HIDEHUD_WEAPONSELECTION | HIDEHUD_PLAYERDEAD | HIDEHUD_NEEDSUIT );
	}

	void Init( void )
	{
		m_iSMG1_GL_Loaded = false;
		m_iAR1M1_GL_Loaded = false;
#ifndef HL2MP
		wchar_t *tempString = g_pVGuiLocalize->Find("#Valve_Hud_AMMO_ALT");
		if (tempString)
		{
			SetLabelText(tempString);
		}
		else
		{
			SetLabelText(L"ALT");
		}
#endif // HL2MP
	}

	void VidInit( void )
	{
	}

	void SetAmmo(int ammo, bool playAnimation, bool SMG1_GL_Loaded)
	{
		C_BaseCombatWeapon *wpn = GetActiveWeapon();

		const char* ActiveWeaponName = wpn->GetName();

		if (ammo != m_iAmmo || m_iSMG1_GL_Loaded != SMG1_GL_Loaded) // some activity in secondary attacks
		{
			if (ammo == 0)
			{
				g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("AmmoSecondaryEmpty");
			}

			if (ammo < m_iAmmo && strcmp(ActiveWeaponName, "weapon_smg1") == 0)
			{
				// ammo has decreased and current weapon is SMG1 means that its grenade launcher is unloaded
				g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("AmmoSecondaryDecreasedUnloaded");
			}
			else if (ammo < m_iAmmo)
			{
				// ammo has decreased
				g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("AmmoSecondaryDecreased");
			}
			else if (SMG1_GL_Loaded == false && strcmp(ActiveWeaponName, "weapon_smg1") == 0)
			{
				// ammunition has increased but active weapon is SMG1 and its grenade launcher is unloaded
				g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("AmmoSecondaryIncreasedUnloaded");
			}
			else
			{
				// ammunition has increased or grenade launcher has been loaded
				g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("AmmoSecondaryIncreased");
			}

		}

		if (m_iAmmo != ammo)
		{
			m_iAmmo = ammo;
		}

		if (m_iSMG1_GL_Loaded != SMG1_GL_Loaded)
		{
			m_iSMG1_GL_Loaded = SMG1_GL_Loaded;
		}

		SetDisplayValue(ammo);
	}

	void Reset()
	{
		// hud reset, update ammo state
		BaseClass::Reset();
		m_iAmmo = 0;
		m_hCurrentActiveWeapon = NULL;
		SetAlpha( 0 );
		UpdateAmmoState();
	}

	virtual void Paint( void )
	{
		BaseClass::Paint();

#ifndef HL2MP
		if ( m_iconSecondaryAmmo )
		{
			int nLabelHeight;
			int nLabelWidth;
			surface()->GetTextSize( m_hTextFont, m_LabelText, nLabelWidth, nLabelHeight );

			// Figure out where we're going to put this
			int x = text_xpos + ( nLabelWidth - m_iconSecondaryAmmo->Width() ) / 2;
			int y = text_ypos - ( nLabelHeight + ( m_iconSecondaryAmmo->Height() / 2 ) );

			/*C_BaseCombatWeapon *wpn = GetActiveWeapon();

			const char* ActiveWeaponName = wpn->GetName();

			if (strcmp (ActiveWeaponName,"weapon_smg1") == 0 && !SMG1_GL_Loaded)
				m_iconSecondaryAmmo->DrawSelf( x, y, Color (255, 0, 0, 255) );
			else
			*/
				m_iconSecondaryAmmo->DrawSelf(x, y, GetFgColor());
		}
#endif // HL2MP
	}

protected:

	virtual void OnThink()
	{
		// set whether or not the panel draws based on if we have a weapon that supports secondary ammo
		C_BaseCombatWeapon *wpn = GetActiveWeapon();
		C_BasePlayer *player = C_BasePlayer::GetLocalPlayer();
		IClientVehicle *pVehicle = player ? player->GetVehicle() : NULL;
		if (!wpn || !player || pVehicle)
		{
			m_hCurrentActiveWeapon = NULL;
			SetPaintEnabled(false);
			SetPaintBackgroundEnabled(false);
			return;
		}
		else
		{
			SetPaintEnabled(true);
			SetPaintBackgroundEnabled(true);
		}

		UpdateAmmoState();
	}

	void UpdateAmmoState()
	{
		C_BaseCombatWeapon *wpn = GetActiveWeapon();
		C_BasePlayer *player = C_BasePlayer::GetLocalPlayer();

		if (player && wpn && wpn->UsesSecondaryAmmo())
		{
			//SetAmmo(player->GetAmmoCount(wpn->GetSecondaryAmmoType()), true, SMG1_GL_Loaded, AR1M1_GL_Loaded);
			SetAmmo(player->GetAmmoCount(wpn->GetSecondaryAmmoType()), true, SMG1_GL_Loaded);
		}

		if ( m_hCurrentActiveWeapon != wpn )
		{
			if ( wpn->UsesSecondaryAmmo() )
			{
				// we've changed to a weapon that uses secondary ammo
				g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("WeaponUsesSecondaryAmmo");
			}
			else 
			{
				// we've changed away from a weapon that uses secondary ammo
				g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("WeaponDoesNotUseSecondaryAmmo");
			}
			m_hCurrentActiveWeapon = wpn;

			// Get the icon we should be displaying
			m_iconSecondaryAmmo = gWR.GetAmmoIconFromWeapon( m_hCurrentActiveWeapon->GetSecondaryAmmoType() );

		}
	}
	
private:
	CHandle< C_BaseCombatWeapon > m_hCurrentActiveWeapon;
	CHudTexture *m_iconSecondaryAmmo;
	int		m_iAmmo;
	bool	m_iSMG1_GL_Loaded;
	bool	m_iAR1M1_GL_Loaded;
};

DECLARE_HUDELEMENT( CHudSecondaryAmmo );

and scripts/hudanimations.txt

event AmmoSecondaryIncreased
{
	StopEvent AmmoSecondaryDecreased	0.0
	StopEvent AmmoSecondaryDecreasedUnloaded	0.0
	StopEvent AmmoSecondaryIncreasedUnloaded	0.0
	StopEvent AmmoSecondaryEmpty	0.0
	
	Animate HudAmmoSecondary		FgColor	"BrightFg"		Linear	0.0	0.15
	Animate HudAmmoSecondary		FgColor	"FgColor"		Deaccel	0.15	1.5
	Animate HudAmmoSecondary		Blur		"5"			Linear	0.0	0.0 
	Animate HudAmmoSecondary		Blur		"0"			Accel		0.01	1.5 	
}

event AmmoSecondaryDecreased
{
	StopEvent AmmoSecondaryIncreased	0.0
	
	Animate HudAmmoSecondary		Blur		"7"			Linear	0.0	0.0
	Animate HudAmmoSecondary		Blur		"0"			Deaccel	0.1	1.5
	
	Animate HudAmmoSecondary		TextColor	"BrightFg"		Linear	0.0	0.1
	Animate HudAmmoSecondary		TextColor	"FgColor"		Deaccel	0.1	0.75
}

event AmmoSecondaryIncreasedUnloaded
{
	StopEvent AmmoSecondaryEmpty	0.0

	Animate HudAmmoSecondary		FgColor		"BrightDamagedFg"	Linear	0.0	0.2
	Animate HudAmmoSecondary		FgColor		"DamagedFg"		Accel		0.2	1.2
	Animate HudAmmoSecondary		Blur		"7"			Linear	0.0	0.0
	Animate HudAmmoSecondary		Blur		"0"			Deaccel	0.1	1.5	
}

event AmmoSecondaryDecreasedUnloaded
{
	StopEvent AmmoSecondaryIncreased	0.0
	
	Animate HudAmmoSecondary		FgColor		"BrightDamagedFg"	Linear	0.0	0.2
	Animate HudAmmoSecondary		FgColor		"DamagedFg"		Accel		0.2	1.2
	Animate HudAmmoSecondary		Blur		"7"			Linear	0.0	0.0
	Animate HudAmmoSecondary		Blur		"0"			Deaccel	0.1	1.5
}

event AmmoSecondaryEmpty
{
	Animate HudAmmoSecondary		FgColor		"BrightDamagedFg"	Linear	0.0	0.2
	Animate HudAmmoSecondary		FgColor		"DamagedFg"		Accel		0.2	1.2
	Animate HudAmmoSecondary		Blur		"7"			Linear	0.0	0.0
	Animate HudAmmoSecondary		Blur		"0"			Deaccel	0.1	1.5

}

Thanks a lot in advance!

Edited by: HEVcrab

Jun 3 2021 Anchor

Who knows, maybe that part:

if (strcmp (ActiveWeaponName,"weapon_smg1") == 0 && !SMG1_GL_Loaded)
m_iconSecondaryAmmo->DrawSelf( x, y, Color (255, 0, 0, 255) );
else
*/
m_iconSecondaryAmmo->DrawSelf(x, y, GetFgColor());

-----------
255, 0, 0 is red color

and that else end with that comment "*/", which block second command.(delete that "*/")

********************
if (strcmp (ActiveWeaponName,"weapon_smg1") == 0 && !SMG1_GL_Loaded)
m_iconSecondaryAmmo->DrawSelf( x, y, Color (255, 0, 0, 255) );
else
m_iconSecondaryAmmo->DrawSelf( x, y, Color (0, 128, 0, 255) );

Jun 3 2021 Anchor
Bazlik_Commander wrote:

Who knows, maybe that part:

if (strcmp (ActiveWeaponName,"weapon_smg1") == 0 && !SMG1_GL_Loaded)
m_iconSecondaryAmmo->DrawSelf( x, y, Color (255, 0, 0, 255) );
else
*/
m_iconSecondaryAmmo->DrawSelf(x, y, GetFgColor());

-----------
255, 0, 0 is red color

and that else end with that comment "*/", which block second command.(delete that "*/")

********************
if (strcmp (ActiveWeaponName,"weapon_smg1") == 0 && !SMG1_GL_Loaded)
m_iconSecondaryAmmo->DrawSelf( x, y, Color (255, 0, 0, 255) );
else
m_iconSecondaryAmmo->DrawSelf( x, y, Color (0, 128, 0, 255) );

That is my old code I commented out as it didn't work properly either.

In-game I noticed that the HUD animations don't play on grenade launcher variable events without changing ammo amount, i.e. reloading and attempting to shoot with no ammo. That is strange...

The indication still doesn't work properly:

1) the secondary ammo widget stays red even when the grenade launcher is loaded, and

2) does not become red when trying to fire secondary attack without ammo.

Jun 3 2021 Anchor

To find that error, you need to simplify the code, try only with reload(unlimited grenades mod).

What happens if you totally cut line:

if (ammo != m_iAmmo || m_iSMG1_GL_Loaded != SMG1_GL_Loaded) // some activity in secondary attacks

(backup whole script and make new one with option reload only for that launcher).

Jun 5 2021 Anchor

Actually I tried to use a very lame way to share variables between client and server.

Made a proper implementation on the server side that saves the grenade launcher variables to savegames.

TODO: make the client access them.

Done! :)

Bazlik_Commander, thank you for the replies, though I figured it out.

Reply to thread
click to sign in and post

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.