ClientCommand

From Valve Developer Community
Jump to: navigation, search

The function ClientCommand() can be found in the player classes of the server project and is executed when it is not possible to resolve an incoming command string from a client in any other way (for example through a ConCommand). Returning the value 'false' will cause the server to tell the respective client that his command was supposedly invalid.


The client uses the function engine->ServerCmd() to trigger ClientCommand() on the server and can even optionally pass information along in the string:

void SelectClass( int iClassIndex ) 						// a function on the client
{
	engine->ServerCmd( VarArgs( "selectclass %i", iClassIndex ) );		// sent to the server
}

The following snippet of code shows how the command selectclass is handled by the server:

bool CSDKPlayer::ClientCommand( const CCommand &args )
{
	if ( FStrEq( pcmd, "selectclass" ) ) 					// this is our command name
	{
		if ( args.ArgC() < 2 ) 						// make sure that an argument was passed along
		{
			Warning( "bad selectclass cmd\n" );
			return true;
		}

		int iIndex = atoi( args[1] );					// convert our argument back to an integer

		HandleClassSelection( iIndex );
		return true;
	}

	[...]
}