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
No edit summary
(Undo revision 484850 by WoShiGeNiCheng (talk))
Tag: Undo
 
(51 intermediate revisions by 23 users not shown)
Line 1: Line 1:
{{sq}}{{l4d2}} {{portal2}}  '''logic_scipt''' is a point entity available in [[Portal 2]] and [[Left 4 Dead 2]]. It is an entity that acts as a container for [[VScript]]. For example, the entity is used extensively with [[L4D2 Vscripts|vscripts]] for the logic in "carnival games" found in the Dark Carnival campaign--''Peanut Gallery'', ''Strongman Contest'', ''Whack-a-Stach'', etc.
{{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]]


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


;EntityGroup[0-15] Group00-Group15 <code><[[targetname]]></code>
{{this is a|logical entity|name=logic_script|since=Left 4 Dead 2}} It is also available in the {{tf2branch|4}} and {{mapbase|4}}.
:<target_destination>
{{Note|The array can be extended beyond 16 entities. Simply add additional Group## keys with SmartEdit turned off.}}  
{{KV Targetname}}


===Additional KeyValues===
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.
These additional keys can to be added to the entity with SmartEdit turned off.
;vscripts <code><[[string]]></code>
:Name(s) of script files that are executed after all entities have spawned. The location of the vscript, starting at '''scripts/vscripts'''. Example: carnival_games/gallery_copy_rotators\
;thinkfunction <code><[[string]]></code>
:Name of a function in this entity's script scope which will be called automatically. In L4D2, this allows a function specified to be called every 100 milliseconds (ten times a second) for the duration of the script. You tell it which function to call by adding a thinkfunction key value in your logic_script, setting the value to the name of the function. Valve uses "Think" as the name of the function for their "carnival games". You don't want to do much in this function if it takes more than a 100 milliseconds as it may cause issues.


==Inputs==
{{note|This entity counts as an [[edict_t|edict]], not a logical entity as one would expect.}}
{{clr}}


{{I Targetname}}
== KeyValues ==
{{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>
}}}}


==Outputs==


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


{{DISPLAYTITLE:logic_script}}
== See also ==
* [[L4D2 VScripts]]
* [[Left 4 Dead 2 Tool Updates]]


==See also==
== External links ==
*[[L4D2 Vscripts]]
* [https://web.archive.org/web/20170524163422/http://forums.steampowered.com/forums/showthread.php?t=1238461 It's the VScripting documentation FAQ! (Steam Forums)]
*[[Trigger_finale|trigger_finale]]
* [http://leeland.stores.yahoo.net/l4d2-scripting.html Scripting for Left 4 Dead 2 (Leeland.net)] - Example uses of logic_script
*[[Info_director|info_director]]
*[[Left_4_Dead_2_Tool_Updates|Left 4 Dead 2 Tool Updates]]


==External links==
<!-- for cats not added by {{this is a}} template -->
*[http://forums.steampowered.com/forums/showthread.php?t=1238461 It's the vscript'ing documentation FAQ! (Steam Forums)]
[[Category:Mapbase entities]]
*[http://forums.steampowered.com/forums/showthread.php?t=1242468 Tutorial - Writing a Mini Game - Tic Tac Toe - Part One (Steam Forums)] - An excellent scripting example that uses logic_script to create a tic-tac-toe game with a difficult AI.
[[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