This article's documentation is for the "GoldSrc" engine. Click here for more information.
This article's documentation is for anything that uses the Source engine. Click here for more information.
This article's documentation is for Source 2. Click here for more information.

Alias: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
m (better example)
 
(108 intermediate revisions by 5 users not shown)
Line 1: Line 1:
'''<code>alias</code>''' is a [[Developer Console|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 <code>config.cfg</code> is not set to read-only. It is common for aliases to be defined in configuration files.
{{LanguageBar}}{{toc-right}}
{{this is a|console command|name=alias|engine=GoldSrc|engine1=Source|engine2=Source 2}} It defines an ''alias'', that is, a shortcut to one or more commands. It may overwrite previous definitions of the alias.


If no parameters are given, <code>alias</code> will print a list of all aliases and their values.
Aliases will only act for the engine session they were defined in.
== Examples ==
After running the following command, typing <code>foo</code> in the console will execute <code>echo bar</code>
alias foo "echo bar"


== Usage ==
{{CodeBlock|<!--
  -->alias<br><!--
  -->{{stx|type|alias}} {{stx|func|<name>}} <command><br><!--
  -->{{stx|type|alias}} {{stx|func|<name>}} "<command1>; <command2>; ..."<!--
-->}}
If no parameters are given, the game prints {{code|Current alias commands:}} followed by a list of all existing aliases and their values, if any. They appear in the order in which they were defined for the first time, starting with the newest.


An example of an alias that toggles zoom. running <code>ZoomToggle</code> will alternate between running <code>+zoom</code> and <code>-zoom</code>
The first parameter is the alias name (max. 31 characters, not case-sensitive) and all subsequent parameters are concatenated with a space character to form the command that the alias name will be associated with (max. 1023 characters). <!--{{Bug|The game always appends a space at its end.}}--> Passing no command behaves like passing the empty command.
alias "ZoomON"   "+zoom; alias ZoomToggle ZoomOFF"
alias "ZoomOFF"    "-zoom; alias ZoomToggle ZoomON"
alias "ZoomToggle" "ZoomON"


Invoking the name of an alias like a command (after it has been defined with the alias command) will execute its associated command; Passing parameters has no effect.


An example of using aliases to increment/decrement the field of view. Note that running <code>alias fovIN</code> makes the command <code>fovIN</code> no longer do anything
The name of a new alias may not equal any existing console variable or console command.
alias "fov_85" "fov 85; alias fovIN;        alias fovOUT fov_90"
If the alias already exists, its associated command will be overwritten.
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"


When an alias is mapped to a command with quotation marks, the quotation marks do not become part of the alias. Instead, they decide whether semicolons terminate the alias command. If the command {{code|{{stx|type|alias}} {{stx|func|bind_mouse1}} bind mouse1 "echo foo;echo bar"}} is run, then {{code|{{stx|func|bind_mouse1}} }} is stored as {{code|bind mouse1 echo foo;echo bar}}.
=== Examples ===
After running the command {{Code|{{stx|type|alias}} {{stx|func|foo}} echo bar}}, typing {{code|{{stx|func|foo}}}} in the console will execute {{code|echo bar}}.


A common use of <code>alias</code> is to run commands when a key is unpressed. for example, the following aliases can be used to test when <code>alt</code> is pressed and unpressed to make <code>alt + f4</code> exit the engine
After running the command {{Code|{{stx|type|alias}} {{stx|func|foo}}}}, typing {{code|{{stx|func|foo}}}} in the console will no longer do anything.
bind alt "+alt_pressed"
alias +alt_pressed "bind f4 exit"
alias -alt_pressed "unbind f4"




{{warning|An alias doesn't work if the command it is set to invoke has quotation marks. ({{ent|alias}} does not support quotation marks inside quotation marks.)}}
An example of an alias that toggles zoom. Running {{code|{{stx|func|ZoomToggle}}}} will alternate between running {{code|+zoom}} and {{code|-zoom}}
  alias healthpls "ent_fire !self addoutput "health 200"" //does not work
{{CodeBlock|<!--
{{workaround|Create a separate file containing that lone command and set alias to {{ent|exec}} the file.}}
      -->{{stx|type|alias}} ZoomON "+zoom; {{stx|type|alias}} {{stx|func|ZoomToggle}} ZoomOFF"<nowiki>
//This is the only line in file health_wrapper.cfg:
</nowiki>{{stx|type|alias}} ZoomOFF "-zoom; {{stx|type|alias}} {{stx|func|ZoomToggle}} ZoomON"<nowiki>
ent_fire !self addoutput "health 200"
</nowiki>{{stx|type|alias}} {{stx|func|ZoomToggle}} ZoomON<!--
-->}}


alias healthpls "exec health_wrapper.cfg" //works


== Advanced Uses ==
An example of three aliases used to cycle between several binds.
The <code>alias</code> command alone makes the source console [[Wikipedia:Turing_completeness|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.
{{CodeBlock|style=white-space:pre|<!--
  -->{{stx|type|alias}} {{stx|func|message_1}} "say message 1.; bind x {{stx|func|message_2}}"<br><!--
  -->{{stx|type|alias}} {{stx|func|message_2}} "say message 2.; bind x {{stx|func|message_3}}"<br><!--
  -->{{stx|type|alias}} {{stx|func|message_3}} "say message 3.; bind x {{stx|func|message_1}}"<br><!--
  -->bind x {{stx|func|message_1}}<!--
-->}}


== See Also ==
:{{Tip|In cases where a console variable has to toggle between several values, {{ent|toggle}} should be used instead of aliases to toggle, such as the following:<br><!--
[[Console Command List]]
-->{{Code|toggle tf_bot_quota normal fill match}}<br><!--


[[Category:Console Commands]]
-->In cases where a console variable should be changed by a constant value or factor, using the commands {{ent|incrementvar}} and {{ent|multvar}} goes without the definition of numerous aliases and therefore causes much less redundance:<!--
[[Category:Core Console Commands]]
-->{{CodeBlock|style=white-space:pre|<!--
  -->{{stx|type|alias}} {{stx|func|fovIN}}  incrementvar fov {{stx|no|85 95 -5}}<br><!--
  -->{{stx|type|alias}} {{stx|func|fovOUT}} incrementvar fov {{stx|no|85 95 5}}<!--
-->}}
}}
 
 
Alias names may also use the prefixes {{code|+}} and {{code|-}}, see {{sect|page=bind|Syntax}}. A common use of <code>alias</code> is to change keybindings when a key is pressed or unpressed. For example, the following aliases can be used to make {{key|Alt|F4}} exit the engine (whereas {{key|F4}} alone will not):
{{CodeBlock|<!--
  -->{{stx|type|alias}} {{stx|func|+alt_pressed}}  bind f4 exit<br><!--
  -->{{stx|type|alias}} {{stx|func|-alt_pressed}} unbind f4<br><!--
  -->bind alt {{stx|func|+alt_pressed}} {{stx|comm|// note that pressing Alt executes +alt_pressed and releasing Alt executes -alt_pressed}}<br><!--
-->}}
 
== Technical details ==
Each alias is a [[Wikipedia:Struct (C programming language)|struct]] consisting of a <tt>name</tt>, a command string <tt>value</tt> and a reference to the next alias <tt>next</tt>.
 
To iterate over all existing aliases, the game stores a reference to some "first" alias. When a new alias is created, it is inserted as the first element. When iterating over all aliases (such as when printing them via the command {{code|alias}}), the alias that was created first will be the last element.
 
== Advanced uses ==
Ignoring the limitation that there can only be a predefined maximum number of aliases, the <code>alias</code> command alone makes the console [[Wikipedia:Turing completeness|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 console is only able to simulate a [[Wikipedia:Finite-state machine|finite-state machine]]. However, due to how the <code>alias</code> command can simulate a finite-state machine, it negates the typical drawback of mathematical operations requiring extremely large quantities of states. This is because unlike a traditional finite-state machine, the results of inputs (contents of aliases) are not changed by default upon a transition. The following example helps illustrate this:
{{CodeBlock|<!--
      -->{{stx|type|alias}} state_A "{{stx|type|alias}} {{stx|func|input_1}} state_B; {{stx|type|alias}} {{stx|func|input_2}} state_C; {{stx|type|alias}} {{stx|func|input_3}} state_D"<nowiki>
</nowiki>{{stx|type|alias}} state_B  {{stx|type|alias}} {{stx|func|input_3}} state_B<!--
-->}}
 
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 what are effectively switch statements, except the case is switched separately from when the switch is invoked. A set of binary switches, which are effectively bits, 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 switches to a traditional finite-state machine's requirement of 2<sup>32</sup> states to store an equivalent amount of data.

Latest revision as of 16:36, 26 July 2024

English (en)Translate (Translate)

alias is a console command available in all GoldSrc GoldSrc, Source Source, and Source 2 Source 2 games. It defines an alias, that is, a shortcut to one or more commands. It may overwrite previous definitions of the alias.

Aliases will only act for the engine session they were defined in.

Usage

alias
alias <name> <command>
alias <name> "<command1>; <command2>; ..."

If no parameters are given, the game prints Current alias commands: followed by a list of all existing aliases and their values, if any. They appear in the order in which they were defined for the first time, starting with the newest.

The first parameter is the alias name (max. 31 characters, not case-sensitive) and all subsequent parameters are concatenated with a space character to form the command that the alias name will be associated with (max. 1023 characters). Passing no command behaves like passing the empty command.

Invoking the name of an alias like a command (after it has been defined with the alias command) will execute its associated command; Passing parameters has no effect.

The name of a new alias may not equal any existing console variable or console command. If the alias already exists, its associated command will be overwritten.

When an alias is mapped to a command with quotation marks, the quotation marks do not become part of the alias. Instead, they decide whether semicolons terminate the alias command. If the command alias bind_mouse1 bind mouse1 "echo foo;echo bar" is run, then bind_mouse1 is stored as bind mouse1 echo foo;echo bar.

Examples

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

After running the command alias foo, typing foo in the console will no longer do anything.


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 three aliases used to cycle between several binds.

alias message_1 "say message 1.; bind x message_2"
alias message_2 "say message 2.; bind x message_3"
alias message_3 "say message 3.; bind x message_1"
bind x message_1
Tip.pngTip:In cases where a console variable has to toggle between several values, toggle should be used instead of aliases to toggle, such as the following:
toggle tf_bot_quota normal fill match
In cases where a console variable should be changed by a constant value or factor, using the commands incrementvar and multvar goes without the definition of numerous aliases and therefore causes much less redundance:
alias fovIN incrementvar fov 85 95 -5
alias fovOUT incrementvar fov 85 95 5


Alias names may also use the prefixes + and -, see bind § Syntax. A common use of alias is to change keybindings when a key is pressed or unpressed. For example, the following aliases can be used to make Alt+F4 exit the engine (whereas F4 alone will not):

alias +alt_pressed bind f4 exit
alias -alt_pressed unbind f4
bind alt +alt_pressed // note that pressing Alt executes +alt_pressed and releasing Alt executes -alt_pressed

Technical details

Each alias is a struct consisting of a name, a command string value and a reference to the next alias next.

To iterate over all existing aliases, the game stores a reference to some "first" alias. When a new alias is created, it is inserted as the first element. When iterating over all aliases (such as when printing them via the command alias), the alias that was created first will be the last element.

Advanced uses

Ignoring the limitation that there can only be a predefined maximum number of aliases, the alias command alone makes the 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 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. This is because unlike a traditional finite-state machine, the results of inputs (contents of aliases) are not changed by default upon a transition. 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_B

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 what are effectively switch statements, except the case is switched separately from when the switch is invoked. A set of binary switches, which are effectively bits, 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 switches to a traditional finite-state machine's requirement of 232 states to store an equivalent amount of data.