This article's documentation is for anything that uses the Source engine. Click here for more information.
This article's documentation is for Source 2. Click here for more information.

Logic script: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
mNo edit summary
(Undo revision 484850 by WoShiGeNiCheng (talk))
Tag: Undo
 
(24 intermediate revisions by 12 users not shown)
Line 1: Line 1:
{{Source topicon}} {{Source 2 topicon}}
{{LanguageBar}} {{Source topicon}} {{Source 2 topicon}}
{{Lang}} __NOTOC__ {{CD|CLogicScript|CPointEntity}}
{{tabsBar|main=s2|base=logic_script}}
{{CD|CLogicScript|CPointEntity|file1=1}} __NOTOC__
[[File:Logic_script.png|left|Source 1 editor sprite]]
[[File:Logic_script.png|left|Source 1 editor sprite]]


[[File:Logic_script_src2.png|left|Source 2 editor sprite]]
[[File:Logic_script_src2.png|left|Source 2 editor sprite]]


{{Entity|logic_script|type=e0|since=Left 4 Dead 2}} It is also available in {{tf2|4}} and it is the container for [[VScript]]s. In Left 4 Dead 2, for example, this entity is used extensively for the logic in "carnival games" found in the Dark Carnival campaign, such as ''Peanut Gallery'', ''Strongman Contest'', ''Whack-a-Stach'', etc.
{{this is a|logical entity|name=logic_script|since=Left 4 Dead 2}} It is also available in the {{tf2branch|4}} and {{mapbase|4}}.


This entity functions as a container for [[VScript]]s. In Left 4 Dead 2, for example, it is used extensively for the logic in "carnival games" found in the Dark Carnival campaign, such as ''Peanut Gallery'', ''Strongman Contest'', ''Whack-a-Stach'', etc.
{{note|This entity counts as an [[edict_t|edict]], not a logical entity as one would expect.}}
{{clr}}
{{clr}}


== KeyValues ==
== KeyValues ==
{{KV|start}}
{{KV Targetname}}
{{KV|new=1|Group00|toKey=Group15|target_destination|EntityGroup[0]|toPN=EntityGroup[15]|Entities to add in the EntityGroup array for the script.}}
{{KV|EntityGroup[0]|intn=Group00|to=EntityGroup[15]|intn2=Group15|target_destination
{{KV|new=1|vscripts|scriptlist|Entity Scripts|since=L4D2|Space delimited list of [[VScript]] files (without file extension) that are executed after all entities have spawned. The scripts are all executed in the same script scope, later ones overwriting any identical variables and functions.}}
|3=Targetnames of entities to make them accessible in Squirrel code. All specified entity names will be searched for upon this entity spawning and their [[VScript Fundamentals#Script Handles|script handles]] are added to this <code>logic_script</code>'s script scope:
{{KV|new=1|thinkfunction|string|[[Entity_Scripts#Thinker_Functions|Think function]]|since=L4D2|Name of the function within this entity's script that'll be called automatically every 100 milliseconds, or a user-defined interval if the function returns a number. Avoid [[expensive]] operations in this function, as it may cause performance problems.}}
:*<code>EntityGroup</code>: an array where <code>EntityGroup[0]</code> is the handle of an entity with the name specified in the '''EntityGroup[0]''' keyvalue etc. If multiple entities exist with that name, the first handle found is used. The array length is the highest non-empty EntityGroup index that is not empty plus one, so in Squirrel code, be sure to not hardcode indices if the length is uncertain. If the keyvalue '''EntityGroup[i]''' is the empty string (for <code>i &lt; EntityGroup.len()</code>), then <code>EntityGroup[i]</code> is <code>null</code>.
{{KV|end}}
:*<code>MyEntityGroup</code> {{csgo|not}}: a table which contains the handles with keys being the names, for example if the '''EntityGroup[2]''' keyvalue is set to <code>sound.start</code>, then <code>MyEntityGroup["sound.start"] == EntityGroup[2]</code>.
:*<code>MyEntityGroupArray</code> {{csgo|not}}: an array which is the same as <code>EntityGroup</code>.
:{{expand|title=VScript run internally to achieve this|
The following VScript is run in logic_script scope when it spawns. <code>__AppendToScriptGroup</code> is then called with each name specified by <code>Group00</code>-<code>Group15</code> keyvalues.
<syntaxhighlight lang=js>EntityGroup <- [];
MyEntityGroup <- {};
MyEntityGroupArray <- EntityGroup;
function __AppendToScriptGroup( name )
{
if ( name.len() == 0 )
{
EntityGroup.append( null );
}
else
{
local ent = Entities.FindByName( null, name );
EntityGroup.append( ent );
if ( ent != null )
{
ent.ValidateScriptScope();
MyEntityGroup[name] <- ent;
ent.GetScriptScope().EntityGroup <- EntityGroup;
}
}
}</syntaxhighlight>
}}}}


== Inputs ==
 
{{IO|RunScriptFile|Execute a [[VScript]] file from disk, without file extension. The script contents are merged with the script scope of the receiving entity.|param=script|since={{l4d2}}|also={{tf2}}}}
{{note|For other VScript keyvalues notable for this entity like '''vscripts''', '''thinkfunction''' and inputs like '''RunScriptCode''', '''CallScriptFunction''' see [[Generic Keyvalues, Inputs and Outputs]] }}
{{IO|RunScriptCode|Execute a string of VScript source code in the scope of the entity receiving the input. String quotation may be needed when fired via console. {{ModernBugfix|In Hammer, using string arguments will corrupt the [[VMF]] file's structure, making the file unviewable for the next Hammer session.|Remove the string argument manually with a text editor.}}{{ModernNote|{{tf2}} Backtick characters ` are replaced with quotation marks at runtime, allowing quotation marks to be used when normally not possible.}}|param=string|since={{l4d2}}|also={{tf2}}}}
{{IO|CallScriptFunction|Calls a VScript function defined in the scope of the receiving entity.|param=string|since={{l4d2}}|also={{tf2}}|nofgd=1}}
{{IO|TerminateScriptScope|Destroys the script scope of the receving entity.|only={{tf2}}|nofgd=1}}


== See also ==
== See also ==
* [[L4D2 Vscripts]]
* [[L4D2 VScripts]]
* [[Left 4 Dead 2 Tool Updates]]
* [[Left 4 Dead 2 Tool Updates]]
* [[trigger_finale]]


== External links ==
== External links ==
* [https://web.archive.org/web/20170524163422/http://forums.steampowered.com/forums/showthread.php?t=1238461 It's the vscript'ing documentation FAQ! (Steam Forums)]
* [https://web.archive.org/web/20170524163422/http://forums.steampowered.com/forums/showthread.php?t=1238461 It's the VScripting documentation FAQ! (Steam Forums)]
* [http://leeland.stores.yahoo.net/l4d2-scripting.html Scripting for Left 4 Dead 2 (Leeland.net)] - Example uses of logic_script
* [http://leeland.stores.yahoo.net/l4d2-scripting.html Scripting for Left 4 Dead 2 (Leeland.net)] - Example uses of logic_script
<!-- for cats not added by {{this is a}} template -->
[[Category:Mapbase entities]]
[[Category:Mapbase logical entities]]
[[Category:Team Fortress 2 entities]]
[[Category:Team Fortress 2 logical entities]]
[[Category:Half-Life 2: Deathmatch entities]]
[[Category:Half-Life 2: Deathmatch logical entities]]

Latest revision as of 01:45, 5 July 2025

English (en)中文 (zh)Translate (Translate)
C++ Class hierarchy
CLogicScript
CPointEntity
CBaseEntity
C++ logicentities.cpp
Source 1 editor sprite
Source 2 editor sprite

logic_script is a logical entity available in all Source Source games since Left 4 Dead 2 Left 4 Dead 2. It is also available in the Team Fortress 2 branch Team Fortress 2 branch and Mapbase Mapbase.

This entity functions as a container for VScripts. In Left 4 Dead 2, for example, it is used extensively for the logic in "carnival games" found in the Dark Carnival campaign, such as Peanut Gallery, Strongman Contest, Whack-a-Stach, etc.

Note.pngNote:This entity counts as an edict, not a logical entity as one would expect.

KeyValues

Name (targetname) <string>[ Edit ]
The name that other entities refer to this entity by, via Inputs/Outputs or other keyvalues (e.g. parentname or target).
Also displayed in Hammer's 2D views and Entity Report.
See also:  Generic Keyvalues, Inputs and Outputs available to all entities

EntityGroup[0] (Group00) to EntityGroup[15] (Group15) <targetname>
Targetnames of entities to make them accessible in Squirrel code. All specified entity names will be searched for upon this entity spawning and their script handles are added to this logic_script's script scope:
  • EntityGroup: an array where EntityGroup[0] is the handle of an entity with the name specified in the EntityGroup[0] keyvalue etc. If multiple entities exist with that name, the first handle found is used. The array length is the highest non-empty EntityGroup index that is not empty plus one, so in Squirrel code, be sure to not hardcode indices if the length is uncertain. If the keyvalue EntityGroup[i] is the empty string (for i < EntityGroup.len()), then EntityGroup[i] is null.
  • MyEntityGroup (not in Counter-Strike: Global Offensive): a table which contains the handles with keys being the names, for example if the EntityGroup[2] keyvalue is set to sound.start, then MyEntityGroup["sound.start"] == EntityGroup[2].
  • MyEntityGroupArray (not in Counter-Strike: Global Offensive): an array which is the same as EntityGroup.
VScript run internally to achieve this

The following VScript is run in logic_script scope when it spawns. __AppendToScriptGroup is then called with each name specified by Group00-Group15 keyvalues.

EntityGroup <- [];
MyEntityGroup <- {};
MyEntityGroupArray <- EntityGroup;
function __AppendToScriptGroup( name ) 
{
	if ( name.len() == 0 ) 
	{ 
		EntityGroup.append( null ); 
	} 
	else
	{ 
		local ent = Entities.FindByName( null, name );
		EntityGroup.append( ent );
		if ( ent != null )
		{
			ent.ValidateScriptScope();
			MyEntityGroup[name] <- ent;
			ent.GetScriptScope().EntityGroup <- EntityGroup;
		}
	}
}


Note.pngNote:For other VScript keyvalues notable for this entity like vscripts, thinkfunction and inputs like RunScriptCode, CallScriptFunction see Generic Keyvalues, Inputs and Outputs

See also

External links