Implementing a run key

From Valve Developer Community
Jump to navigation Jump to search

By default, GoldSrc GoldSrc-based games make the player always run. The engine provides a +speed command and associated kbutton_t (in_speed) that applies a speed multiplier to movement when held. For mods where walking should be the default and a run key is desired instead, the logic needs to be inverted.

Background

The in_speed kbutton is declared in cl_dll/input.cpp and drives two things:

  • Move scaling - in CL_CreateMove, move axis values are multiplied by cl_movespeedkey when the button is held.
  • Joystick move scaling - in IN_JoyMove (cl_dll/inputw32.cpp), the same multiplier is applied to joystick axis output.

In the original code, in_speed being active triggers the multiplier. To flip this so that the player walks by default and runs only while the key is held, two changes are needed:

  1. Invert which branch applies the multiplier.
  2. Set cl_movespeedkey to a value less than 1.0 so the multiplier slows the player down rather than speeding them up.

Changes

cl_dll/input.cpp

CL_CreateMove

Find the block that scales move values when in_speed is active:

		if (in_speed.state & 1)
		{
			cmd->forwardmove *= cl_movespeedkey->value;
			cmd->sidemove *= cl_movespeedkey->value;
			cmd->upmove *= cl_movespeedkey->value;
		}

Invert the condition so the multiplier applies when the key is not held:

		if ( !(in_speed.state & 1) )
		{
			cmd->forwardmove *= cl_movespeedkey->value;
			cmd->sidemove *= cl_movespeedkey->value;
			cmd->upmove *= cl_movespeedkey->value;
		}

The default state (no key held) now applies the multiplier, slowing the player to walk speed. Holding the key bypasses the multiplier and moves at full speed.

cl_dll/inputw32.cpp

IN_JoyMove

IN_JoyMove applies joystick and gamepad axis values to the player's movement and view angles each frame. It maintains its own copy of the speed check, which must be inverted to match the keyboard behavior.

Find:

	if (in_speed.state & 1)
		speed = cl_movespeedkey->value;
	else
		speed = 1;

Change to:

	if (in_speed.state & 1)
		speed = 1;
	else
		speed = cl_movespeedkey->value;

Without this change, joystick input would always run at full speed regardless of the run key.

cl_movespeedkey default value

cl_movespeedkey must be set to a value less than 1.0 so that the multiplier reduces speed rather than increasing it. The appropriate value is a design decision - retail Half-Life uses 0.3 for its walk modifier.

Registered cvar (console-accessible)

The SDK default registers cl_movespeedkey via pfnRegisterVariable in InitInput, making it overridable from the console:

cl_movespeedkey = gEngfuncs.pfnRegisterVariable( "cl_movespeedkey", "0.3", 0 );

The declaration at the top of input.cpp remains a pointer:

cvar_t *cl_movespeedkey;

Unregistered cvar (fixed in code)

If you do not want players to be able to change the run speed from the console, remove the pfnRegisterVariable call from InitInput and replace the pointer declaration with a plain struct:

cvar_t cl_movespeedkey = { "cl_movespeedkey", "0.3", 0 };
Note.pngNote: Any references to cl_movespeedkey->value in the file will need to be updated to cl_movespeedkey.value to match the non-pointer declaration.

Binding

Bind +speed to the desired walk key in your config.cfg:

bind "SHIFT" "+speed"

See also