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
(Undo revision 484850 by WoShiGeNiCheng (talk))
Tag: Undo
 
(49 intermediate revisions by 23 users not shown)
Line 1: Line 1:
{{sq}} {{base point multi|logic_script|game1=Left 4 Dead 2|game2=Alien Swarm|game3=Portal 2}}
{{LanguageBar}} {{Source topicon}} {{Source 2 topicon}}
{{tabsBar|main=s2|base=logic_script}}
{{CD|CLogicScript|CPointEntity|file1=1}} __NOTOC__
[[File:Logic_script.png|left|Source 1 editor sprite]]


== Entity description ==
[[File:Logic_script_src2.png|left|Source 2 editor sprite]]
A container for [[VScript|scripts]]. 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.


==KeyValues==
{{this is a|logical entity|name=logic_script|since=Left 4 Dead 2}} It is also available in the {{tf2branch|4}} and {{mapbase|4}}.
{{KV|EntityGroup[0]|to=EntityGroup[15]|target_destination|Entities to add in the EntityGroup array for the script.}}
 
{{Note|The array can be extended beyond 16 entities. Simply add additional Group## keys with SmartEdit turned off.}}  
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}}
 
== KeyValues ==
{{KV Targetname}}
{{KV Targetname}}
{{KV|EntityGroup[0]|intn=Group00|to=EntityGroup[15]|intn2=Group15|target_destination
|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:
:*<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>.
:*<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 ==
{{I Targetname}}


== Outputs ==
{{note|For other VScript keyvalues notable for this entity like '''vscripts''', '''thinkfunction''' and inputs like '''RunScriptCode''', '''CallScriptFunction''' see [[Generic Keyvalues, Inputs and Outputs]] }}
{{O Targetname}}


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


==External links==
== External links ==
* [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
 
<!-- 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