Talk:GetLocalPlayer

From Valve Developer Community
Jump to navigation Jump to search

This solution makes no sense. This will always return the player at index 1, and won't work properly on the client if the client isn't the first to connect to the server. Solokiller (talk) 21:23, 6 August 2016 (UTC)

Page Rework (6 August 2019)

Alright, so I've done a complete rework of this page since it was pretty much bullshit before. I assumed this page was mostly used by people fixing up singleplayer code (especially NPC/AI code) for multiplayer so I decided to make this a list of functions capable of replacing UTIL_GetLocalPlayer() for multiplayer purposes while still keeping in a "fix" for UTIL_GetLocalPlayer().

In case you need it, here's the solution that the old article proposed:

"Open util.cpp and find

CBasePlayer *UTIL_GetLocalPlayer( void )
{
	if ( gpGlobals->maxClients > 1 )
	{
		if ( developer.GetBool() )
		{
			Assert( !"UTIL_GetLocalPlayer" );
			
#ifdef	DEBUG
			Warning( "UTIL_GetLocalPlayer() called in multiplayer game.\n" );
#endif
		}

		return NULL;
	}

	return UTIL_PlayerByIndex( 1 );
}

and replace it for this

CBasePlayer *UTIL_GetLocalPlayer( void )
{
	if ( gpGlobals->maxClients > 1 )
	{
		for (int i = 1; i <= gpGlobals->maxClients; i++)
		{
			return UTIL_PlayerByIndex(i);
		}
	}

	return UTIL_PlayerByIndex( 1 );
}

"

Also, I know that the "Fix up UTIL_GetLocalPlayer" section is not very helpful but I'll leave it in for now. If you take issue with it, feel free to delete it.

Hit me up on my user talk page if you need to discuss anything with me regarding this article. RenateZwei (talk) 15:45, 6 August 2019 (UTC)