ClientCommand: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
Biohazard 90 (talk | contribs) (better example) |
||
Line 1: | Line 1: | ||
ClientCommand() is | 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 his command was supposedly invalid. | ||
The client uses the function ''engine->ServerCmd()'' to trigger ClientCommand() on the server and can even pass information along in the string optionally: | |||
<source lang=cpp> | <source lang=cpp> | ||
void SelectClass( int iClassIndex ) // a function on the client | |||
{ | { | ||
engine->ServerCmd( VarArgs( "selectclass %i", iClassIndex ) ); // sent to the server | |||
} | } | ||
</source> | </source> | ||
The | 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 ) | |||
{ | { | ||
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; | |||
} | } | ||
[...] | |||
} | } | ||
</source> | </source> | ||
[[Category:Programming]] | [[Category:Programming]] |
Revision as of 16:17, 16 April 2011
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 pass information along in the string optionally:
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;
}
[...]
}