Clients (SourceMod Scripting)

From AlliedModders Wiki
Jump to: navigation, search

SourceMod assigns a Client Index number to client in the server. You can use these numbers to refer to specific clients in various SourceMod APIs. In particular, the [Clients APIs https://sm.alliedmods.net/new-api/clients].

The server is given client index 0, and the first connected user will have client index 1.

There are two variables that can be used to talk about the maximum number of clients:

  • MaxClients: The maximum number of players currently allowed to connect to the server.
    • This is set at runtime based on gpGlobals->maxClients in the game server.
  • MAXPLAYERS: The theoretical maximum number of clients allowed on any Source game.
    • This number is set to 65, enough for 64 players + a SourceTV client.

These values represent the maximum count of clients. Since Client indexing starts at 1, be wary of off-by-one errors here!

It's recommended that you use <= MaxClients when looping through the client list, and MAXPLAYERS+1 when initializing an array to contain data for individual clients.

For example:

// Declare static arrays to support the maximum number of clients possible.
new playerVotes[MAXPLAYERS+1];
 
 
public void OnPluginStart()
{
    RegConsoleCmd("start_vote", Command_StartVote);
    RegConsoleCmd("vote", Command_Vote);
}
 
public Action Command_StartVote(int client, int args)
{
  // Loop through all clients based on current number of max clients.
  for(int i = 0; i <= MaxClients; i++) {
    playerVotes[i] = 0;
  }
}
 
public Action Command_Vote(int client, int args)
{
    char arg[128];
    if( args > 0)
    {
        GetCmdArg(i, arg, sizeof(arg));
        // Associate data with clients at their client index in an array.
        playerVotes[client] = StringToInt(arg);
    }
 
    return Plugin_Handled;
}


Other notes:

The game engine's CBaseServer has its own m_clients list. You can subtract 1 from a SourceMod client index to get the CBaseServer::m_clients index.