ServerCmd(): Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
mNo edit summary
m (avoid C++ redir :-p)
Line 9: Line 9:
  bool CHL2_Player::ClientCommand( const CCommand &args )
  bool CHL2_Player::ClientCommand( const CCommand &args )
  {
  {
  if ( ![[Q_stricmp()|Q_stricmp]]( args[0], "MyCommand" ) )
  if ( ![[V_stricmp()|V_stricmp]]( args[0], "MyCommand" ) )
  {
  {
  [[Msg()|Msg]]( "MyCommand received! Value: %i\n", [[atoi()|atoi]](args[1]) );
  [[Msg()|Msg]]( "MyCommand received! Value: %i\n", [[atoi()|atoi]](args[1]) );

Revision as of 08:32, 4 April 2008

ServerCmd() is a quick and easy method for passing user input to the server available through cbase.h. It uploads an ASCII string, including with it information about which connected client sent it. The received strings are processed in CHL2_Player::ClientCommand() as a CCommand (i.e. a string array where 0 is the command itself and 1+ are parameters).

Warning.pngWarning:Since ServerCmd() passes a full string value, it is NOT suitable for commands that will be sent frequently. Single-player mods can probably get away with the overhead, but multiplayer mods must send with the Usercmd instead.

Example

client:
	engine->ServerCmd( VarArgs("MyCommand %i", m_iMyNumber) ); // No variables? Pass the command as a simple string.
server:
	bool CHL2_Player::ClientCommand( const CCommand &args )
	{
		if ( !V_stricmp( args[0], "MyCommand" ) )
		{
			Msg( "MyCommand received! Value: %i\n", atoi(args[1]) );
			return true;
		}
	}

See also