Script: Difference between revisions
(→Description: not sure this tip actually fits this page. afaik ` are converted only inside RunScriptCode input) |
(Less colors, more overview. Added headline Limitations.) |
||
Line 2: | Line 2: | ||
It is available in all games that have a {{sq|4}} implementation of [[VScript]], namely [[VScript#Squirrel|these]]. | It is available in all games that have a {{sq|4}} implementation of [[VScript]], namely [[VScript#Squirrel|these]]. | ||
== Syntax == | |||
<pre>script <code></pre> | |||
== Description == | == Description == | ||
It executes Squirrel code from the root table. The code can contain spaces without quotation marks | It executes Squirrel code from the root table. The code can contain spaces even without using quotation marks {{code2|"}}. | ||
{{ | == Limitations == | ||
Semicolons {{code2|;}} cannot be used for scripting needs because the [[Developer Console|developer console]] treats them as a delimiter for console commands. | |||
* {{code2|for}} statements are impossible but not {{code2|foreach}} statements: To print the numbers 0 to 9, one can do {{code2|1=foreach (i,n in array(10)) printl(i)}}, where {{code2|array(10)}} is an array with 10 null entries. | |||
* Multiple script commands separated by semicolons will fail to parse correctly if there is a space between them due to Squirrel attempting to use the semicolon as a separator. For example, {{code2|script printl("print1"); script printl("print2")}} will throw a script error, but {{code2|script printl("print1");script printl("print2")}} will not. | |||
Quotes {{code2|"}} may cause problems. | |||
{{pre| | * In games on the modern [[SDK 2013]] branch since 2025 ({{tf2}}{{css}}{{dods}}{{hl2dm}}{{hldms}}), the <kbd>RunScriptCode</kbd> input will automatically convert backticks {{code2|`}} in its parameter to quotes {{code2|"}}. Older VScript implementations don't have this ({{l4d2}}{{asw}}{{portal2}}{{csgo}}) but can still escape quotes instead e. g. {{code2|\"string\"}}. This way of escaping quotes is only possible in scripts and '''not''' in Hammer or {{cmd|ent_fire}}. | ||
script | |||
}} | |||
Code referencing {{code2|activator}}, {{code2|caller}} or {{code2|self}} may cause an exception. These pre-defined variables normally don't exist and are only created if code is executed from an entity's script scope or in response to [[I/O]]. | |||
* One can use {{code2|RunScriptCode}} inputs to target these variables, e. g. {{code2|ent_fire player runscriptcode "printl(self)"}} | |||
* One can define the variables as needed temporarily, e. g. {{code2|script ::activator <- Entities.FindByClassname(null, "player")}}. | |||
== Examples == | == Examples == |
Revision as of 08:05, 11 May 2025
script
is a console command available in all Source games since
Left 4 Dead 2.
It is available in all games that have a Squirrel implementation of VScript, namely these.
Syntax
script <code>
Description
It executes Squirrel code from the root table. The code can contain spaces even without using quotation marks ".
Limitations
Semicolons ; cannot be used for scripting needs because the developer console treats them as a delimiter for console commands.
- for statements are impossible but not foreach statements: To print the numbers 0 to 9, one can do foreach (i,n in array(10)) printl(i), where array(10) is an array with 10 null entries.
- Multiple script commands separated by semicolons will fail to parse correctly if there is a space between them due to Squirrel attempting to use the semicolon as a separator. For example, script printl("print1"); script printl("print2") will throw a script error, but script printl("print1");script printl("print2") will not.
Quotes " may cause problems.
- In games on the modern SDK 2013 branch since 2025 (
), the RunScriptCode input will automatically convert backticks ` in its parameter to quotes ". Older VScript implementations don't have this (
) but can still escape quotes instead e. g. \"string\". This way of escaping quotes is only possible in scripts and not in Hammer or ent_fire.
Code referencing activator, caller or self may cause an exception. These pre-defined variables normally don't exist and are only created if code is executed from an entity's script scope or in response to I/O.
- One can use RunScriptCode inputs to target these variables, e. g. ent_fire player runscriptcode "printl(self)"
- One can define the variables as needed temporarily, e. g. script ::activator <- Entities.FindByClassname(null, "player").
Examples
script printl( GetMapName() )
Prints the name of the current map in the console.
script player <- Entities.FindByClassname(null, "player"); script player.SetMaxHealth( 200 )
Using a variable, find the player with the lowest entity index then set their max health to 200.
script { player <- Entities.FindByClassname(null, "player") } { player.SetMaxHealth(200) }
The same code, but as a single console command, achieved by using curly braces to bypass semicolons. Note that in this example, one pair of curly braces would suffice; if there is no nesting, putting every other statement in curly braces is sufficient. Note also that the statement player <- ...
despite the curly braces, creates a table slot in the root table that persists after the closing brace. If the player variable should be a local variable, its assignment statement must not stand alone inside curly braces, since its scope ends with the closing curly brace.
When executing multiple complicated commands repeatedly using the up ↑ and down ↓ arrow keys in the console it may even be useful to add a comment in the command, for example the following.
script /* SET MAX HEALTH 200 */ { player <- Entities.FindByClassname(null, "player") } { player.SetMaxHealth(200) } // SET MAX HEALTH 200
Squirrel will never see line comments starting with //
because the developer console sees and removes them first.
Block comments /* */
can also be used because Squirrel supports them!