StuffCMD

This may be one of the most well used functions in coding today!

The stuffcmd function is very useful in Quake2 .dll programming.  It allows the server to force aliases, or commands, upon a client. This function should be in every mod made, and actually probably is. Add in the blue code and take out the pink code.

First, go to the end of p_client.c, and add these lines of code:

void stuffcmd (edict_t *e, char *s)
{
    gi.WriteByte (11);
    gi.WriteString (s);
    gi.unicast (e, true);
}

This is the actual stuffcmd function that let's you send commands to a client as though they typed them in the console themselves.
Next, add this line to the end of g_local.h:

void stuffcmd(edict_t *e, char *s);

This is just a forward declaration.  Now save them both.

Here's a few examples on how to use it:
Say you want to simplify some commands for clients. If they had to use two commands to use a grappling hook, like "cmd hook on" and "cmd hook off", then you can use stuffcmd to put them into one command. To set an alias so it's just one key, add these lines to your clientbegin and clientbegindeathmatch functions in p_client.c:

stuffcmd(ent, "alias +hook \"cmd hook on\"\n");
stuffcmd(ent, "alias -hook \"cmd hook off\"\n");

This makes it so a client only needs to bind a key to "+hook" to use the grappling hook, and it fires and pulls while holding the key down, and the hook releases when the client releases the key.

Another excellent use for stuffcmd is executing .cfg files.  If you want the client's .cfg file to execute automatically upon entering the game, add this line to the clientbegin and clientbegindeathmatch functions in p_client.c:

stuffcmd(ent, "exec cfg/mymod.cfg\n");

Of course you would change mymod to whatever you named the .cfg file.

That's it! Piece 'O Cake!

Tutorial by Willi
Quake Style - Tutorials