Left 4 Dead 2/Scripting: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
(Merged DirectorOptions into a new page.)
(Rewrote the introduction to hopefully be more accessible)
Line 1: Line 1:
{{toc-right}}
{{toc-right}}
{{sq}}{{l4d2}} '''Left 4 Dead 2 [[VScript|vscripts]]''' are Squirrel language-based scripts that can be run in-game. For examples, please see '''[[L4D2 Vscript Examples]]'''.
{{sq}}{{l4d2}} '''Left 4 Dead 2 [[VScript|VScripts]]''' are server-side scripts that are run in an in-game scripting engine. They are written in [http://squirrel-lang.org/ Squirrel], a compilable scripting language similar to [http://www.lua.org/ Lua].  


== Description ==
Left 4 Dead 2 vscripts are written in [http://squirrel-lang.org/ Squirrel], a compilable scripting language similar to [http://www.lua.org/ Lua]. The file extensions are .nut and .nuc, where .nuc denotes encryption of plain text .nut.


=== Usage ===
== Uses ==
*Director manipulation - onslaughts, CI wanderer options, complete emptiness/silence, [[L4D2_Level_Design/Boss_Prohibition|prohibition of boss infected]] (tanks and witches), direction of mobs, specific spawning behavior, etc.
 
*Finale - Custom sequences of events, Gauntlet and Scavenge Logic
=== [[L4D2_Director_Scripts|Director Scripts]] ===
*Complex entity scripting - entity I/O, logic, arithmetic, loops, counters, timers, prop spawning, etc. (Example: Dark Carnival mini-games)
The most common use of [[VScript|VScripts]] in Left 4 Dead 2 is to influence the behavior of the AI Director. These scripts can range from simple adjustments of infected spawning and [[L4D2_Level_Design/Boss_Prohibition|prohibiting boss infected]], to custom events like onslaughts and gauntlets, and even complex staged panic events and fully [[L4D2_Level_Design/Custom_Finale|custom finales]]. Most of the events in the official campaigns are mainly implemented this way.
*Game modes such as Mutations and Scavenge
 
*Miscellaneous!
Director Scripts work mainly by writing overriding values of the various variables used by the Director into a <code>DirectorOptions</code> table.
 
Only one Director Script can be running at a time. Executing a new one will terminate any previous running one and remove any values it set in <code>DirectorOptions</code>.
 
=== [[#Entity Scripts|Entity Scripts]] ===
Another common use is to attach a script to an entity. The script provides easy access to read and modify many of the entity's properties, and even to write new [[KeyValues]]. This allows for controlling entities in ways that would be very complicated or impossible to with the entity I/O system in Hammer.
 
Any entity is capable of running a script, and has the ability to set a specified ''think'' function to run every 0.1 seconds, as well as executing script code as an entity output.
 
Some entities also have specialized functions for VScripts, with the most prominent being [[point_script_use_target]], which allows for turning other entities into fully programmable timed buttons. Other examples are [[logic_script]], that is designed specifically to run VScripts, as well as [[point_template]] and [[env_entity_maker]].
 
=== [[#Global Scripts|Global Scripts]] ===
Scripts can also be made to run globally based on map and game mode. These scripts are commonly used to create scripting and modify global Director options for [[Mutation_Gametype_(L4D2)|mutations]].
 
Please see Valve's [[L4D2_EMS|Expanded Mutation System tutorial]] for more information.
 
 
 
== Script files ==
The scripts are loaded from text files with the file extensions <code>.nut</code> and <code>.nuc</code>, where <code>.nuc</code> denotes encryption of plain text <code>.nut</code>. Custom scripts are read from '''\left 4 dead 2\left4dead2\scripts\vscripts\''', as well as '''\scripts\vscripts\''' when packed into a .vpk file.


=== Location ===
=== Location ===
Line 32: Line 49:


[[#External_links|Packages with deciphered official scripts are also available.]]
[[#External_links|Packages with deciphered official scripts are also available.]]


== Loading vscripts ==
== Loading vscripts ==
*Entities
**Any entity is capable of loading a script on map spawn.
**The vscript can be reloaded with console command <code>ent_fire <name of entity> runscriptfile <relative vscript path></code>. This is useful for quick script reloading.
**Script functions can be called with entity inputs like <code>RunScriptCode</code>.
**Features '''thinkfunction''', a [[Keyvalue|keyvalue]] that calls a user-defined function every 0.1 seconds. While it has the potential to become expensive, a programmer is able to limit the amount of code executed. The shoot gallery in Dark Carnival relies on this.
**Some functions are specialized for certain entity classes, such as [[point_template]] and [[env_entity_maker]].
**Also available is [[logic_script]], an entity that registers multiple entities as an array EntityGroup.
*[[info_director]]
**Loads DirectorOptions such as onslaughts, panic events, infected limits, etc.
**Custom DirectorOptions are used frequently to tweak director behavior that best suits the environment. c1_mall_ambient.nut is one such example.
**Uses script scope <code>DirectorScript</code>
{{note|Scripts used with <code>ScriptedPanicEvent</code> will not work if they are in a subdirectory, even though you can use subdirectories in other script contexts. They must reside under the vscripts folder, or they will simply act as a 1 stage 1 second delay.}}
*Game Modes
**Mode Specific Script
***Will automatically run the script with the same name as the [[Mutation_Gametype_(L4D2)|game mode]].
***For example, running a map <code>map <map name> mutation12</code> will automatically load the Realism Versus script, mutation12.nuc.
***Uses script scope <code>g_ModeScript</code>
***{{note|Adding a script for a mode will enable Scripted Mode on it. This works on the base modes as well, and will enable [[L4D2_EMS|EMS]] [[L4D2_EMS/Appendix:_Game_Events|game event callbacks]] and hooks. {{todo|Does this have any other consequences?}} }}
**Map Specific Script
***Per-map scripts can be created that run when the map is loaded in the specified game mode.
***The script naming syntax is <map name>_<mode name>.nut.
***For example, c1m4_atrium_mutation12.nut.
***Uses script scope <code>g_MapScript</code>


*Finale
=== Director Scripts ===
**Loaded via [[trigger_finale]], either automatically or pointing to a specific vscript.
 
**Automatically loads <map name>_finale.nut script on finale start.
;Using the [[info_director]] entity using the following inputs:
**[[L4D2 Level Design/Scavenge Finale|Scavenge]], [[L4D2 Level Design/Custom Finale|Custom]], and [[L4D2 Level Design/Gauntlet Finale|Gauntlet]] finales are dependent on vscripts.
:<code>BeginScript <''script name''></code>
**Uses script scope <code>DirectorScript</code>
::Executes a generic Director script, for example for onslaught events or changing spawning behavior.
:<code>EndScript</code>
::Ends the running script and resets the Director options to the map specific values.
:<code>BeginScriptedPanicEvent <''script name''></code>
::Begins a Scripted Panic event.
 
{{note|Scripts used with <code>BeginScriptedPanicEvent</code> will not work if they are in a subdirectory, even though you can use subdirectories in other script contexts. They must reside under the vscripts folder, or they will simply act as a 1 stage 1 second delay.}}
 
;Finale scripts
:Automatically loads the <code><map name>_finale.nut</code> script on map start. The script is executed when the [[trigger_finale]] is used, either manually by the player, or with the <code>ForceFinaleStart</code> input.
:{{todo|[[trigger_finale]] also has an option to specify a finale script, but it doesn't seem to work}}
 
 
All Director Scripts are placed in the script scope <code>DirectorScript</code>
 
=== Entity Scripts ===
;Automatic loading
:Uses the script set in the '''Entity Scripts''' KeyValue.
;Manual loading
:Using the <code>RunScriptFile</code> input.
 
Entity Scripts are placed in the script scope <code>_<unique ID>_<entity name></code>
 
=== Global Scripts ===
;Mode Specific Scripts
:Will automatically run the script with the same name as the [[Mutation_Gametype_(L4D2)|game mode]]. For example, running a map <code>map <map name> mutation12</code> will automatically load the Realism Versus script, <code>mutation12.nuc</code>.
:They are placed in the script scope <code>g_ModeScript</code>
 
{{note|Adding a script for a mode will enable Scripted Mode on it. This works on the base modes as well, and will enable [[L4D2_EMS|EMS]] [[L4D2_EMS/Appendix:_Game_Events|game event callbacks]] and hooks. {{todo|Does this have any other consequences?}} }}
 
;Map Specific Scripts
:Per-map scripts can be created that run when the map is loaded in the specified game mode, using the syntax <code><map name>_<mode name>.nut</code>, for example, <code>c1m4_atrium_mutation12.nut</code>.
:They are placed in the script scope <code>g_MapScript</code>
 
 


== Example scripts ==
== Example scripts ==
Line 112: Line 141:


;DirectorOptions
;DirectorOptions
:<code>DirectorScript.DirectorOptions < DirectorScript.MapScript.DirectorOptions < DirectorScript.MapScript.LocalScript.DirectorOptions < DirectorScript.MapScript.ChallengeScript.DirectorOptions</code>
:<code>DirectorScript.DirectorOptions < g_MapScript.DirectorOptions < g_MapScript.LocalScript.DirectorOptions < g_ModeScript.DirectorOptions</code>


== Director scripts ==


'''Main Article: [[L4D2 Director Scripts]]'''


{{todo|Add a short description of director scripts.}}


=== Director options ===
== Entity Scripts ==
{{merge|L4D2 Entity Scripts}}
Uses script scope <code>_<unique ID>_<entity name></code>


'''Please see [[L4D2 Director Scripts#DirectorOptions|L4D2 Director Scripts]] for a table of available options.'''
{{todo}}


== Mode and map scripts ==
*Any entity is capable of loading a script on map spawn.
*The vscript can be reloaded with console command <code>ent_fire <name of entity> runscriptfile <relative vscript path></code>. This is useful for quick script reloading.
*Script functions can be called with entity inputs like <code>RunScriptCode</code>.
*Features '''thinkfunction''', a [[Keyvalue|keyvalue]] that calls a user-defined function every 0.1 seconds. While it has the potential to become expensive, a programmer is able to limit the amount of code executed. The shoot gallery in Dark Carnival relies on this.
*Some functions are specialized for certain entity classes, such as [[point_template]] and [[env_entity_maker]].
*Also available is [[logic_script]], an entity that registers multiple entities as an array EntityGroup.


== Global Scripts ==
{{merge|L4D2 Global Scripts}}
{{todo}}
{{todo}}
=== Mode Scripts ===
Uses script scope <code>g_ModeScript</code>
{{note|Adding a script for a mode will enable Scripted Mode on it. This works on the base modes as well, and will enable [[L4D2_EMS|EMS]] [[L4D2_EMS/Appendix:_Game_Events|game event callbacks]] and hooks. {{todo|Does this have any other consequences?}} }}
=== Map Scripts ===
Uses script scope <code>g_MapScript</code>
== Director options ==
'''Please see [[L4D2 Director Scripts#DirectorOptions|L4D2 Director Scripts]] for a table of available options.'''


== Third party tools ==
== Third party tools ==

Revision as of 04:30, 7 May 2014

SquirrelLeft 4 Dead 2 Left 4 Dead 2 VScripts are server-side scripts that are run in an in-game scripting engine. They are written in Squirrel, a compilable scripting language similar to Lua.


Uses

Director Scripts

The most common use of VScripts in Left 4 Dead 2 is to influence the behavior of the AI Director. These scripts can range from simple adjustments of infected spawning and prohibiting boss infected, to custom events like onslaughts and gauntlets, and even complex staged panic events and fully custom finales. Most of the events in the official campaigns are mainly implemented this way.

Director Scripts work mainly by writing overriding values of the various variables used by the Director into a DirectorOptions table.

Only one Director Script can be running at a time. Executing a new one will terminate any previous running one and remove any values it set in DirectorOptions.

Entity Scripts

Another common use is to attach a script to an entity. The script provides easy access to read and modify many of the entity's properties, and even to write new KeyValues. This allows for controlling entities in ways that would be very complicated or impossible to with the entity I/O system in Hammer.

Any entity is capable of running a script, and has the ability to set a specified think function to run every 0.1 seconds, as well as executing script code as an entity output.

Some entities also have specialized functions for VScripts, with the most prominent being point_script_use_target, which allows for turning other entities into fully programmable timed buttons. Other examples are logic_script, that is designed specifically to run VScripts, as well as point_template and env_entity_maker.

Global Scripts

Scripts can also be made to run globally based on map and game mode. These scripts are commonly used to create scripting and modify global Director options for mutations.

Please see Valve's Expanded Mutation System tutorial for more information.


Script files

The scripts are loaded from text files with the file extensions .nut and .nuc, where .nuc denotes encryption of plain text .nut. Custom scripts are read from \left 4 dead 2\left4dead2\scripts\vscripts\, as well as \scripts\vscripts\ when packed into a .vpk file.

Location

Official .nuc script files are located in scripts/vscripts in multiple locations
Note.pngNote:Browse and extract VPK files with third-party programs like GCFScape.
  • left 4 dead 2\left4dead2\pak01_dir.vpk
  • left 4 dead 2\left4dead2_dlc1\pak01_dir.vpk
April 22, 2010 The Passing update
  • left 4 dead 2\left4dead2_dlc2\pak01_dir.vpk
October 5, 2010 The Sacrifice update
  • left 4 dead 2\left4dead2_dlc3\pak01_dir.vpk
March 22, 2011 Cold Stream Beta / L4D1 Transition Project update
  • left 4 dead 2\update\pak01_dir.vpk
This is where mutations were updated/replaced bi-weekly.
  • left 4 dead 2\sdk_content\scripting\scripts\vscripts\
Plaintext versions of many of the scripts introduced in the EMS update.


Decrypting NUC files

.nuc files are ICE encrypted .nut files. The encryption key is SDhfi878. You can use VICE to decode them.

Packages with deciphered official scripts are also available.


Loading vscripts

Director Scripts

Using the info_director entity using the following inputs
BeginScript <script name>
Executes a generic Director script, for example for onslaught events or changing spawning behavior.
EndScript
Ends the running script and resets the Director options to the map specific values.
BeginScriptedPanicEvent <script name>
Begins a Scripted Panic event.
Note.pngNote:Scripts used with BeginScriptedPanicEvent will not work if they are in a subdirectory, even though you can use subdirectories in other script contexts. They must reside under the vscripts folder, or they will simply act as a 1 stage 1 second delay.
Finale scripts
Automatically loads the <map name>_finale.nut script on map start. The script is executed when the trigger_finale is used, either manually by the player, or with the ForceFinaleStart input.
Todo: trigger_finale also has an option to specify a finale script, but it doesn't seem to work


All Director Scripts are placed in the script scope DirectorScript

Entity Scripts

Automatic loading
Uses the script set in the Entity Scripts KeyValue.
Manual loading
Using the RunScriptFile input.

Entity Scripts are placed in the script scope _<unique ID>_<entity name>

Global Scripts

Mode Specific Scripts
Will automatically run the script with the same name as the game mode. For example, running a map map <map name> mutation12 will automatically load the Realism Versus script, mutation12.nuc.
They are placed in the script scope g_ModeScript
Note.pngNote:Adding a script for a mode will enable Scripted Mode on it. This works on the base modes as well, and will enable EMS game event callbacks and hooks.
Todo: Does this have any other consequences?
Map Specific Scripts
Per-map scripts can be created that run when the map is loaded in the specified game mode, using the syntax <map name>_<mode name>.nut, for example, c1m4_atrium_mutation12.nut.
They are placed in the script scope g_MapScript


Example scripts

Please see L4D2 Vscript Examples.

Scripting environment

Please see List of L4D2 Script Functions for built in classes and functions.

Todo: Add better description of the script scopes.

Table structure

DirectorScript = 			//Director scripts get loaded here.
{
	DirectorOptions 		//Active when a director script is, and during scripted events and finales(?).
	MapScript =  			//Reference to g_MapScript. Contains initial values.
	{
		BaseScriptedDOTable 	//Hardcoded DirectorOptions.
		ChallengeScript = 	//Reference to g_ModeScript. Contains initial values.
		{
			MutationState 	//Populated with the the mode and map names.
		}
		LocalScript =
		{
			DirectorOptions //Current DirectorOptions(?).
		}
	}
}
g_MapScript = 				//Map specific scripts get loaded here.
{
	MapOptions 			//Initial values for SessionOptions.
	MapState 			//Initial values for SessionState.
}
g_ModeScript = 				//Mode specific scripts get loaded here (Scripted mode only).
{
	DirectorOptions 		//Current Director options, outside scripted events.
	MutationState 			//Initial values for SessionState.
	MutationOptions 		//Initial values for SessionOptions.
}
g_rr
g_RoundState
SessionOptions 				//Global Director options (Scripted mode only).
SessionState  				//State variables for game modes (Scripted mode only).


Delegation

Some of the tables have delegation set up, so that if a key isn't present in it, any operation done to done to the value, including creating the slot, is done to its parent-table instead.

DirectorOptions
DirectorScript.DirectorOptions < g_MapScript.DirectorOptions < g_MapScript.LocalScript.DirectorOptions < g_ModeScript.DirectorOptions



Entity Scripts

Merge-arrows.png
It has been suggested that this article or section be merged with L4D2 Entity Scripts. (Discuss)

Uses script scope _<unique ID>_<entity name>

[Todo]

  • Any entity is capable of loading a script on map spawn.
  • The vscript can be reloaded with console command ent_fire <name of entity> runscriptfile <relative vscript path>. This is useful for quick script reloading.
  • Script functions can be called with entity inputs like RunScriptCode.
  • Features thinkfunction, a keyvalue that calls a user-defined function every 0.1 seconds. While it has the potential to become expensive, a programmer is able to limit the amount of code executed. The shoot gallery in Dark Carnival relies on this.
  • Some functions are specialized for certain entity classes, such as point_template and env_entity_maker.
  • Also available is logic_script, an entity that registers multiple entities as an array EntityGroup.

Global Scripts

Merge-arrows.png
It has been suggested that this article or section be merged with L4D2 Global Scripts. (Discuss)

[Todo]


Mode Scripts

Uses script scope g_ModeScript

Note.pngNote:Adding a script for a mode will enable Scripted Mode on it. This works on the base modes as well, and will enable EMS game event callbacks and hooks.
Todo: Does this have any other consequences?


Map Scripts

Uses script scope g_MapScript


Director options

Please see L4D2 Director Scripts for a table of available options.


Third party tools

Todo: Add info about VSLib

See also

External links

Alternative Documentation
Deciphered Official Scripts
Squirrel+Left 4 Dead 2 = Pnkhrt-16px.png