ServerCmd(): Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
No edit summary
 
No edit summary
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
'''<code>ServerCmd()</code>''' is a quick and easy method for passing user input to the server, available through <code>[[cbase.h]]</code>. It uploads an [[Wikipedia:ASCII|ASCII]] string, including with it information about which connected client sent it. The received strings are processed in <code>[[CHL2_Player]]::'''ClientCommand()'''</code> as a <code>[[CCommand]]</code> (i.e. a string array where 0 is the command itself and 1+ are parameters).
'''<code>ServerCmd()</code>''' is a quick and easy method for passing user input to the server available through <code>[[cbase.h]]</code>. It uploads an [[Wikipedia:ASCII|ASCII]] string, including with it information about which connected client sent it. The received strings are processed in <code>ClientCommand()</code> as a <code>[[CCommand]]</code> (i.e. a string array where 0 is the command itself and 1+ are parameters).


{{warning|Since <code>ServerCmd()</code> 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'''.}}
{{tip|You can also trigger a <code>[[Developer_Console_Control#Using_the_ConCommand_class|ConCommand]]</code> if the client's argument matches its name.}}
 
{{warning|Since <code>ServerCmd()</code> 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==
==Example==
  ''client:''
  ''client:''
  engine->ServerCmd( [[VarArgs()|VarArgs]]<span style="color:gray;">("MyCommand %i", m_iMyNumber)</span> ); ''// if no variables are required, simply pass the command as a string.''
  [[engine]]->ServerCmd( [[VarArgs()|VarArgs]]<span style="color:gray;">("MyCommand %i", m_iMyNumber)</span> ); ''// No variables? Pass the command as a simple string.''
  ''server:''
  ''server:''
  bool CHL2_Player::ClientCommand( const CCommand &args )
  bool [[CBasePlayer]]::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]) );

Latest revision as of 11:42, 26 August 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 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