Create server commands? Created 1 year ago2022-12-04 22:35:55 UTC by JacksBestGaming JacksBestGaming

Created 1 year ago2022-12-04 22:35:55 UTC by JacksBestGaming JacksBestGaming

Posted 1 year ago2022-12-04 22:35:55 UTC Post #347153
I want to create a command that can be ran on the dedicated server to send a console message to all clients. I have the code I need to run to send the message, but know I need to figure out how to make a command that can be sent from the dedicated server. Does anybody know how to make a console command for the dedicated server?
Posted 1 year ago2022-12-05 10:17:43 UTC Post #347154
Think of a similar existing command, like "changelevel mapname" or "say blabla", and just do a search for it in HL SDK's code, then see how it works.
Posted 1 year ago2022-12-05 17:16:30 UTC Post #347155
There are no examples of server commands in the SDK but it's pretty straightforward.

To add a command you need to call this function: https://github.com/ValveSoftware/halflife/blob/c7240b965743a53a29491dd49320c88eecf6257b/engine/eiface.h#L262

This registers the command with the given name.

Here's an example:
void PrintPlayerName()
{
    if (CMD_ARGC() < 1)
    {
        g_engfuncs.pfnServerPrint("Usage: print_player_name <player_index>\n");
        return;
    }

    const int index = atoi(CMD_ARGV(1));

    CBaseEntity* player = UTIL_PlayerByIndex(index);

    if (!player)
    {
        return;
    }

    g_engfuncs.pfnServerPrint(UTIL_VarArgs("Player name: %s\n"), STRING(player->pev->netname));
}

// Somewhere in startup code.
g_engfuncs.pfnAddServerCommand("print_player_name", &PrintPlayerName);
This command prints the name of the player at the given index, or nothing if the player slot isn't in use.
You must be logged in to post a response.