Difference between revisions of "Vscript Fundamentals:zh-cn"

From Valve Developer Community
Jump to: navigation, search
Line 24: Line 24:
A common VScript feature is to augment the features of entities using Entity Scripts.  
Adding a script to the <code>vscripts</code> (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 script scope made up of an unique identifier followed by the entity name or class name; <code>_<unique ID>_<entity name></code>, placed in the root table. If no script has been run on the entity, a script scope can be manually created by using the <code>CBaseEntity::ValidateScriptScope()</code> method.
添加一个脚本到一个服务器端实体的<code>vscripts</code> (实体脚本)键值将使得该脚本作为实体脚本被加载. 该脚本将在实体产生后被自动执行, 并加载到一个脚本域内,该脚本域由唯一标识符+实体名称或种类名组成; <code>_<unique ID>_<entity name></code>, 并被放在根表中. 如果一个实体没有任何脚本被执行, 可以通过 <code>CBaseEntity::ValidateScriptScope()</code> 方法来手动创建一个脚本域.
{{note|Source 2 has both public and private script scopes. Entity Scripts get loaded into the private script scope.}}
{{注意|起源2 有公共脚本域和私有脚本域之分. 实体脚本会被加载到私有脚本域之中.}}
Additional scripts can be loaded by specifying multiple scripts in the <code>vscripts</code> keyvalue, or using the <code>RunScriptFile</code> Input. All scripts that are run on the same entity will load into the same script scope, overwriting any identical variables and functions.
可以通过在<code>vscripts</code> 键值中指定多个脚本来加载其他脚本, 或者使用 <code>RunScriptFile</code> 输入. 在同一实体上运行的所有脚本将加载到相同的脚本范围, 覆盖任何相同的变量和函数.
A think function can be set with the <code>thinkfunction</code> [[Keyvalue|KeyValue]] or by the <code>AddThinkToEnt()</code> function, 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 inputs <code>RunScriptCode ''function_name(argument, ...)''</code> or <code>RunScriptFunction ''function_name''</code>.
Think函数可以被设置于<code>thinkfunction</code> [[Keyvalue|键值]]中或者由 <code>AddThinkToEnt()</code> 函数来添加, 被指定的函数会以0.1秒一次的频率被调用. 函数也可以通过I/O系统的输入<code>RunScriptCode ''function_name(argument, ...)''</code><code>RunScriptFunction ''function_name''</code>来手动调用。
An Entity Script has a <code>self</code> (Source 1) or <code>thisEntity</code> (Source 2) reference to the script handle of the entity owning it, allowing the script easy access to control the entity through its class methods.
An Entity Script has a <code>self</code> (Source 1) or <code>thisEntity</code> (Source 2) reference to the script handle of the entity owning it, allowing the script easy access to control the entity through its class methods.

Revision as of 03:24, 26 March 2019


本页面内容由Dazai Nerau译自英文版页面. 欢迎任何人补充新内容或者修改其中的错误.



脚本环境由关联数组或者构成 , 彼此之间互相嵌套.

脚本被加载时, 它就会被放置在一个被称为它的脚本域的表中, 然后脚本中的任何代码都能够被执行. 执行完代码后,所有的变量、函数以及类都会被保存在脚本域中.


与游戏中的实体进行交互是经由 脚本句柄 得以实现的, 脚本句柄是一种用于引用特定实体的对象. 脚本句柄包含了访问器(accessor)和修改器(mutator)方法以读取和修改实体的属性. 方法的可用性取决于游戏和实体的类型. 请阅读各种游戏的脚本的API参考以了解详情。



详阅文章Entity Scripts


添加一个脚本到一个服务器端实体的vscripts (实体脚本)键值将使得该脚本作为实体脚本被加载. 该脚本将在实体产生后被自动执行, 并加载到一个脚本域内,该脚本域由唯一标识符+实体名称或种类名组成; _<unique ID>_<entity name>, 并被放在根表中. 如果一个实体没有任何脚本被执行, 可以通过 CBaseEntity::ValidateScriptScope() 方法来手动创建一个脚本域.

注意:起源2 有公共脚本域和私有脚本域之分. 实体脚本会被加载到私有脚本域之中.

可以通过在vscripts 键值中指定多个脚本来加载其他脚本, 或者使用 RunScriptFile 输入. 在同一实体上运行的所有脚本将加载到相同的脚本范围, 覆盖任何相同的变量和函数.

Think函数可以被设置于thinkfunction 键值中或者由 AddThinkToEnt() 函数来添加, 被指定的函数会以0.1秒一次的频率被调用. 函数也可以通过I/O系统的输入RunScriptCode function_name(argument, ...)RunScriptFunction function_name来手动调用。

An Entity Script has a self (Source 1) or thisEntity (Source 2) reference to the script handle of the entity owning it, allowing the script easy access to control the entity through its class methods.


Entities have the ability to call functions in their script scope from the C++ side. Common entity classes have predefined function calls programmed into them to occur at certain events, allowing scripts to execute code. For example, creating a function called Precache() in an entity script will call that function right after the entity spawns, allowing the script to precache any custom assets. These functions do not need to be registered, and are always called if if one with the right name exist. Please see the API documentation for your game to find out what hook functions are available for each class.

To do: Does Source 2 implement this?



If available in the game API, scripts can use the EntFire() and DoEntFire() functions to fire outputs to map entities. More functions may be available depending on game.

If arguments for activator and caller are available in the functions, they take a script handle and can be used to fire an output to an entity using the "!self" or "!activator" keyword, even without having to know its name, as long as the handle is available.

Arbitrary VScript code can be run from the I/O system, using the RunScriptCode input available in all entities. The code will run in the calling entities script scope. 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 RunScriptCode.

Example Squirrel code:

// Sets the health of the entity owning the script scope to 500.
DoEntFire( "!self", "SetHealth", "500", 0, self, self )


Using the CBaseEntity::ConnectOutput(string output, string function) method, an entity Output can be connected to a function in the script scope of the entity.

For the duration of the function call, the variables activator and caller are set to the handles of the activating and calling entities, allowing for example for an easy way to find which player triggered something.

Bug: In <Left 4 Dead 2>, these variables are not set correctly.

Example Squirrel code:

// Lights a prop on fire when it's used by the player.
function IgniteSelf()
	DoEntFire( "!self", "Ignite", "", 0, self, self )

// Connects the OnPlayerUse output to the above function.
self.ConnectOutput( "OnPlayerUse", "IgniteSelf" )


When an entity receives an input, the game code will attempt to call a script function of the format Input<Name of Input>() in the receiving Entity Script. If the function returns false, the input is prevented from firing.

Note:The input name is case sensitive, and uses the CamelCase format.

As with connected output functions, the variables activator and caller are set to the handles of the activating and calling entities.

Note:Unlike for output functions, these are set correctly in <Left 4 Dead 2>.

Example Squirrel code:

// The script of a door or button. Intercepts the Unlock input, 
// and doesn't allow the door/button to unlock until 5 inputs have been received.

UnlockCounter <- 5 // Counter local to the entity script scope.

// Called when the Unlock input is received.
function InputUnlock()
	if( UnlockCounter <= 0 )
		return true // Allows the unlock
	return false // Discards the input


Entity handle
An opaque entity reference passing a C++ EHANDLE. Can only be compared with other handles or passed to API functions expecting them. Only used rarely.
Script handle
An entity instance with accessors and mutators to the C++ entity object. Represented as a HSCRIPT typedef in C++ code.
Script scope
Execution context of a script. A table where the variables, functions and classes of a VScript are placed.


List of L4D2 Script Functions

List of Portal 2 Script Functions

List of CS:GO Script Functions

List of Contagion Script Functions

Dota 2 Scripting API