Script: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
m (Used game templates directly. Dark pre.)
(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.)
Line 1: Line 1:
{{lang|script}}
{{lang|script}}
<code>'''script'''</code> is a [[console command]] in all {{src|4}} games since {{l4d2|4}} that have a {{sq}} [[Squirrel]] implementation of [[VScript]]{{confirm}}, namely [[VScript#Squirrel|these]].
<code>'''script'''</code> is a [[console command]] in all {{src|4}} games since {{l4d2|4}} that have a {{sq}} [[Squirrel]] implementation of [[VScript]], namely [[VScript#Squirrel|these]], and also in {{tf2|4}}.


== Description ==
== Description ==
It executes Squirrel code from the root table.
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 quotation marks (<code>"</code>) and semicolons (<code>;</code>) cannot be used for scripting needs.
{{note|The code can contain spaces but it cannot contain semicolons (<code>;</code>) because the developer console considers semicolons as delimiters for commands.}}
 
{{tip|In {{tf2|3}}, backticks (<code>`</code>) can be used to create `strings` safely with this command.}}
{{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 17: Line 18:
}}
}}
Should print the name of the current map in the console in most games.
Should print the name of the current map in the console in most games.
{{pre|
script player <- Entities.FindByClassname(null, `player`);
script player.SetMaxHealth( 200 )
}}
{{tf2|only}} Using backticks and a variable, find a player then set their max health to 200.


== See also ==
== See also ==

Revision as of 04:41, 20 January 2023

English (en)中文 (zh)Translate (Translate)

script is a console command in all Source Source games since Left 4 Dead 2 Left 4 Dead 2 that have a Squirrel Squirrel implementation of VScript, namely these, and also in Team Fortress 2 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 quotation marks (") and semicolons (;) cannot be used for scripting needs.

Tip.pngTip:In Team Fortress 2, backticks (`) can be used to create `strings` safely with this command.
Note.pngNote: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 activator, caller or self.
PlacementTip.pngWorkaround:Define the variables as needed temporarily, e.g. script ::activator <- Entities.FindByClassname(null, "player").

Syntax

script <code>

Example

script printl( GetMapName() )

Should print the name of the current map in the console in most games.

script player <- Entities.FindByClassname(null, `player`);
script player.SetMaxHealth( 200 )

(only in Team Fortress 2) Using backticks and a variable, find a player then set their max health to 200.

See also