[Engine] DP_CON_SET

Post tutorials on how to do certain tasks within game or engine code here.
Post Reply
Spike
Posts: 2914
Joined: Fri Nov 05, 2004 3:12 am
Location: UK
Contact:

[Engine] DP_CON_SET

Post by Spike »

quick easy one:

cvar.c (or cmd.c, whereever really)

Code: Select all

void Cvar_Set_f(void)
{
	char *cvarname;
	char *cvarvalue;
	cvar_t *var;

	cvarname = Cmd_Argv(1);
	cvarvalue = Cmd_Argv(2);

	var = Cvar_FindVar(cvarname);
	if (!var)
	{
		if (Cmd_Exists(cvarname))
		{
			Con_Printf("%s exists as a command\n", cvarname);
			return;
		}
		var = malloc(sizeof(*var));
		if (!var)
			return;
		memset(var, 0, sizeof(*var));
		var->name = strdup(cvarname);
		var->string = "";
		Cvar_RegisterVariable(var);
	}

	Cvar_Set(cvarname, cvarvalue);
}
(try and find a cvar, if it doesn't exist, make sure its not already a command, malloc space for it, register it with an empty default value, and then set it to the argument requested)

cvar.h, add the following somewhere.

Code: Select all

void Cvar_Set_f(void);
cmd.c, Cmd_Init, add

Code: Select all

	Cmd_AddCommand ("set", Cvar_Set_f);
somewhere.


Now mods that use your engine don't have to use inane meaninglessly named cvars called 'temp1' for instance, but can instead use sane names like 'g_maxbots'. Because its slightly more sane.

Have fun... :P
Post Reply