ServerCmd()

From Valve Developer Community
Jump to navigation Jump to search

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 ClientCommand() as a CCommand (i.e. a string array where 0 is the command itself and 1+ are parameters).

Tip.pngTip:You can also trigger a ConCommand if the client's argument matches its name.
Warning.pngWarning:Since ServerCmd() passes a full string value, it is NOT suitable for commands that will be sent frequently. You might get away with it in single-player, but in a multi-player mod you should transmit frequent commands by modifying the Usercmd.

Example

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

See also