• Register
Forum Thread
  Posts  
Source SDK 2013 Singleplayer C++ code: How do I access variables of server's CBasePlayer class from the client? (Forums : Coding & Scripting : Source SDK 2013 Singleplayer C++ code: How do I access variables of server's CBasePlayer class from the client?) Locked
Thread Options
Jun 5 2021 Anchor

I implemented grenade launcher variables on the server side so that they're saved to savegames.
They are implemented in server class

CBasePlayer

TODO: make the client access them. How do I do that?

This doesn't build as client has its own class C_BasePlayer that's different from the server's.

 void SetAmmo(int ammo, CBasePlayer *player)
	{

		C_BaseCombatWeapon *wpn = GetActiveWeapon();

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

		if (((*player).m_SMG1_GL_action_failed && strcmp(ActiveWeaponName, "weapon_smg1") == 0) || ((*player).m_AR1M1_GL_action_failed && strcmp(ActiveWeaponName, "weapon_ar1m1") == 0)) //if the player picks up SMG1/AR1M1 with no secondary ammo, its widget is still default color, but paint it red when firing with no ammo
		{
			g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("AmmoSecondaryEmpty");
			if ((*player).m_SMG1_GL_action_failed)
				(*player).SetSMG1_GLAF_Value(false);
			if ((*player).m_AR1M1_GL_action_failed)
				(*player).SetAR1M1_GLAF_Value(false);
		}

		//if (strcmp(ActiveWeaponName, "weapon_smg1") != 0 && strcmp(ActiveWeaponName, "weapon_ar1m1") != 0) // these weapons handle secondary ammo HUD animations other way 
		//{

		if (ammo != m_iAmmo || m_iSMG1_GL_Loaded != (*player).m_SMG1_GL_Loaded || m_iAR1M1_GL_Loaded != (*player).m_AR1M1_GL_Loaded) // ammo amount changed or the player reloaded the grenade launcher means we call the animation
		{
			if (ammo == 0)
			{
				g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("AmmoSecondaryEmpty");
				//engine->ClientCmd("testhudanim AmmoSecondaryEmpty");
			}

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

		}

		//}

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

		if (m_iSMG1_GL_Loaded != (*player).m_SMG1_GL_Loaded)
		{
			m_iSMG1_GL_Loaded = (*player).m_SMG1_GL_Loaded;
		}

		if (m_iAR1M1_GL_Loaded != (*player).m_AR1M1_GL_Loaded)
		{
			m_iAR1M1_GL_Loaded = (*player).m_AR1M1_GL_Loaded;
		}

		SetDisplayValue(ammo);
	}

Solved, moved the variables to HL2_Player class.

Edited by: HEVcrab

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.