ClientCommand: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
(Created page with 'ClientCommand() is how the server receives client commands sent using ServerCmd. Using the below snippet of code as an example is how the server receives commands: <source lan…')
 
m (→‎top: clean up, added orphan tag)
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
ClientCommand() is how the server receives client commands sent using ServerCmd.
{{Orphan|date=January 2024}}


Using the below snippet of code as an example is how the server receives commands:
The [[function]] ClientCommand() can be found in the [[CBasePlayer|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 their 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:
 
<source lang=cpp>
void SelectClass( int iClassIndex ) // a function on the client
{
engine->ServerCmd( VarArgs( "selectclass %i", iClassIndex ) ); // sent to the server
}
</source>
 
The following snippet of code shows how the command ''selectclass'' is handled by the server:


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


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


HandleClassSelection( iIndex );
return true;
return true;
}
}


...
[...]
}
}
</source>
</source>


The command "menuopen" is called by engine->ServerCmd( "menuopen" ) of CSDKClassMenu::SetVisible (ie. this code is executed by the '''Client''')
[[Category:Programming]]
 
<source lang=cpp>
void CSDKClassMenu::SetVisible( bool state )
{
BaseClass::SetVisible( state );
 
if ( state )
{
engine->ServerCmd( "menuopen" ); // to the server
engine->ClientCmd( "_cl_classmenuopen 1" ); // for other panels
}
else
{
engine->ServerCmd( "menuclosed" );
engine->ClientCmd( "_cl_classmenuopen 0" );
}
}
</source>

Latest revision as of 22:38, 21 January 2024

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 their 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;
	}

	[...]
}