Zh/Left 4 Dead 2/Scripting: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
(Created page with "{{subst:#if: Translation of 'Left 4 Dead 2/Scripting' to '中文' via Template:LanguageBar buttons * * * * * * * * * * * * * * * * * * * * * * * * * * * *...")
 
mNo edit summary
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
{{subst:#if:|||{{LAuto/t}}
{{LanguageBar|title=脚本}}
 
--- DON'T JUST BLINDLY DELETE THIS PART. DO REPLACE THE LINKS AND CATEGORIES. THE PICTURE SHOWS HOW TO USE IT ! ---
 
SEARCH FOR:
\[\[(?!#|File(?:[ _]talk)?:|Image(?:[ _]talk)?:|Media:|Template(?:[ _]talk)?:|MediaWiki(?:[ _]talk)?:|Talk:|Category[ _]talk:|Project[ _]talk:|Valve[ _]Developer[ _]Community[ _]talk:|Help[ _]talk:|User(?:[ _]talk)?:|c:|commons:|Dictionary:|Google:|GoogleGroups:|IMDB:|M:|Meta:|Metawikipedia:|MW:|SdkBug:|SourceForge:|Steampowered:|W:|Wiki:|WikiBooks:|Wikipedia:|Wikiquote:|Wiktionary:|WP:)(:?(?:Category|Category|Help|Project|Valve[ _]Developer[ _]Community|Special|)(?:[^\|\]]+))(\|?.*?)\]\]
 
REPLACE WITH:
{{subst:LAuto|$1$2}}
 
}}{{wip}}{{translating}}
{{LanguageBar}}
{{TabsBar|main=Scripting}}
{{TabsBar|main=Scripting}}
{{back|Left 4 Dead 2/Docs}}
{{back|Zh/Left 4 Dead 2/Docs|回到求生之路2/文档}}
{{toc-right}}
{{toc-right}}
此页面由[[User:世凪Yonagi|世凪Yonagi]]个人汉化,欢迎大家添加新内容或修复错误。
<hr>
== 有用链接 ==
* {{L|/Game Events|游戏事件列表}}
* {{L|/Script Functions|脚本函数列表}}
* {{L|/Director Scripts|导演脚本}}
* {{L|/Expanded Mutation System|扩展突变系统}}
* {{L|/Mutation Gametype|突变游戏类型}}
* {{L|/Decrypted mutations|解密突变}}


== Useful links ==
== 概述 ==
* [[/Game Events|List of game events]]
{{sq}}{{l4d2}} '''Left 4 Dead 2 {{L|VScripts}}''' 是在游戏内虚拟机中运行的服务器端脚本。它们使用 {{L|Squirrel}} 编写,这是一种类似于 [http://www.lua.org/ Lua] 的编译型脚本语言。当前运行的是 Squirrel 3.0.4 版本。
* [[/Script Functions|List of script functions]]
* [[/Director Scripts|Director Scripts]]
* [[/Expanded Mutation System|Expanded Mutation System]]
* [[/Mutation Gametype|Mutation Gametype]]
* [[/Decrypted mutations|Decrypted mutations]]
 
== Overview ==
{{sq}}{{l4d2}} '''Left 4 Dead 2 [[VScript]]s''' are server-side scripts that are run in an in-game virtual machine. They are written in [[Squirrel]], a compiled scripting language similar to [http://www.lua.org/ Lua]. It currently runs Squirrel version 3.0.4.


== Uses ==
== 用途 ==


=== [[L4D2_Director_Scripts|Director Scripts]] ===
=== {{L|L4D2_Director_Scripts|导演脚本}} ===
The most common use of [[VScript]]s in ''Left 4 Dead 2'' is to temporarily 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.
在《Left 4 Dead 2》中,{{L|VScript}} 最常见的用途是临时影响 AI 导演的行为。这些脚本的范围可以从简单的特感生成调整和 {{L|L4D2_Level_Design/Boss_Prohibition|禁止BOSS特感}},到自定义事件如猛攻和考验事件,甚至复杂的分阶段恐慌事件和完整的 {{L|L4D2_Level_Design/Custom_Finale|自定义结局}}。官方战役中的大多数事件主要是通过这种方式实现的。


Director Scripts work mainly by writing overriding values of the various variables used by the Director into a <code>DirectorOptions</code> table.
导演脚本主要通过将导演使用的各种变量的覆盖值写入一个 <code>DirectorOptions</code> 表来工作。


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>.
一次只能运行一个导演脚本。执行新的脚本将终止任何先前运行的脚本,并移除它在 <code>DirectorOptions</code> 中设置的任何值。


=== [[#Entity Scripts 2|Entity Scripts ↓]] ===
=== [[#Entity Scripts 2|实体脚本 ↓]] ===
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.
另一个常见用途是将脚本附加到实体上。该脚本提供了读取和修改实体许多属性的便捷方法,甚至可以编写新的 {{L|KeyValues}}。这使得能够以在 Hammer 中使用实体 I/O 系统非常复杂或不可能的方式控制实体。


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.
任何实体都能够运行脚本,并且能够设置一个指定的 ''think'' 函数每 0.1 秒运行一次,以及作为实体输出执行脚本代码。


Some entities also have specialized functions for VScripts, with the most prominent being {{ent|point_script_use_target}}, which allows for turning other entities into fully programmable timed buttons.
一些实体还具有针对 VScript 的专用函数,其中最突出的是 {{ent|point_script_use_target}},它允许将其他实体变成完全可编程的定时按钮。


=== [[#Global Scripts 2|Global Scripts ↓]] ===
=== [[#Global Scripts 2|全局脚本 ↓]] ===
Scripts can also be made to run on map load, based on game mode and optionally map name. These scripts are commonly used to create pure script addons and modify global Director options for [[Mutation_Gametype_(L4D2)|mutations]].
脚本也可以设置为在地图加载时运行,基于游戏模式并可选择地图名称。这些脚本通常用于创建纯脚本插件和为 {{L|Mutation_Gametype_(L4D2)|突变}} 修改全局导演选项。


Global scripts can have [[Left 4 Dead 2/Script Functions#Scripted_Mode_hooks|script hook functions]] added that get called from the game at certain events, like when players/objects take damage.
全局脚本可以添加 {{L|Left 4 Dead 2/Script Functions#Scripted_Mode_hooks|脚本钩子函数}},这些函数在特定事件(如玩家/对象受到伤害时)从游戏中被调用。


There are many utility functions and features readily available for these scripts, including a resource and building system, and custom panic wave spawning. Please see Valve's [[L4D2_EMS|Expanded Mutation System tutorial]] for more information.
这些脚本有许多现成的实用函数和功能,包括资源和建筑系统,以及自定义恐慌波生成。请参阅 Valve 的 {{L|L4D2_EMS|扩展突变系统教程}} 以获取更多信息。


=== Other Uses ===
=== 其他用途 ===
All scripts can access functions for many features, including [[L4D2_EMS/Appendix:_Spawning|spawning entities]] either from precompiled lists or programmatically, [[L4D2_EMS/Appendix:_Spawning_Infected|spawning infected]], a [[L4D2_EMS/Appendix:_HUD|HUD system]], [[L4D2_EMS/Appendix:_Table_Save_Restore|storing data]] across levels and to disk, and much more.
所有脚本都可以访问许多功能的函数,包括 {{L|L4D2_EMS/Appendix:_Spawning|生成实体}}(从预编译列表或编程方式)、{{L|L4D2_EMS/Appendix:_Spawning_Infected|生成特感}}、{{L|L4D2_EMS/Appendix:_HUD|HUD 系统}}、跨关卡和磁盘{{L|L4D2_EMS/Appendix:_Table_Save_Restore|存储数据}}等等。


Please see [[L4D2 Vscript Examples]] for more examples and ideas.
请参阅 {{L|L4D2 Vscript Examples}} 以获取更多示例和想法。


== 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 plaintext <code>.nut</code>. Custom scripts are read from '''\left 4 dead 2\left4dead2\scripts\vscripts\''', as well as '''\scripts\vscripts\''' when packed into a <code>.vpk</code> file.
脚本从文件扩展名为 <code>.nut</code> <code>.nuc</code> 的文本文件加载,其中 <code>.nuc</code> 表示对纯文本 <code>.nut</code> 的加密。自定义脚本从 '''\left 4 dead 2\left4dead2\scripts\vscripts\''' 读取,以及在打包到 <code>.vpk</code> 文件时从 '''\scripts\vscripts\''' 读取。


=== Location ===
=== 位置 ===
;Official <code>.nuc</code> script files are located in '''scripts/vscripts''' in multiple locations
;官方的 <code>.nuc</code> 脚本文件位于多个位置的 '''scripts/vscripts''' 目录中
{{note|Browse and extract [[VPK]] files with third-party programs like [[GCFScape]].}}
{{note|使用 {{L|GCFScape}} 等第三方程序浏览和提取 {{L|VPK}} 文件。}}
*'''left 4 dead 2\left4dead2\pak01_dir.vpk'''
*'''left 4 dead 2\left4dead2\pak01_dir.vpk'''
;*'''left 4 dead 2\left4dead2_dlc1\pak01_dir.vpk'''
;*'''left 4 dead 2\left4dead2_dlc1\pak01_dir.vpk'''
:April 22, 2010 The Passing update
:2010年4月22日 The Passing 更新
;*'''left 4 dead 2\left4dead2_dlc2\pak01_dir.vpk'''
;*'''left 4 dead 2\left4dead2_dlc2\pak01_dir.vpk'''
:October 5, 2010 The Sacrifice update
:2010年10月5日 The Sacrifice 更新
;*'''left 4 dead 2\left4dead2_dlc3\pak01_dir.vpk'''
;*'''left 4 dead 2\left4dead2_dlc3\pak01_dir.vpk'''
:March 22, 2011 Cold Stream Beta / L4D1 Transition Project update
:2011年3月22日 Cold Stream Beta / L4D1 过渡项目更新
;*'''left 4 dead 2\update\pak01_dir.vpk'''
;*'''left 4 dead 2\update\pak01_dir.vpk'''
:This is where mutations were updated/replaced bi-weekly. Also contain files introduced with September 24, 2020 The Last Stand Community Update.
:这是突变每两周更新/替换的地方。也包含 2020年9月24日 The Last Stand 社区更新引入的文件。
;*'''left 4 dead 2\sdk_content\scripting\scripts\vscripts\'''
;*'''left 4 dead 2\sdk_content\scripting\scripts\vscripts\'''
:Plaintext versions of many of the scripts introduced in the EMS update.
:EMS 更新中引入的许多脚本的纯文本版本。


=== Decrypting NUC Files ===
=== 解密 NUC 文件 ===
.nuc files are ICE encrypted .nut files. The encryption key is '''SDhfi878'''. You can use [[VICE]] to decode them.<br>
.nuc 文件是 ICE 加密的 .nut 文件。加密密钥是 '''SDhfi878'''。你可以使用 {{L|VICE}} 来解码它们。<br>
You may find decrypted mutation vscripts [[L4D2_Decrypted_mutations|here]].
你可以在 {{L|L4D2_Decrypted_mutations|这里}} 找到解密的突变 vscripts。


== Loading VScripts ==
== 加载 VScripts ==
VScripts are loaded in a variety of ways, depending on their context. They can also be manually loaded with the console command <code>script_execute ''filename''</code>.
VScripts 有多种加载方式,取决于它们的上下文。它们也可以使用控制台命令 <code>script_execute ''文件名''</code> 手动加载。


=== Director Scripts ===
=== 导演脚本 ===
;*{{ent|info_director}} inputs
;*{{ent|info_director}} 输入
{{I|BeginScript|param=scriptlist|Executes generic Director scripts, whether for onslaught events or changing spawning behavior.}}
{{I|BeginScript|param=scriptlist|执行通用导演脚本,无论是用于猛攻事件还是改变生成行为。}}
:They are placed in the <code>LocalScript</code> script scope.
:它们被放置在 <code>LocalScript</code> 脚本作用域中。
{{I|EndScript|Ends the currently running local script, then resets the Director options to either map-specific values, or default values.}}
{{I|EndScript|结束当前运行的本地脚本,然后将导演选项重置为地图特定值或默认值。}}
{{I|ScriptedPanicEvent|param=scriptlist|With the given scripts, set up the structure of a [[L4D2 Director Scripts#Finales and Scripted Panic Events|scripted panic event]].}}
{{I|ScriptedPanicEvent|param=scriptlist|使用给定的脚本,设置 {{L|L4D2 Director Scripts#Finales and Scripted Panic Events|脚本化恐慌事件}} 的结构。}}
:They are placed in the <code>LocalScript</code> script scope.
:它们被放置在 <code>LocalScript</code> 脚本作用域中。


{{note|The scripts of these inputs ''must'' reside under the "vscripts" subfolder. Events will play out only the 1 second DELAY "stage 0" otherwise.}}
{{note|这些输入的脚本''必须''位于 "vscripts" 子文件夹下。否则事件将只播放 1 秒延迟的"阶段 0"}}


;*[[L4D2 Director Scripts#Finales and Scripted Panic Events|Finale scripts]]
;*{{subst:LAuto|L4D2 Director Scripts#Finales and Scripted Panic Events|结局脚本]]
:As a {{ent|trigger_finale}} finale starts, it loads a script of this format: <code>[mapname]_finale.nut</code>.
:{{ent|trigger_finale}} 结局开始时,它会加载此格式的脚本:<code>[地图名]_finale.nut</code>
:They are placed in the <code>g_MapScript</code> script scope.
:它们被放置在 <code>g_MapScript</code> 脚本作用域中。


=== Entity Scripts ===
=== 实体脚本 ===
;*Keyvalue
;*键值
{{KV|Entity Scripts|intn=vscripts|scriptlist|As an entity is preparing itself before it spawns, the VScript path names in that keyvalue will be ran. (See [[L4D2_Vscripts#Entity_Scripts_3|Entity Scripts]] for more info.)}}
{{KV|Entity Scripts|intn=vscripts|scriptlist|在实体生成前准备自身时,将运行该键值中的 VScript 路径名称。(更多信息请参见 {{L|L4D2_Vscripts#Entity_Scripts_3|实体脚本}}。)}}
;*Input
;*输入
{{I|RunScriptFile|param=script|Inserts the given script into the entity's scope, then runs it.}}
{{I|RunScriptFile|param=script|将给定脚本插入实体的作用域,然后运行它。}}


Entity Scripts are placed in the <code>_<unique ID>_<entity name></code> script scope.
实体脚本被放置在 <code>_<唯一ID>_<实体名称></code> 脚本作用域中。


=== Global Scripts ===
=== 全局脚本 ===
;*Mode scripts
;*模式脚本
:A script with the same name as the current [[Mutation_Gametype_(L4D2)|game mode]]. Loaded when a new mode is set.
:与当前 {{subst:LAuto|Mutation_Gametype_(L4D2)|游戏模式]] 同名的脚本。在设置新模式时加载。
:They are placed in the <code>g_ModeScript</code> script scope.
:它们被放置在 <code>g_ModeScript</code> 脚本作用域中。
:{{tip|The <code>map</code> command can also set gamemodes. For example, <code>map <map name> mutation12</code> loads the Realism Versus mode script, <code>mutation12.nuc</code>.}}
:{{tip|<code>map</code> 命令也可以设置游戏模式。例如,<code>map <地图名> mutation12</code> 加载写实对抗模式脚本 <code>mutation12.nuc</code>}}
:{{note|Adding a script for a mode will enable Scripted Mode.}}
:{{note|为模式添加脚本将启用脚本模式。}}


;*Map-specific mode scripts
;*特定地图的模式脚本
:Per-map scripts when the map is loaded in the specified game mode. They are in the name format: ''<nowiki>[mapname]_[modename].nut</nowiki>''.
:当地图在指定游戏模式下加载时的每地图脚本。它们的名称格式为:''<nowiki>[地图名]_[模式名].nut</nowiki>''
:They are placed in the <code>g_MapScript</code> script scope.
:它们被放置在 <code>g_MapScript</code> 脚本作用域中。


;*Addon scripts
;*插件脚本
:These scripts vary in context, but they all share one big attribute: they don't override files with the same name, but instead load alongside them. They are executed in the order below, each after its respective base script (without <code>_addon</code> suffix). A script is considered an "addon script" if it's named as:
:这些脚本的上下文各不相同,但它们都有一个共同的大属性:它们不会覆盖同名的文件,而是与它们一起加载。它们按以下顺序执行,每个都在其各自的基础脚本(不带 <code>_addon</code> 后缀)之后执行。如果脚本被命名为以下之一,则被视为"插件脚本"
:# <code>mapspawn_addon.nut</code> (runs once every chapter) - <code>getroottable()</code> scope
:# <code>mapspawn_addon.nut</code>(每章节运行一次)- <code>getroottable()</code> 作用域
:# <code>response_testbed_addon.nut</code> (runs once every chapter) - <code>g_rr</code> scope
:# <code>response_testbed_addon.nut</code>(每章节运行一次)- <code>g_rr</code> 作用域
:# <code>scriptedmode_addon.nut</code> (runs twice every round) - <code>g_MapScript</code> scope
:# <code>scriptedmode_addon.nut</code>(每回合运行两次)- <code>g_MapScript</code> 作用域
:# <code>director_base_addon.nut</code> (runs twice every round) - <code>DirectorScript</code> scope
:# <code>director_base_addon.nut</code>(每回合运行两次)- <code>DirectorScript</code> 作用域


== Scripting Environment ==
== 脚本环境 ==
'''Please see [[Left 4 Dead 2/Script Functions]] for built in classes and functions.'''  
'''请参阅 {{L|Left 4 Dead 2/Script Functions}} 了解内置类和函数。'''


When a script is loaded, it is placed into a table, or Script Scope. Global and Director Scripts are put into set scopes, while a unique scope is generated for each Entity Script. Please see [[Vscript Fundamentals]] for more information.
当脚本加载时,它被放置在一个表或脚本作用域中。全局脚本和导演脚本被放入设定的作用域,而为每个实体脚本生成唯一的作用域。更多信息请参阅 {{L|Vscript Fundamentals}}。


=== Table Structure ===
=== 表结构 ===
<source lang=cpp>
<source lang=cpp>
DirectorScript = // Base Director Scope.
DirectorScript = // 基础导演作用域。
{
{
DirectorOptions // Base DirectorOptions table.
DirectorOptions // 基础 DirectorOptions 表。
MapScript =  // Script scope for either map scripts or trigger_finale scripts
MapScript =  // 地图脚本或 trigger_finale 脚本的作用域
{
{
DirectorOptions         
DirectorOptions         
BaseScriptedDOTable // Default DirectorOptions for Scripted mode
BaseScriptedDOTable // 脚本模式的默认 DirectorOptions
ChallengeScript = // Script scope for mode scripts
ChallengeScript = // 模式脚本的作用域
{
{
DirectorOptions // Filled with BaseScriptedDOTable, MutationOptions and MapOptions; if identical keys are present then the most-right table gets precedence.
DirectorOptions // 使用 BaseScriptedDOTable、MutationOptions 和 MapOptions 填充;如果存在相同的键,则最右边的表优先。
MutationState // Filled with MapState overwriting previous values
MutationState // 使用 MapState 覆盖先前值填充
MutationOptions // Initial values used if defined in mode script
MutationOptions // 如果在模式脚本中定义则使用初始值
}
}
LocalScript = // Script scope for scripts executed through info_director 'BeginScript' or 'ScriptedPanicEvent' inputs
LocalScript = // 通过 info_director 'BeginScript' 'ScriptedPanicEvent' 输入执行的脚本的作用域
{
{
DirectorOptions // DirectorOptions for current Director Script (like onslaughts). Only available when a script is active
DirectorOptions // 当前导演脚本的 DirectorOptions(如猛攻)。仅在脚本活动时可用
}
}
MapOptions // Initial values used if defined in map script
MapOptions // 如果在地图脚本中定义则使用初始值
MapState // Initial values used if defined in map script
MapState // 如果在地图脚本中定义则使用初始值
}
}
}
}
g_MapScript // Global reference to the Map Script scope (DirectorScript.MapScript).
g_MapScript // 对地图脚本作用域 (DirectorScript.MapScript) 的全局引用。
g_ModeScript // Global reference to the Mode Script scope (DirectorScript.MapScript.ChallengeScript).
g_ModeScript // 对模式脚本作用域 (DirectorScript.MapScript.ChallengeScript) 的全局引用。
SessionOptions // Global reference to g_ModeScript.DirectorOptions (Scripted mode only).
SessionOptions // g_ModeScript.DirectorOptions 的全局引用(仅限脚本模式)。
SessionState  // Global reference to g_ModeScript.MutationState (Scripted mode only).
SessionState  // g_ModeScript.MutationState 的全局引用(仅限脚本模式)。


g_rr // Scope holding the Response Rule system.
g_rr // 持有响应规则系统的作用域。
g_RoundState // TODO
g_RoundState // 待办
</source>
</source>


=== Delegation ===
=== 委托 ===
Some of the tables have [http://squirrel-lang.org/doc/squirrel3.html#d0e2038 delegation] set up, so that if a key isn't present in it, it is read from its delegate (usually parent) table instead.
一些表设置了 [http://squirrel-lang.org/doc/squirrel3.html#d0e2038 委托],因此如果键不在其中,则从其委托(通常是父)表中读取。


The tables are delegated like this (delegates on the right):
这些表的委托关系如下(委托在右侧):
; The Director Scope
; 导演作用域
: <code>(DirectorScript.MapScript.ChallengeScript or DirectorScript.MapScript.LocalScript) < DirectorScript.MapScript < DirectorScript < ::</code>
: <code>(DirectorScript.MapScript.ChallengeScript DirectorScript.MapScript.LocalScript) < DirectorScript.MapScript < DirectorScript < ::</code>
; DirectorOptions
; DirectorOptions
: <code>DirectorScript.MapScript.ChallengeScript.DirectorOptions (Scripted mode only) < DirectorScript.MapScript.LocalScript.DirectorOptions (when Director Script active) < DirectorScript.MapScript.DirectorOptions < DirectorScript.DirectorOptions</code>
: <code>DirectorScript.MapScript.ChallengeScript.DirectorOptions (仅限脚本模式) < DirectorScript.MapScript.LocalScript.DirectorOptions (当导演脚本活动时) < DirectorScript.MapScript.DirectorOptions < DirectorScript.DirectorOptions</code>
: See GetDirectorOptions in {{ent|director_base.nut}}.
: 请参阅 {{ent|director_base.nut}} 中的 GetDirectorOptions。


== Entity Scripts ==
== 实体脚本 ==
Use script scope <code>_<unique ID>_<entity name></code>
使用脚本作用域 <code>_<唯一 ID>_<实体名称></code>


Adding a script to the '''Entity Scripts''' KeyValue of a server-side entity loads the script as an Entity Script. The script is executed when the entity spawns, and loads into a unique script scope made up of an unique identifier followed by the entity name or class name.
将脚本添加到服务器端实体的 '''实体脚本''' 键值中,会将脚本作为实体脚本加载。脚本在实体生成时执行,并加载到一个由唯一标识符后跟实体名称或类名组成的唯一脚本作用域中。


A think function can be set with the <code>thinkfunction</code> [[Keyvalue|KeyValue]], the specified script function every 0.1 seconds. While it has the potential to become expensive, a programmer is able to limit the amount of code executed. Functions can also be manually called through the I/O system with the input <code>RunScriptCode ''function_name(argument, …)</code>.
可以使用 <code>thinkfunction</code> {{L|Keyvalue|键值}} 设置一个 think 函数,指定的脚本函数每 0.1 秒运行一次。虽然它有变得昂贵的潜力,但程序员能够限制执行的代码量。函数也可以通过 I/O 系统使用输入 <code>RunScriptCode ''函数名(参数, …)</code> 手动调用。


Entity Scripts have a <code>self</code> reference to their owning entity handle, allowing the script easy access to control the entity through its class methods. There are also hook functions available depending on the entity class. Both methods and hooks are documented [[Left 4 Dead 2/Script Functions#Classes|here]].
实体脚本有一个 <code>self</code> 引用指向其拥有的实体句柄,允许脚本通过其类方法轻松控制实体。根据实体类,还有可用的钩子函数。两种方法和钩子都在 {{L|Left 4 Dead 2/Script Functions#Classes|这里}} 有文档记录。


Some entities have additional script functionality:
一些实体具有额外的脚本功能:
*{{ent|point_script_use_target}} - Has both methods and hooks for controlling the button.
*{{ent|point_script_use_target}} - 具有控制按钮的方法和钩子。
*{{ent|logic_script}} - Can pass multiple entity names to the script as an array called <code>EntityGroup</code>.
*{{ent|logic_script}} - 可以将多个实体名称作为名为 <code>EntityGroup</code> 的数组传递给脚本。
*{{ent|point_template}}
*{{ent|point_template}}
*{{ent|env_entity_maker}}
*{{ent|env_entity_maker}}
*{{ent|info_item_position}}
*{{ent|info_item_position}}


The script can be reloaded with console command <code>ent_fire <name of entity> runscriptfile ''<relative vscript path>''</code>. This is useful for quick script reloading.
可以使用控制台命令 <code>ent_fire <实体名> runscriptfile ''<相对 vscript 路径>''</code> 重新加载脚本。这对于快速重新加载脚本很有用。


=== [[Inputs_and_Outputs|I/O system]] Interaction ===
=== {{L|Inputs_and_Outputs|I/O 系统}} 交互 ===
Any script can use the <code>EntFire()</code> and <code>DoEntFire()</code> functions to fire outputs to map entities. Since the ''<code>activator</code>'' and ''<code>caller</code>'' arguments in <code>DoEntFire()</code> take a script handle, it can be used to fire an output to an entity using the {{ent|!self}} or {{ent|!activator}} keyword, even without having to know its name, as long as the entity handle is available.
任何脚本都可以使用 <code>EntFire()</code> <code>DoEntFire()</code> 函数向地图实体触发输出。由于 <code>DoEntFire()</code> 中的 ''<code>activator</code>'' 和 ''<code>caller</code>'' 参数接受脚本句柄,因此可以使用它来使用 {{ent|!self}} {{ent|!activator}} 关键字向实体触发输出,即使不必知道其名称,只要实体句柄可用。


<source lang=cpp>
<source lang=cpp>
EntFire( "church_bell_relay", "Trigger", 0 ); // Fires an output to the Trigger input of the named entity.
EntFire( "church_bell_relay", "Trigger", 0 ); // 向命名实体的 Trigger 输入触发输出。


player <- null;
player <- null;
while(player = Entities.FindByClassname(player, "player"))  // Iterate through the script handles of the players.
while(player = Entities.FindByClassname(player, "player"))  // 遍历玩家的脚本句柄。
{
{
     DoEntFire("!self", "speakresponseconcept", "PlayerLaugh", 0, null, player); // Make each player laugh.
     DoEntFire("!self", "speakresponseconcept", "PlayerLaugh", 0, null, player); // 让每个玩家大笑。
}
}
</source>
</source>


Conversely, the <code>[[Left 4 Dead 2/Script Functions#CBaseEntity|CBaseEntity]]::ConnectOutput()</code> and <code>DisconnectOutput()</code> functions can be used to call a script function when the specified entity output fires.
相反,<code>{{L|Left 4 Dead 2/Script Functions#CBaseEntity|CBaseEntity}}::ConnectOutput()</code> <code>DisconnectOutput()</code> 函数可用于在指定的实体输出触发时调用脚本函数。
In addition, arbitrary VScript code can be run from the I/O system, using the <code>RunScriptCode</code> input available in all entities. The code will run in the current entities script scope.
此外,可以使用所有实体中可用的 <code>RunScriptCode</code> 输入从 I/O 系统运行任意 VScript 代码。代码将在当前实体的脚本作用域中运行。
{{warning|Never use double-quotation marks in any Hammer Output, since it will corrupt the map file. This means that strings cannot be passed with <code>RunScriptCode</code>.}}
{{warning|切勿在任何 Hammer 输出中使用双引号,因为它会损坏地图文件。这意味着无法使用 <code>RunScriptCode</code> 传递字符串。}}


== Global Scripts ==
== 全局脚本 ==
Naming a script file with a game mode name makes it a mode specific script which executes every time a map is loaded in the specified mode. If a Mode Script exists, a map specific script with the map name followed by an underscore and the mode name can also be loaded.
将脚本文件命名为游戏模式名称会使其成为模式特定脚本,每次在指定模式下加载地图时都会执行。如果存在模式脚本,则也可以加载后跟下划线和模式名的地图特定脚本。


=== Mode Scripts ===
=== 模式脚本 ===
Use script scope <code>g_ModeScript</code>
使用脚本作用域 <code>g_ModeScript</code>


==== Tables ====
==== ====
;<code>MutationState</code>
;<code>MutationState</code>
:Initial values for the <code>SessionState</code> table.
:<code>SessionState</code> 表的初始值。
;<code>MutationOptions</code>
;<code>MutationOptions</code>
:Initial values for the <code>SessionOptions</code> table.
:<code>SessionOptions</code> 表的初始值。


=== Map Scripts ===
=== 地图脚本 ===
Use script scope <code>g_MapScript</code>
使用脚本作用域 <code>g_MapScript</code>


==== Tables ====
==== ====
;<code>MapState</code>
;<code>MapState</code>
:Map specific values for the <code>SessionState</code> table.
:<code>SessionState</code> 表的地图特定值。
;<code>MapOptions</code>
;<code>MapOptions</code>
:Map specific values for the <code>SessionOptions</code> table.
:<code>SessionOptions</code> 表的地图特定值。


=== Scripted Mode ===
=== 脚本模式 ===
Adding a Mode Script will enable Scripted Mode for the game mode. This works on the base game modes included in the game as well.
添加模式脚本将为该游戏模式启用脚本模式。这也适用于游戏中包含的基础游戏模式。


Scripted Mode loads utility functions into the <code>g_MapScript</code> scope, disables the hardcoded setting of 2 on the <code>MaxSpecials</code> Director option, and enables [[L4D2_EMS/Appendix:_Game_Events|game event callbacks]] and [[Left 4 Dead 2/Script Functions#Scripted_Mode_hooks|script hook functions]]. It also seems to break the finale tank music in Dark Carnival.
脚本模式将实用函数加载到 <code>g_MapScript</code> 作用域中,禁用 <code>MaxSpecials</code> 导演选项上硬编码的 2 值,并启用 {{L|L4D2_EMS/Appendix:_Game_Events|游戏事件回调}} 和 {{L|Left 4 Dead 2/Script Functions#Scripted_Mode_hooks|脚本钩子函数}}。它似乎还会破坏 Dark Carnival 中的结局坦克音乐。


{{todo|Does enabling Scripted Mode have any other consequences?}}
{{todo|启用脚本模式还有其他后果吗?}}


==== Available Functions ====
==== 可用函数 ====
{{todo|Document the available utility features.}}
{{todo|记录可用的实用功能。}}


=== Shared Tables ===
=== 共享表 ===
;<code>SessionState</code>
;<code>SessionState</code>
:A table for storing session specific variables. Not preserved across level transitions.
:用于存储会话特定变量的表。不跨关卡过渡保留。
;<code>SessionOptions</code>
;<code>SessionOptions</code>
:Active [[L4D2_Director_Scripts#DirectorOptions|Director Options]] for the game mode.
:游戏模式的活跃 {{L|L4D2_Director_Scripts#DirectorOptions|导演选项}}。


== Glossary ==
== 术语表 ==
;DirectorOptions
;DirectorOptions
:A table of named variables that override the AI Director behavior.
:一个命名变量表,用于覆盖 AI 导演行为。
;Entity handle
;实体句柄
:Also known as EHANDLE. {{todo|Looks like some sort of pointer reference to an entity.}}
:也称为 EHANDLE。{{todo|看起来像是某种指向实体的指针引用。}}
;Script handle
;脚本句柄
:An entity instance with accessors and mutators to the C++ entity object. Also known as HScript.
:具有对 C++ 实体对象的访问器和修改器的实体实例。也称为 HScript。
;Script scope
;脚本作用域
:The table where the variables, functions and classes of a VScript are placed.
:放置 VScript 的变量、函数和类的表。


== Director Options ==
== 导演选项 ==
'''Please see [[L4D2 Director Scripts#DirectorOptions|L4D2 Director Scripts]] for a table of available options.'''
'''请参阅 {{L|L4D2 Director Scripts#DirectorOptions|L4D2 导演脚本}} 了解可用选项表。'''


== Third-Party Tools ==
== 第三方工具 ==


===VSLib===
===VSLib===
'''VSLib''' (''VS''cript ''Lib''rary) is a simple, lightweight library for L4D2's VScript mutation system. It greatly simplifies coding VScripts by taking care of a lot of tedious work for you. It is used in several popular mods like [http://steamcommunity.com/sharedfiles/filedetails/?id=214630948 Rayman1103's Admin System] and [http://steamcommunity.com/sharedfiles/filedetails/?id=157525096 Stranded]. Read more about [[VSLib]].
'''VSLib'''''V''Script ''Lib''rary)是一个用于 L4D2 的 VScript 突变系统的简单、轻量级库。它通过处理许多繁琐的工作,极大地简化了 VScript 的编码。它用于几个流行的模组中,如 [http://steamcommunity.com/sharedfiles/filedetails/?id=214630948 Rayman1103 的管理员系统] [http://steamcommunity.com/sharedfiles/filedetails/?id=157525096 Stranded]。阅读更多关于 {{L|VSLib}} 的信息。


== See also ==
== 另请参阅 ==
=== Intros ===
=== 介绍 ===
*[[VScript]]
*{{L|VScript}}
*[[Mutation Gametype (L4D2)]]
*{{L|Mutation Gametype (L4D2)}}
=== Documentations ===
=== 文档 ===
*[[VSLib]]
*{{L|VSLib}}
*[[L4D2 Vscript Examples]]
*{{L|L4D2 Vscript Examples}}
*[[L4D2_Director_Scripts|L4D2 Director Scripts]]
*{{L|L4D2_Director_Scripts|L4D2 导演脚本}}
*[[Left 4 Dead 2/Script Functions|L4D2 Script Functions]]
*{{L|Left 4 Dead 2/Script Functions|L4D2 脚本函数}}
*[[L4D2_Decrypted_mutations|Decrypted Mutation Vscripts]]
*{{L|L4D2_Decrypted_mutations|解密突变 Vscripts}}
*[[L4D2_Level_Design/Boss_Prohibition|L4D2 Level Design/Boss Prohibition]]
*{{L|L4D2_Level_Design/Boss_Prohibition|L4D2 关卡设计/BOSS 禁止}}
*[[L4D2_Level_Design/Custom_Finale|Custom Finales]] ''Contains all original finale scripts as reference.''
*{{L|L4D2_Level_Design/Custom_Finale|自定义结局}} ''包含所有原始结局脚本作为参考。''
*[[L4D2 EMS|Extended Mutation System]]
*{{L|L4D2 EMS|扩展突变系统}}
*[[Left_4_Dead_2_Tool_Updates|Left 4 Dead 2 Tool Updates]]
*{{L|Left_4_Dead_2_Tool_Updates|Left 4 Dead 2 工具更新}}


=== Miscelleanous ===
=== 杂项 ===
*{{ent|logic_script}}
*{{ent|logic_script}}
*{{ent|trigger_finale}}
*{{ent|trigger_finale}}
*{{ent|info_director}}
*{{ent|info_director}}


== External links ==
== 外部链接 ==
;Alternative Tool
;替代工具
* [http://www.mediafire.com/?q4dqbs554p494vk (mediafire)L4D2 VScript Editor Beta 0.5 by Cynick:support nut (de)compile for l4d2]  
* [http://www.mediafire.com/?q4dqbs554p494vk (mediafire) L4D2 VScript 编辑器 Beta 0.5 by Cynick:支持 nut ()编译 for l4d2]  
;Alternative Documentation
;替代文档
* [http://forums.steampowered.com/forums/showthread.php?t=1128303 Director Scripts - .nuc files (Steam forums)] {{deadlink}}
* [http://forums.steampowered.com/forums/showthread.php?t=1128303 导演脚本 - .nuc 文件 (Steam 论坛)] {{deadlink}}
* [http://forums.steampowered.com/forums/showthread.php?t=1238461 It's the vscript'ing documentation FAQ! (Steam forums)] {{deadlink}}
* [http://forums.steampowered.com/forums/showthread.php?t=1238461 这是 vscript'ing 文档 FAQ! (Steam 论坛)] {{deadlink}}
* [http://forums.steampowered.com/forums/showthread.php?t=1242468 Tutorial - Writing a Mini Game - Tic Tac Toe - Part One (Steam Forums)] {{deadlink}}
* [http://forums.steampowered.com/forums/showthread.php?t=1242468 教程 - 编写一个小游戏 - 井字棋 - 第一部分 (Steam 论坛)] {{deadlink}}
**[http://leeland.stores.yahoo.net/l4d2-scripts-play-tic-tac-toe.html Writing a Mini Game - Tic Tac Toe - Part One] - Author's Website {{deadlink}}
**[http://leeland.stores.yahoo.net/l4d2-scripts-play-tic-tac-toe.html 编写一个小游戏 - 井字棋 - 第一部分] - 作者网站 {{deadlink}}
**[http://www.youtube.com/watch?v=B9hASsoHK54 l4d2 - Vscript example - Tic-Tac-Toe] - Video of early Prototype
**[http://www.youtube.com/watch?v=B9hASsoHK54 l4d2 - Vscript 示例 - 井字棋] - 早期原型视频
**[http://www.youtube.com/watch?v=mvDFEoA0ib0 l4d2 - Vscript example - Tic-Tac-Toe - updated] - Video of current version with "brutally misanthropic AI"
**[http://www.youtube.com/watch?v=mvDFEoA0ib0 l4d2 - Vscript 示例 - 井字棋 - 更新] - 当前版本视频,带有"残忍厌世 AI"
* [http://forums.steampowered.com/forums/showthread.php?t=1238004 Mutation scripts (Steam forums)] {{deadlink}}
* [http://forums.steampowered.com/forums/showthread.php?t=1238004 突变脚本 (Steam 论坛)] {{deadlink}}
* [http://modulatum.wordpress.com/2008/03/20/squirrel-binary-for-windows/ Squirrel Binary for Windows]
* [http://modulatum.wordpress.com/2008/03/20/squirrel-binary-for-windows/ Squirrel Windows 二进制文件]
* [http://en.wikipedia.org/wiki/Squirrel_(programming_language) Squirrel (programming language)] - Wikipedia Article on Squirrel
* [http://en.wikipedia.org/wiki/Squirrel_(programming_language) Squirrel (编程语言)] - Wikipedia 关于 Squirrel 的文章
* {{sq}}[http://squirrel-lang.org/ Squirrel: The Programming Language - Documentation and Sample Code]
* {{sq}}[http://squirrel-lang.org/ Squirrel: 编程语言 - 文档和示例代码]
* [https://steamcdn-a.akamaihd.net/apps/valve/2009/ai_systems_of_l4d_mike_booth.pdf The AI Systems of Left 4 Dead by Michael Booth (PDF)]
* [https://steamcdn-a.akamaihd.net/apps/valve/2009/ai_systems_of_l4d_mike_booth.pdf Left 4 Dead 的 AI 系统 by Michael Booth (PDF)]
* {{as}}[http://swarmarmory.com/forums/viewtopic.php?f=19&t=541 "Creating a "Money"/Point System"] - Swarm Armory
* {{as}}[http://swarmarmory.com/forums/viewtopic.php?f=19&t=541 "创建"金钱"/点数系统"] - Swarm Armory
;Deciphered Official Scripts:
;解密的官方脚本:
*[http://www.mediafire.com/?k280fhwjsp21sn8 Version 2.1.3.6 and all mutations]
*[http://www.mediafire.com/?k280fhwjsp21sn8 版本 2.1.3.6 和所有突变]
*[http://ata4.info/downloads/left4dead2/scriptsrc.zip Version 2.0.8.4 (August 16, 2011)]
*[http://ata4.info/downloads/left4dead2/scriptsrc.zip 版本 2.0.8.4 (2011年8月16日)]
<div style="text-align: right; direction: ltr; margin-left: 1em;">{{sq}}+ {{l4d2}} = [[File:PinkHeartCensor.png|16px]]</div>
<div style="text-align: right; direction: ltr; margin-left: 1em;">{{sq}}+ {{l4d2}} = [[File:PinkHeartCensor.png|16px]]</div>


[[Category:Left_4_Dead_2]]
{{ACategory|Left 4 Dead 2}}
[[Category:VScript]]
{{ACategory|VScript}}
[[Category:Scripting]]
{{ACategory|Scripting}}

Latest revision as of 07:43, 3 October 2025

English (en)中文 (zh)Translate (Translate)
<(en)
edit
回到求生之路2/文档

此页面由世凪Yonagi个人汉化,欢迎大家添加新内容或修复错误。


有用链接

概述

Squirrel求生之路2 Left 4 Dead 2 VScripts(en) 是在游戏内虚拟机中运行的服务器端脚本。它们使用 Squirrel(en) 编写,这是一种类似于 Lua 的编译型脚本语言。当前运行的是 Squirrel 3.0.4 版本。

用途

导演脚本(en)

在《Left 4 Dead 2》中,VScript(en) 最常见的用途是临时影响 AI 导演的行为。这些脚本的范围可以从简单的特感生成调整和 禁止BOSS特感(en),到自定义事件如猛攻和考验事件,甚至复杂的分阶段恐慌事件和完整的 自定义结局(en)。官方战役中的大多数事件主要是通过这种方式实现的。

导演脚本主要通过将导演使用的各种变量的覆盖值写入一个 DirectorOptions 表来工作。

一次只能运行一个导演脚本。执行新的脚本将终止任何先前运行的脚本,并移除它在 DirectorOptions 中设置的任何值。

实体脚本 ↓

另一个常见用途是将脚本附加到实体上。该脚本提供了读取和修改实体许多属性的便捷方法,甚至可以编写新的 KeyValues(en)。这使得能够以在 Hammer 中使用实体 I/O 系统非常复杂或不可能的方式控制实体。

任何实体都能够运行脚本,并且能够设置一个指定的 think 函数每 0.1 秒运行一次,以及作为实体输出执行脚本代码。

一些实体还具有针对 VScript 的专用函数,其中最突出的是 point_script_use_target,它允许将其他实体变成完全可编程的定时按钮。

全局脚本 ↓

脚本也可以设置为在地图加载时运行,基于游戏模式并可选择地图名称。这些脚本通常用于创建纯脚本插件和为 突变(en) 修改全局导演选项。

全局脚本可以添加 脚本钩子函数(en),这些函数在特定事件(如玩家/对象受到伤害时)从游戏中被调用。

这些脚本有许多现成的实用函数和功能,包括资源和建筑系统,以及自定义恐慌波生成。请参阅 Valve 的 扩展突变系统教程(en) 以获取更多信息。

其他用途

所有脚本都可以访问许多功能的函数,包括 生成实体(en)(从预编译列表或编程方式)、生成特感(en)HUD 系统(en)、跨关卡和磁盘存储数据(en)等等。

请参阅 L4D2 Vscript Examples(en) 以获取更多示例和想法。

脚本文件

脚本从文件扩展名为 .nut.nuc 的文本文件加载,其中 .nuc 表示对纯文本 .nut 的加密。自定义脚本从 \left 4 dead 2\left4dead2\scripts\vscripts\ 读取,以及在打包到 .vpk 文件时从 \scripts\vscripts\ 读取。

位置

官方的 .nuc 脚本文件位于多个位置的 scripts/vscripts 目录中
Note.png注意:使用 GCFScape(en) 等第三方程序浏览和提取 VPK(en) 文件。
  • left 4 dead 2\left4dead2\pak01_dir.vpk
  • left 4 dead 2\left4dead2_dlc1\pak01_dir.vpk
2010年4月22日 The Passing 更新
  • left 4 dead 2\left4dead2_dlc2\pak01_dir.vpk
2010年10月5日 The Sacrifice 更新
  • left 4 dead 2\left4dead2_dlc3\pak01_dir.vpk
2011年3月22日 Cold Stream Beta / L4D1 过渡项目更新
  • left 4 dead 2\update\pak01_dir.vpk
这是突变每两周更新/替换的地方。也包含 2020年9月24日 The Last Stand 社区更新引入的文件。
  • left 4 dead 2\sdk_content\scripting\scripts\vscripts\
EMS 更新中引入的许多脚本的纯文本版本。

解密 NUC 文件

.nuc 文件是 ICE 加密的 .nut 文件。加密密钥是 SDhfi878。你可以使用 VICE(en) 来解码它们。
你可以在 这里(en) 找到解密的突变 vscripts。

加载 VScripts

VScripts 有多种加载方式,取决于它们的上下文。它们也可以使用控制台命令 script_execute 文件名 手动加载。

导演脚本

BeginScript <脚本列表(en)>
执行通用导演脚本,无论是用于猛攻事件还是改变生成行为。
它们被放置在 LocalScript 脚本作用域中。
EndScript
结束当前运行的本地脚本,然后将导演选项重置为地图特定值或默认值。
ScriptedPanicEvent <脚本列表(en)>
使用给定的脚本,设置 脚本化恐慌事件(en) 的结构。
它们被放置在 LocalScript 脚本作用域中。
Note.png注意:这些输入的脚本必须位于 "vscripts" 子文件夹下。否则事件将只播放 1 秒延迟的"阶段 0"。
{{subst
  • LAuto|L4D2 Director Scripts#Finales and Scripted Panic Events|结局脚本]]
trigger_finale 结局开始时,它会加载此格式的脚本:[地图名]_finale.nut
它们被放置在 g_MapScript 脚本作用域中。

实体脚本

  • 键值
Entity Scripts (vscripts) <脚本列表(en)>
在实体生成前准备自身时,将运行该键值中的 VScript 路径名称。(更多信息请参见 实体脚本(en)。)
  • 输入
RunScriptFile <脚本(en)>
将给定脚本插入实体的作用域,然后运行它。

实体脚本被放置在 _<唯一ID>_<实体名称> 脚本作用域中。

全局脚本

  • 模式脚本
与当前 {{subst:LAuto|Mutation_Gametype_(L4D2)|游戏模式]] 同名的脚本。在设置新模式时加载。
它们被放置在 g_ModeScript 脚本作用域中。
Tip.png提示:map 命令也可以设置游戏模式。例如,map <地图名> mutation12 加载写实对抗模式脚本 mutation12.nuc
Note.png注意:为模式添加脚本将启用脚本模式。
  • 特定地图的模式脚本
当地图在指定游戏模式下加载时的每地图脚本。它们的名称格式为:[地图名]_[模式名].nut
它们被放置在 g_MapScript 脚本作用域中。
  • 插件脚本
这些脚本的上下文各不相同,但它们都有一个共同的大属性:它们不会覆盖同名的文件,而是与它们一起加载。它们按以下顺序执行,每个都在其各自的基础脚本(不带 _addon 后缀)之后执行。如果脚本被命名为以下之一,则被视为"插件脚本":
  1. mapspawn_addon.nut(每章节运行一次)- getroottable() 作用域
  2. response_testbed_addon.nut(每章节运行一次)- g_rr 作用域
  3. scriptedmode_addon.nut(每回合运行两次)- g_MapScript 作用域
  4. director_base_addon.nut(每回合运行两次)- DirectorScript 作用域

脚本环境

请参阅 Left 4 Dead 2/Script Functions(en) 了解内置类和函数。

当脚本加载时,它被放置在一个表或脚本作用域中。全局脚本和导演脚本被放入设定的作用域,而为每个实体脚本生成唯一的作用域。更多信息请参阅 Vscript Fundamentals(en)

表结构

DirectorScript = 			// 基础导演作用域。
{
	DirectorOptions 		// 基础 DirectorOptions 表。
	MapScript =  			// 地图脚本或 trigger_finale 脚本的作用域
	{
		DirectorOptions         
		BaseScriptedDOTable 	// 脚本模式的默认 DirectorOptions
		ChallengeScript = 	// 模式脚本的作用域
		{
			DirectorOptions // 使用 BaseScriptedDOTable、MutationOptions 和 MapOptions 填充;如果存在相同的键,则最右边的表优先。
			MutationState 	// 使用 MapState 覆盖先前值填充
			MutationOptions // 如果在模式脚本中定义则使用初始值
		}
		LocalScript =		// 通过 info_director 'BeginScript' 或 'ScriptedPanicEvent' 输入执行的脚本的作用域
		{
			DirectorOptions // 当前导演脚本的 DirectorOptions(如猛攻)。仅在脚本活动时可用
		}
		MapOptions 		// 如果在地图脚本中定义则使用初始值
		MapState 		// 如果在地图脚本中定义则使用初始值
	}
}
g_MapScript 	// 对地图脚本作用域 (DirectorScript.MapScript) 的全局引用。
g_ModeScript 	// 对模式脚本作用域 (DirectorScript.MapScript.ChallengeScript) 的全局引用。
SessionOptions 	// 对 g_ModeScript.DirectorOptions 的全局引用(仅限脚本模式)。
SessionState  	// 对 g_ModeScript.MutationState 的全局引用(仅限脚本模式)。

g_rr		// 持有响应规则系统的作用域。
g_RoundState	// 待办

委托

一些表设置了 委托,因此如果键不在其中,则从其委托(通常是父)表中读取。

这些表的委托关系如下(委托在右侧):

导演作用域
(DirectorScript.MapScript.ChallengeScript 或 DirectorScript.MapScript.LocalScript) < DirectorScript.MapScript < DirectorScript < ::
DirectorOptions
DirectorScript.MapScript.ChallengeScript.DirectorOptions (仅限脚本模式) < DirectorScript.MapScript.LocalScript.DirectorOptions (当导演脚本活动时) < DirectorScript.MapScript.DirectorOptions < DirectorScript.DirectorOptions
请参阅 director_base.nut 中的 GetDirectorOptions。

实体脚本

使用脚本作用域 _<唯一 ID>_<实体名称>

将脚本添加到服务器端实体的 实体脚本 键值中,会将脚本作为实体脚本加载。脚本在实体生成时执行,并加载到一个由唯一标识符后跟实体名称或类名组成的唯一脚本作用域中。

可以使用 thinkfunction 键值(en) 设置一个 think 函数,指定的脚本函数每 0.1 秒运行一次。虽然它有变得昂贵的潜力,但程序员能够限制执行的代码量。函数也可以通过 I/O 系统使用输入 RunScriptCode 函数名(参数, …) 手动调用。

实体脚本有一个 self 引用指向其拥有的实体句柄,允许脚本通过其类方法轻松控制实体。根据实体类,还有可用的钩子函数。两种方法和钩子都在 这里(en) 有文档记录。

一些实体具有额外的脚本功能:

可以使用控制台命令 ent_fire <实体名> runscriptfile <相对 vscript 路径> 重新加载脚本。这对于快速重新加载脚本很有用。

I/O 系统(en) 交互

任何脚本都可以使用 EntFire()DoEntFire() 函数向地图实体触发输出。由于 DoEntFire() 中的 activatorcaller 参数接受脚本句柄,因此可以使用它来使用 !self!activator 关键字向实体触发输出,即使不必知道其名称,只要实体句柄可用。

EntFire( "church_bell_relay", "Trigger", 0 ); // 向命名实体的 Trigger 输入触发输出。

player <- null;
while(player = Entities.FindByClassname(player, "player"))   // 遍历玩家的脚本句柄。
{
    DoEntFire("!self", "speakresponseconcept", "PlayerLaugh", 0, null, player); // 让每个玩家大笑。
}

相反,CBaseEntity(en)::ConnectOutput()DisconnectOutput() 函数可用于在指定的实体输出触发时调用脚本函数。 此外,可以使用所有实体中可用的 RunScriptCode 输入从 I/O 系统运行任意 VScript 代码。代码将在当前实体的脚本作用域中运行。

Warning.png警告:切勿在任何 Hammer 输出中使用双引号,因为它会损坏地图文件。这意味着无法使用 RunScriptCode 传递字符串。

全局脚本

将脚本文件命名为游戏模式名称会使其成为模式特定脚本,每次在指定模式下加载地图时都会执行。如果存在模式脚本,则也可以加载后跟下划线和模式名的地图特定脚本。

模式脚本

使用脚本作用域 g_ModeScript

MutationState
SessionState 表的初始值。
MutationOptions
SessionOptions 表的初始值。

地图脚本

使用脚本作用域 g_MapScript

MapState
SessionState 表的地图特定值。
MapOptions
SessionOptions 表的地图特定值。

脚本模式

添加模式脚本将为该游戏模式启用脚本模式。这也适用于游戏中包含的基础游戏模式。

脚本模式将实用函数加载到 g_MapScript 作用域中,禁用 MaxSpecials 导演选项上硬编码的 2 值,并启用 游戏事件回调(en)脚本钩子函数(en)。它似乎还会破坏 Dark Carnival 中的结局坦克音乐。

待完善: 启用脚本模式还有其他后果吗?

可用函数

待完善: 记录可用的实用功能。

共享表

SessionState
用于存储会话特定变量的表。不跨关卡过渡保留。
SessionOptions
游戏模式的活跃 导演选项(en)

术语表

DirectorOptions
一个命名变量表,用于覆盖 AI 导演行为。
实体句柄
也称为 EHANDLE。
待完善: 看起来像是某种指向实体的指针引用。
脚本句柄
具有对 C++ 实体对象的访问器和修改器的实体实例。也称为 HScript。
脚本作用域
放置 VScript 的变量、函数和类的表。

导演选项

请参阅 L4D2 导演脚本(en) 了解可用选项表。

第三方工具

VSLib

VSLibVScript Library)是一个用于 L4D2 的 VScript 突变系统的简单、轻量级库。它通过处理许多繁琐的工作,极大地简化了 VScript 的编码。它用于几个流行的模组中,如 Rayman1103 的管理员系统Stranded。阅读更多关于 VSLib(en) 的信息。

另请参阅

介绍

文档

杂项

外部链接

替代工具
替代文档
解密的官方脚本
Squirrel+ 求生之路2 = PinkHeartCensor.png