This is the sidebar
This is an old revision of the document!
This guide will explain the more advanced concepts of the console in DarkPlaces.
Aliases are fundamental in script creation. They are the key to simplification, and often the principal part of a script.
Aliases are “shortcuts” to commands. For example, if I am too lazy to type “chmap silvercity” every time I want to play on silvercity locally, all I have to do is set up an alias that way:
alias sc "chmap silvercity"
The quotes are not needed, but it is always better to use them, as multi-command aliases need them:
alias hi say hello; say how are you? alias hi "say hello; say how are you?"
The first alias is a shortcut to “say hello”. When the alias is saved, you will say “how are you?”.
The second alias assigns “hi” to both commands. They are executed one after the other when I enter the new “hi” command.
There are a few special variables accessible only from inside an alias. There are 80 of them, that should be enough for anything.
These are $1 $2 $3 ... $79 which expand to the nth parameter, and $* which contains all the parameters.
You can also use, for example, $3- to expand to all the arguments after the 3rd, included.
For example:
]alias args "echo Hello, here are all your parameters: $*; echo And here is the first: $1; echo And all the parameters from the 4th: $4-" ]args One "Two hey don't typefrag" Three Four "Forty Two" Hello, here are all your parameters: One Two hey don't typefrag Three Four Forty Two And here is the first: One And all the parameters from the 4th: Four Forty Two
As you can see, text contained in quotes is considered as one argument.
See Variable expansion below for more details on the use of variables.
First of all, what does “variable” mean? It is any cvar 1) plus the alias parameters.
Variable expansion lets you use content from variables in other commands. Here is an example session:
]set foo "Hello World!" ]echo $foo Hello World!
As you can see, you just need a $ in front of the variable's name to get expansion. If you want to escape $2), just use $$ (not \$).
]echo I'm going to show you how to expand a cvar. I'm going to show you how to expand a cvar. ]echo Just type this to expand timelimit: $timelimit Just type this to expand timelimit: 20 ]echo Woops, I meant $$timelimit of course! Woops, I meant $timelimit of course!
Sometimes you may want to use the longer syntax: ${variable} if your variable name contains spaces (this won't happen often) or if you need to do “double expansion”, that is interpret the content of a variable as another variable's name, and expand this again. Example:
]set levelOne "levelTwo" ]set levelTwo $message ]echo $levelOne levelTwo ]echo $$levelOne $levelOne ]echo ${$levelOne} Thank you xsax! But our Xolar is in another castle!
The parser escapes the quotes and backslashes present in variables before expanding them, to avoid messups in aliases. Though this can sometimes be annoying. It can fortunately be bypassed using the ${var asis} syntax. Example:
]set bar "this is a fancy backslash: \ " ]echo $bar this is a fancy backslash: \\ ]echo ${bar asis} this is a fancy backslash: \
Another example showing why this escaping makes aliases safer:
]alias foo "echo First parameter: ${1 asis} Second parameter: ${2 asis}" ]alias bar "foo \"${1 asis}\" \"${2 asis}\"" ]bar "One One" "Two Two" First parameter: One One Second parameter: Two Two ]bar "One \"One" "Two Two" First parameter: One Second parameter: One"
Can anyone find a better/simpler example?
As you can see, the second time I called “bar” with an “infected” string, the alias messed up. It would have been safe without using “asis”.
There is a little issue concerning variable expansion in aliases that can fortunately be worked around. It happens when you try to set a new value to a cvar and then want to expand it.
Here is an example of the issue:
]set x OLD ]alias foo "set x NEW; echo $x" ]foo OLD ]foo NEW
As seen here, $x is expanded before the alias is executed. The only way to delay this expansion is to split the alias:
]set x OLD ]alias foo "set x NEW; foo2" ]alias foo2 "echo $x" ]foo NEW
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | |
![]() | GFDLcontent Unless mentioned on the licensing page, the work on this page is licensed under the GNU Free Documentation License. The author states that the text and images can be used within the restrictions of this license (for example, they can be incorporated into certain free encyclopedias such as Wikipedia). |
Kindly hosted by NexuizNinjaz.com |