Script: Difference between revisions
(Expand on developer console delimiters through quotation marks and backticks, moving it out of {{note}}. A new example was also created for tf2's backticks.) |
(Quotation marks do work, tested in tf2 && csgo, I couldn't find a scenario where quotes are a problem. Example with player health should work in all games (with normal quotes). Found workaround for semicolons.) |
||
Line 3: | Line 3: | ||
== Description == | == Description == | ||
It executes Squirrel code from the root table, which 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 | It executes Squirrel code from the root table, which can contain spaces without quotation marks used. Despite this, the [[Developer Console|developer console]] reads the code like a typical command's value, so special console delimiters like semicolons (<code>;</code>) cannot be used for scripting needs, making <tt>for</tt> statements impossible. | ||
{{tip|In {{tf2|3}}, backticks (<code>`</code>) can be used to create `strings` safely with this command.}} | {{tip|In {{tf2|3}}, backticks (<code>`</code>) can be used to create `strings` safely with this command.}} | ||
{{tip|Instead of delimiting two script statements with a semicolon, one can put each statement inside curly braces, as long as this works in terms of scope. In the Squirrel documentation, it says:<br>''Statements can be separated with a new line or ‘<code>;</code>’ (or with the keywords <tt>case</tt> or <tt>default</tt> if inside a switch/case statement), both symbols are not required if the statement is followed by ‘<code>}</code>’.''<sup>[http://www.squirrel-lang.org/squirreldoc/reference/language/statements.html link]</sup>}} | |||
{{note|Since the code is not executed from an entity's script scope and not in response to [[I/O]], one can most likely not execute code that calls the variables <code>activator</code>, <code>caller</code> or <code>self</code>. {{workaround|Define the variables as needed temporarily, e.g. <code>script ::activator <- Entities.FindByClassname(null, "player")</code>.}} }} | {{note|Since the code is not executed from an entity's script scope and not in response to [[I/O]], one can most likely not execute code that calls the variables <code>activator</code>, <code>caller</code> or <code>self</code>. {{workaround|Define the variables as needed temporarily, e.g. <code>script ::activator <- Entities.FindByClassname(null, "player")</code>.}} }} | ||
Line 13: | Line 14: | ||
}} | }} | ||
== | == Examples == | ||
{{pre| | {{pre| | ||
script printl( GetMapName() ) | script printl( GetMapName() ) | ||
}} | }} | ||
Prints the name of the current map in the console. | |||
{{pre| | {{pre| | ||
script player <- Entities.FindByClassname(null, | script player <- Entities.FindByClassname(null, "player"); | ||
script player.SetMaxHealth( 200 ) | script player.SetMaxHealth( 200 ) | ||
}} | }} | ||
Using a variable, find the [[player]] with the lowest [[entity index]] then set their max health to 200. | |||
{{pre| | |||
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 <code>player <- ...</code> despite the curly braces, creates a table slot in the root table that persists after the closing brace. If the <tt>player</tt> variable should be a <tt>local</tt> 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 {{key|up}} and down {{key|down}} arrow keys in the console it may even be useful to add a comment in the command, for example the following. {{pre| | |||
script /* SET HEALTH 200 */ { player <- Entities.FindByClassname(null, "player") } { player.SetMaxHealth(200) } // SET HEALTH 200 | |||
}} | |||
Squirrel will never see line comments starting with <code>//</code> because the developer console sees and removes them first. | |||
Block comments <code>/* */</code> can also be used because Squirrel supports them! | |||
== See also == | == See also == |
Revision as of 18:34, 20 January 2023
script
is a console command in all Source games since
Left 4 Dead 2 that have a
Squirrel implementation of VScript, namely these, and also in
Team Fortress 2.
Description
It executes Squirrel code from the root table, which 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.

`
) can be used to create `strings` safely with this command.
Statements can be separated with a new line or ‘
;
’ (or with the keywords case or default if inside a switch/case statement), both symbols are not required if the statement is followed by ‘}
’.link
activator
, caller
or self
. 
script ::activator <- Entities.FindByClassname(null, "player")
.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 HEALTH 200 */ { player <- Entities.FindByClassname(null, "player") } { player.SetMaxHealth(200) } // SET 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!