Script: Difference between revisions
(update notes/tips) |
No edit summary |
||
Line 8: | Line 8: | ||
{{tip|for games on the modern (2025) [[SDK 2013]] branch ({{tf2}}{{css}}{{dods}}{{hl2dm}}{{hldms}}), Backticks {{code2|`}} can be used in {{code2|EntFire}} functions to create {{code2|`strings`}} safely with this command. {{confirm|games with older VScript implementations ({{l4d2}}{{asw}}{{portal2}}{{csgo}}) may be able to escape quotes instead (e.g. {{code2|\"string\"}}), unconfirmed.}}}} | {{tip|for games on the modern (2025) [[SDK 2013]] branch ({{tf2}}{{css}}{{dods}}{{hl2dm}}{{hldms}}), Backticks {{code2|`}} can be used in {{code2|EntFire}} functions to create {{code2|`strings`}} safely with this command. {{confirm|games with older VScript implementations ({{l4d2}}{{asw}}{{portal2}}{{csgo}}) may be able to escape quotes instead (e.g. {{code2|\"string\"}}), unconfirmed.}}}} | ||
{{warning|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. <br> | {{warning|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. <br> | ||
{{code2|script printl("print1"); script printl("print2")}} will throw a script error, but {{code2|script printl("print1");script printl("print2")}} will not.}} | {{example|{{code2|script printl("print1"); script printl("print2")}} will throw a script error, but {{code2|script printl("print1");script printl("print2")}} will not.}}}} | ||
{{note|Since the code is not executed from an entity's script scope and not in response to [[I/O]], the pre-defined variables for <code>activator</code>, <code>caller</code> or <code>self</code>. {{workaround|Either use {{code2|RunScriptCode}} inputs to target these variables, or define the variables as needed temporarily, e.g. {{code2|ent_fire player runscriptcode "printl(self)"}} or {{code2|script ::activator <- Entities.FindByClassname(null, "player")}}.}} }} | {{note|Since the code is not executed from an entity's script scope and not in response to [[I/O]], the pre-defined variables for <code>activator</code>, <code>caller</code> or <code>self</code>. {{workaround|Either use {{code2|RunScriptCode}} inputs to target these variables, or define the variables as needed temporarily, e.g. {{code2|ent_fire player runscriptcode "printl(self)"}} or {{code2|script ::activator <- Entities.FindByClassname(null, "player")}}.}} }} | ||
Revision as of 14:23, 10 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.
Description
It executes Squirrel code from the root table. The code can contain spaces without quotation marks used. Despite this, the developer console reads the code like a typical command's value, so special console delimiters like semicolons (;
) cannot be used for scripting needs, making for statements impossible.









activator
, caller
or self
. 
Syntax
script <code>
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!