alias

From Valve Developer Community
Revision as of 19:00, 11 February 2023 by Pee (talk | contribs) (Pretty big edit this time. Added information on the alias command's ability to simulate a finite state machine. feel free to make grammatical edits to this.)
Jump to navigation Jump to search

alias is a console command that allows a specified command to invoke one or several other commands. Aliases will only act for the game session they were defined in. However, keys bound can last if config.cfg is not set to read-only.

If no parameters are given, alias will print a list of all aliases and their values.

Examples

After running the following command, typing foo in the console will execute echo bar

alias foo "echo bar"


An example of an alias that toggles zoom. running ZoomToggle will alternate between running +zoom and -zoom

alias "ZoomON"	   "+zoom; alias ZoomToggle ZoomOFF"
alias "ZoomOFF"    "-zoom; alias ZoomToggle ZoomON"
alias "ZoomToggle" "ZoomON"


An example of using aliases to increment/decrement the field of view. Note that running alias fovIN makes the command fovIN no longer do anything

alias "fov_85" "fov 85; alias fovIN;        alias fovOUT fov_90"
alias "fov_90" "fov 90; alias fovIN fov_85; alias fovOUT fov_95"
alias "fov_95" "fov 95; alias fovIN fov_90; alias fovOUT"
alias "fovIN"  "fov_85"
alias "fovOUT" "fov_95"


A common use of alias is to run commands when a key is unpressed. for example, the following aliases can be used to test when alt is pressed and unpressed to make alt+f4 exit the engine

bind alt "+alt_pressed"
alias +alt_pressed "bind f4 exit"
alias -alt_pressed "unbind f4"


The alias command is unique in that it does not require quotation marks for the contents of the alias. This means that aliases can be mapped to commands with quotation marks in them, as long as there are no external quotation marks.

alias healthpls "ent_fire !self addoutput "health 200"" Template:Code comment
alias healthpls ent_fire !self addoutput "health 200"   Template:Code comment


While this works for aliases mapped to single commands, it does not work with a list of semicolon seperated commands as the contents of an alias, because what follows the first semicolon will be executed independently from the alias command. This can be avoided with nested aliases, where the outer alias has quotation marks around it, and the inner alias does not.

The following example will not work:

alias healthpls ent_fire !self addoutput "health 200"; echo health restored.

This, however, works:

alias healthpls "add_health; echo health restored."
alias add_health ent_fire !self addoutput "health 200"

Advanced Uses

Ignoring the limitation that there can only be a predefined maximum number of aliases, the alias command alone makes the source console Turing complete. In other words, it is able to simulate any computer calculation, meaning a command can be run based on an arbitrarily complex way that aliased commands were invoked. For example, an alias can run another command if the amount of times the alias was invoked previously was prime. There are many uses that are more practical.

If the limit of a predefined maximum number of aliases is not ignored, the source console is only able to simulate a finite-state machine. However, due to how the alias command can simulate a finite-state machine, it negates the typical drawback of mathematical operations requiring extremely large quantities of states, and taking extremely long. This is because, when the layer of abstraction to imagine a set of aliases as a finite-state machine is added, it is assumed that the results of inputs (contents of aliases) are not changed by default. In other words, if state A dictates that input 1 will set the machine into state B, and input 2 of state A will set the machine into state C, then after invoking input 1, while the machine is indeed set to state B, inputs 1 and 2 still set the machine into states B and C, respectively, unless state B explicitly changes the functions of the inputs. the following example helps illustrate this:

alias state_A "alias input_1 state_B; alias input_2 state_C; alias input_3 state_D"
alias state_B "alias input_3 state_A"

Here, after state A is activated, followed by input 1 firing, the functions of inputs 1 and 2 persist after state B becomes the current state. This attribute of transitions persisting between states until redefined allows for enumerations. A set of binary enumerations, therefore, may be used to store data, which may be manipulated according to logic, allowing for reasonably efficient mathematical operations and comparisons of any data type, such as integers, doubles, or floats, all of which may be signed or unsigned, and of any length. Compare the ability to store a 32-bit value with 32 binary enumerations with a traditional finite-state machine's requirement of 232 states to store an equivalent amount of data.

It may be useful to not represent alias logic with a finite-state machine, but rather a series of disconnected states with transitions pointing to each, which do not come from any other state(s). each state may invoke any transition, change the location that transitions direct to, and directly call another state as a subroutine (though the transitions that the subroutine changes do not revert after the subroutine finishes).

See Also

Console Command List