本文与游戏《传送门2》有关。点击这里查看更多信息。
This article relates to "Squirrel". Click here for more information.

Zh/Portal 2/Scripting/Script Functions: Difference between revisions

From Valve Developer Community
< Zh‎ | Portal 2
Jump to navigation Jump to search
No edit summary
Line 275: Line 275:
用于为道具等实体设置动画的脚本句柄类。
用于为道具等实体设置动画的脚本句柄类。


{{LanguageBar|Methods|title=使用方法}}


==== 使用方法 ====
==== 使用方法 ====
{| class="standard-table" style="width: 100%;"
{| class="standard-table" style="width: 100%;"
! Function
! 函数
! Signature
! 签名
! Description
! 描述
|-
|-
| <code>GetAttachmentAngles</code>
| <code>GetAttachmentAngles</code>
| <code>Vector CBaseAnimating::GetAttachmentAngles(int id)</code>
| <code>Vector {{L|CBaseAnimating}}::GetAttachmentAngles(int id)</code>
| Get the attachment id's angles as a p,y,r vector.
| 获取附件ID的角度(p,y,r向量)。
|-
|-
| <code>GetAttachmentOrigin</code>
| <code>GetAttachmentOrigin</code>
| <code>Vector CBaseAnimating::GetAttachmentOrigin(int id)</code>
| <code>Vector {{L|CBaseAnimating}}::GetAttachmentOrigin(int id)</code>
| Get the attachment id's origin vector
| 获取附件ID的原始向量。
|-
|-
| <code>GetObjectScaleLevel</code>
| <code>GetObjectScaleLevel</code>
| <code>int CBaseAnimating::GetObjectScaleLevel()</code>
| <code>int {{L|CBaseAnimating}}::GetObjectScaleLevel()</code>
| The scale size of the entity
| 获取实体的缩放级别。
|-
|-
| <code>IsSequenceFinished</code>
| <code>IsSequenceFinished</code>
| <code>bool CBaseAnimating::IsSequenceFinished()</code>
| <code>bool {{L|CBaseAnimating}}::IsSequenceFinished()</code>
| Ask whether the main sequence is done playing
| 检查主序列是否播放完毕。
|-
|-
| <code>LookupAttachment</code>
| <code>LookupAttachment</code>
| <code>int CBaseAnimating::LookupAttachment(string)</code>
| <code>int {{L|CBaseAnimating}}::LookupAttachment(string)</code>
| Get the ID for the named attachment.
| 获取命名附件的ID。
|-
|-
| <code>SetBodygroup</code>
| <code>SetBodygroup</code>
| <code>void CBaseAnimating::SetBodygroup(int group, int index)</code>
| <code>void {{L|CBaseAnimating}}::SetBodygroup(int group, int index)</code>
| Sets a bodygroup. Group is the index for the desired group, and index is the desired part to use.
| 设置身体组。group是目标组的索引,index是要使用的部位索引。
|}
|}


=== CBaseFlex ===
=== CBaseFlex ===
继承 [[#CBaseAnimating|CBaseAnimating]]
继承 [[#CBaseAnimating|CBaseAnimating]]


==== 使用方法 ====
==== 使用方法 ====
{| class="standard-table" style="width: 100%;"
{| class="standard-table" style="width: 100%;"
! Function
! 函数
! Signature
! 签名
! Description
! 描述
|-
|-
| <code>GetCurrentScene</code>
| <code>GetCurrentScene</code>
| <code>handle CBaseFlex::GetCurrentScene()</code>
| <code>handle {{L|CBaseFlex}}::GetCurrentScene()</code>
| Returns the instance of the oldest active scene entity (if any).
| 返回最早激活的场景实体实例(如果有)。
|-
|-
| <code>GetSceneByIndex</code>
| <code>GetSceneByIndex</code>
| <code>handle CBaseFlex::GetSceneByIndex(int)</code>
| <code>handle {{L|CBaseFlex}}::GetSceneByIndex(int)</code>
| Returns the instance of the scene entity at the specified index.
| 返回指定索引的场景实体实例。
|}
|}


=== CBasePlayer ===
=== CBasePlayer ===
继承 [[#CBaseAnimating|CBaseAnimating]]
继承 [[#CBaseAnimating|CBaseAnimating]]


==== 使用方法 ====
==== 使用方法 ====
{| class="standard-table" style="width: 100%;"
{| class="standard-table" style="width: 100%;"
! Function
! 函数
! Signature
! 签名
! Description
! 描述
|-
|-
| <code>IsNoclipping</code>
| <code>IsNoclipping</code>
| <code>bool CBasePlayer::IsNoclipping()</code>
| <code>bool {{L|CBasePlayer}}::IsNoclipping()</code>
| Returns true if the player is in noclip mode.
| 如果玩家处于穿墙模式则返回true。
|}
|}


Line 344: Line 343:
脚本实例: <code>Entities</code>
脚本实例: <code>Entities</code>


一个用于查找和迭代游戏中实体的脚本句柄的接口。
用于查找和迭代游戏中实体的脚本接口。


要迭代一组实体,请在适当的方法中将<code>null(空)</code>传递给"previous"参数以开始迭代,或引用先前找到的实体以继续搜索。请看以下例子:
要迭代一组实体,请在适当的方法中将<code>null</code>传递给"previous"参数以开始迭代,或引用先前找到的实体以继续搜索。示例:
<source lang=cpp>
<source lang=cpp>
//第一种方式
//第一种方式
local ent = null;    //变量名"ent"是任意的(但也得合法)。
local ent = null;    //变量名"ent"可自定义
while(ent = Entities.FindByName(ent, "entityname"))    //事实上, 我们要用"=",而不是"=="!(判断ent是否有效) 如果"ent"变为null(后面无符合条件的实体),则循环中止。
while(ent = Entities.FindByName(ent, "entityname"))    //注意使用"="而非"=="
{
{
   //...    //在每次迭代中, "ent"都不为null, 运行此处代码。
   //...    //每次迭代时"ent"有效则执行此处代码
}
}


//第二种方式
//第二种方式
for(local ent;ent = Entities.FindByName(ent, "entityname");)    //变量名"ent"是任意的(但也得合法)。事实上, 我们要用"=",而不是"=="!(判断ent是否有效) 如果"ent"变为null(后面无符合条件的实体),则循环中止。(请确保所有分号都放置正确)
for(local ent;ent = Entities.FindByName(ent, "entityname");)    //注意分号位置
{
{
   //...    //在每次迭代中, "ent"都不为null时, 运行此处代码。
   //...    //每次迭代时"ent"有效则执行此处代码
}
}
</source>
</source>


==== 使用方法 ====
==== 使用方法 ====
{| class="standard-table" style="width: 100%;"
{| class="standard-table" style="width: 100%;"
! Function
! 函数
! Signature
! 签名
! Description
! 描述
|-
|-
| <code>CreateByClassname</code>
| <code>CreateByClassname</code>
| <code>handle CEntities::CreateByClassname(string)</code>
| <code>handle {{L|CEntities}}::CreateByClassname(string)</code>
| Creates an entity by classname
| 通过类名创建实体
|-
|-
| <code>FindByClassname</code>
| <code>FindByClassname</code>
| <code>handle CEntities::FindByClassname(handle start_ent, string classname)</code>
| <code>handle {{L|CEntities}}::FindByClassname(handle start_ent, string classname)</code>
| Find entities by class name. Pass 'null' to start an iteration, or reference to a previously found entity to continue a search
| 按类名查找实体。传递'null'开始迭代,或引用先前找到的实体继续搜索
|-
|-
| <code>FindByClassnameNearest</code>
| <code>FindByClassnameNearest</code>
| <code>handle CEntities::FindByClassnameNearest(string classname, Vector loc, float radius)</code>
| <code>handle {{L|CEntities}}::FindByClassnameNearest(string classname, Vector loc, float radius)</code>
| Find entities by class name nearest to a point.
| 查找距离某点最近的指定类名实体
|-
|-
| <code>FindByClassnameWithin</code>
| <code>FindByClassnameWithin</code>
| <code>handle CEntities::FindByClassnameWithin(handle start_ent, string classname, Vector loc, float radius)</code>
| <code>handle {{L|CEntities}}::FindByClassnameWithin(handle start_ent, string classname, Vector loc, float radius)</code>
| Find entities by class name within a radius. Pass 'null' to start an iteration, or reference to a previously found entity to continue a search
| 查找半径内的指定类名实体。传递'null'开始迭代
|-
|-
| <code>FindByModel</code>
| <code>FindByModel</code>
| <code>handle CEntities::FindByModel(handle start_ent, string model)</code>
| <code>handle {{L|CEntities}}::FindByModel(handle start_ent, string model)</code>
| Find entities by model name. Pass 'null' to start an iteration, or reference to a previously found entity to continue a search
| 按模型名查找实体。传递'null'开始迭代
|-
|-
| <code>FindByName</code>
| <code>FindByName</code>
| <code>handle CEntities::FindByName(handle start_ent, string targetname)</code>
| <code>handle {{L|CEntities}}::FindByName(handle start_ent, string targetname)</code>
| Find entities by name (including wildcards). Pass 'null' to start an iteration, or reference to a previously found entity to continue a search.
| 按名称(支持通配符)查找实体。传递'null'开始迭代
|-
|-
| <code>FindByNameNearest</code>
| <code>FindByNameNearest</code>
| <code>handle CEntities::FindByNameNearest(string targetname, Vector loc, float radius)</code>
| <code>handle {{L|CEntities}}::FindByNameNearest(string targetname, Vector loc, float radius)</code>
| Find entities by name nearest to a point.
| 查找距离某点最近的指定名称实体
|-
|-
| <code>FindByNameWithin</code>
| <code>FindByNameWithin</code>
| <code>handle CEntities::FindByNameWithin(handle, string, Vector loc, float radius)</code>
| <code>handle {{L|CEntities}}::FindByNameWithin(handle, string, Vector loc, float radius)</code>
| Find entities by name within a radius. Pass 'null' to start an iteration, or reference to a previously found entity to continue a search
| 查找半径内的指定名称实体。传递'null'开始迭代
|-
|-
| <code>FindByTarget</code>
| <code>FindByTarget</code>
| <code>handle CEntities::FindByTarget(handle start_ent, string targetname)</code>
| <code>handle {{L|CEntities}}::FindByTarget(handle start_ent, string targetname)</code>
| Find entities with a specific <code>target</code> keyvalue. Pass 'null' to start an iteration, or reference to a previously found entity to continue a search
| 查找具有特定<code>target</code>键值的实体。传递'null'开始迭代
|-
|-
| <code>FindInSphere</code>
| <code>FindInSphere</code>
| <code>handle CEntities::FindInSphere(handle start_ent, Vector loc, float radius)</code>
| <code>handle {{L|CEntities}}::FindInSphere(handle start_ent, Vector loc, float radius)</code>
| Find entities within a radius. Pass 'null' to start an iteration, or reference to a previously found entity to continue a search
| 查找半径内的实体。传递'null'开始迭代
|-
|-
| <code>First</code>
| <code>First</code>
| <code>handle CEntities::First()</code>
| <code>handle {{L|CEntities}}::First()</code>
| Begin an iteration over the list of entities. Equivalent to Next(null).
| 开始实体列表迭代。等同于Next(null)
|-
|-
| <code>Next</code>
| <code>Next</code>
| <code>handle CEntities::Next(handle)</code>
| <code>handle {{L|CEntities}}::Next(handle)</code>
| Continue an iteration over the list of entities, providing reference to a previously found entity.
| 继续迭代,需提供先前找到的实体引用
|}
|}


=== CEnvEntityMaker ===
=== CEnvEntityMaker ===
继承 [[#CBaseEntity|CBaseEntity]]
继承 [[#CBaseEntity|CBaseEntity]]


脚本句柄类是{{L|env_entity_maker}}。  
脚本句柄类是{{L|env_entity_maker}}。
 


==== 使用方法 ====
==== 使用方法 ====
{| class="standard-table" style="width: 100%;"
{| class="standard-table" style="width: 100%;"
! Function
! 函数
! Signature
! 签名
! Description
! 描述
|-
|-
| <code>SpawnEntity</code>
| <code>SpawnEntity</code>
| <code>void CEnvEntityMaker::SpawnEntity()</code>
| <code>void {{L|CEnvEntityMaker}}::SpawnEntity()</code>
| Create an entity at the location of the maker
| 在maker位置创建实体
|-
|-
| <code>SpawnEntityAtEntityOrigin</code>
| <code>SpawnEntityAtEntityOrigin</code>
| <code>void CEnvEntityMaker::SpawnEntityAtEntityOrigin(handle)</code>
| <code>void {{L|CEnvEntityMaker}}::SpawnEntityAtEntityOrigin(handle)</code>
| Create an entity at the location of a specified entity instance
| 在指定实体位置创建实体
|-
|-
| <code>SpawnEntityAtLocation</code>
| <code>SpawnEntityAtLocation</code>
| <code>void CEnvEntityMaker::SpawnEntityAtLocation(Vector, Vector)</code>
| <code>void {{L|CEnvEntityMaker}}::SpawnEntityAtLocation(Vector, Vector)</code>
| Create an entity at a specified location and orientaton, orientation is Euler angle in degrees (pitch, yaw, roll)
| 在指定位置和欧拉角(pitch,yaw,roll)创建实体
|-
|-
| <code>SpawnEntityAtNamedEntityOrigin</code>
| <code>SpawnEntityAtNamedEntityOrigin</code>
| <code>void CEnvEntityMaker::SpawnEntityAtNamedEntityOrigin(string)</code>
| <code>void {{L|CEnvEntityMaker}}::SpawnEntityAtNamedEntityOrigin(string)</code>
| Create an entity at the location of a named entity
| 在命名实体位置创建实体
|}
|}


=== CLinkedPortalDoor ===
=== CLinkedPortalDoor ===
{{todo}}
{{todo}}


 
==== 使用方法 ====
==== Methods ====
{| class="standard-table" style="width: 100%;"
{| class="standard-table" style="width: 100%;"
! Function
! 函数
! Signature
! 签名
! Description
! 描述
|-
|-
| <code>GetPartnerInstance</code>
| <code>GetPartnerInstance</code>
| <code>handle CLinkedPortalDoor::GetPartnerInstance()</code>
| <code>handle {{L|CLinkedPortalDoor}}::GetPartnerInstance()</code>
| Get the instance handle of the door's linked partner
| 获取门链接伙伴的实例句柄
|-
|-
| <code>GetPartnername</code>
| <code>GetPartnername</code>
| <code>string CLinkedPortalDoor::GetPartnername()</code>
| <code>string {{L|CLinkedPortalDoor}}::GetPartnername()</code>
| Returns the partnername of the door.
| 返回门的伙伴名称
|}
|}


=== CPlayerVoiceListener ===
=== CPlayerVoiceListener ===
{{todo}}
{{todo}}


 
==== 使用方法 ====
==== Methods ====
{| class="standard-table" style="width: 100%;"
{| class="standard-table" style="width: 100%;"
! Function
! 函数
! Signature
! 签名
! Description
! 描述
|-
|-
| <code>GetPlayerSpeechDuration</code>
| <code>GetPlayerSpeechDuration</code>
| <code>float CPlayerVoiceListener::GetPlayerSpeechDuration(int)</code>
| <code>float {{L|CPlayerVoiceListener}}::GetPlayerSpeechDuration(int)</code>
| Returns the number of seconds the player has been continuously speaking.
| 返回玩家持续说话的秒数
|-
|-
| <code>IsPlayerSpeaking</code>
| <code>IsPlayerSpeaking</code>
| <code>bool CPlayerVoiceListener::IsPlayerSpeaking(int)</code>
| <code>bool {{L|CPlayerVoiceListener}}::IsPlayerSpeaking(int)</code>
| Returns whether the player specified is speaking.
| 返回指定玩家是否正在说话
|}
|}


=== CPortal_Player ===
=== CPortal_Player ===
{{todo}}
{{todo}}


 
==== 使用方法 ====
==== Methods ====
{| class="standard-table" style="width: 100%;"
{| class="standard-table" style="width: 100%;"
! Function
! 函数
! Signature
! 签名
! Description
! 描述
|-
|-
| <code>GetWheatleyMonitorDestructionCount</code>
| <code>GetWheatleyMonitorDestructionCount</code>
| <code>int CPortal_Player::GetWheatleyMonitorDestructionCount()</code>
| <code>int {{L|CPortal_Player}}::GetWheatleyMonitorDestructionCount()</code>
| Get number of wheatley monitors destroyed by the player.
| 获取玩家摧毁Wheatley显示器的数量
|-
|-
| <code>IncWheatleyMonitorDestructionCount</code>
| <code>IncWheatleyMonitorDestructionCount</code>
| <code>void CPortal_Player::IncWheatleyMonitorDestructionCount()</code>
| <code>void {{L|CPortal_Player}}::IncWheatleyMonitorDestructionCount()</code>
| Set number of wheatley monitors destroyed by the player.
| 增加玩家摧毁Wheatley显示器的计数
|-
|-
| <code>TurnOffPotatos</code>
| <code>TurnOffPotatos</code>
| <code>void CPortal_Player::TurnOffPotatos()</code>
| <code>void {{L|CPortal_Player}}::TurnOffPotatos()</code>
| Turns Off the Potatos material light
| 关闭Potatos材质光源
|-
|-
| <code>TurnOnPotatos</code>
| <code>TurnOnPotatos</code>
| <code>void CPortal_Player::TurnOnPotatos()</code>
| <code>void {{L|CPortal_Player}}::TurnOnPotatos()</code>
| Turns On the Potatos material light
| 开启Potatos材质光源
|}
|}


=== CPropLinkedPortalDoor ===
=== CPropLinkedPortalDoor ===
{{todo}}
{{todo}}


 
==== 使用方法 ====
==== Methods ====
{| class="standard-table" style="width: 100%;"
{| class="standard-table" style="width: 100%;"
! Function
! 函数
! Signature
! 签名
! Description
! 描述
|-
|-
| <code>GetPartnerInstance</code>
| <code>GetPartnerInstance</code>
| <code>handle CPropLinkedPortalDoor::GetPartnerInstance()</code>
| <code>handle {{L|CPropLinkedPortalDoor}}::GetPartnerInstance()</code>
| Get the instance handle of the door's linked partner
| 获取门链接伙伴的实例句柄
|-
|-
| <code>GetPartnername</code>
| <code>GetPartnername</code>
| <code>string CPropLinkedPortalDoor::GetPartnername()</code>
| <code>string {{L|CPropLinkedPortalDoor}}::GetPartnername()</code>
| Returns the partnername of the door.
| 返回门的伙伴名称
|}
|}


=== CSceneEntity ===
=== CSceneEntity ===
Extends [[#CBaseEntity|CBaseEntity]]
继承 [[#CBaseEntity|CBaseEntity]]
 
 
==== Methods ====
{| class="standard-table" style="width: 100%;"
! Function
! Signature
! Description
|-
| <code>AddBroadcastTeamTarget</code>
| <code>void CSceneEntity::AddBroadcastTeamTarget(int)</code>
| Adds a team (by index) to the broadcast list
|-
| <code>EstimateLength</code>
| <code>float CSceneEntity::EstimateLength()</code>
| Returns length of this scene in seconds.
|-
| <code>FindNamedEntity</code>
| <code>handle CSceneEntity::FindNamedEntity(string)</code>
| given an entity reference, such as !target, get actual entity from scene object
|-
| <code>IsPaused</code>
| <code>bool CSceneEntity::IsPaused()</code>
| If this scene is currently paused.
|-
| <code>IsPlayingBack</code>
| <code>bool CSceneEntity::IsPlayingBack()</code>
| If this scene is currently playing.
|-
| <code>LoadSceneFromString</code>
| <code>bool CSceneEntity::LoadSceneFromString(string, string)</code>
| given a dummy scene name and a vcd string, load the scene
|-
| <code>RemoveBroadcastTeamTarget</code>
| <code>void CSceneEntity::RemoveBroadcastTeamTarget(int)</code>
| Removes a team (by index) from the broadcast list
|}
 
 
=== CScriptKeyValues ===
Script handle representation of a models [[$keyvalues]] block.
Sub keys are instances of the same class.
 
 
==== Methods ====
{| class="standard-table" style="width: 100%;"
! Function
! Signature
! Description
|-
| <code>FindKey</code>
| <code>handle CScriptKeyValues::FindKey(string)</code>
| Given a KeyValues object and a key name, find a KeyValues object associated with the key name
|-
| <code>GetFirstSubKey</code>
| <code>handle CScriptKeyValues::GetFirstSubKey()</code>
| Given a KeyValues object, return the first sub key object
|-
| <code>GetKeyBool</code>
| <code>bool CScriptKeyValues::GetKeyBool(string)</code>
| Given a KeyValues object and a key name, return associated bool value
|-
| <code>GetKeyFloat</code>
| <code>float CScriptKeyValues::GetKeyFloat(string)</code>
| Given a KeyValues object and a key name, return associated float value
|-
| <code>GetKeyInt</code>
| <code>int CScriptKeyValues::GetKeyInt(string)</code>
| Given a KeyValues object and a key name, return associated integer value
|-
| <code>GetKeyString</code>
| <code>string CScriptKeyValues::GetKeyString(string)</code>
| Given a KeyValues object and a key name, return associated string value
|-
| <code>GetNextKey</code>
| <code>handle CScriptKeyValues::GetNextKey()</code>
| Given a KeyValues object, return the next key object in a sub key group
|-
| <code>IsKeyEmpty</code>
| <code>bool CScriptKeyValues::IsKeyEmpty(string)</code>
| Given a KeyValues object and a key name, return true if key name has no value
|-
| <code>ReleaseKeyValues</code>
| <code>void CScriptKeyValues::ReleaseKeyValues()</code>
| Given a root KeyValues object, release its contents
|}


=== CTriggerCamera ===
Extends [[#CBaseEntity|CBaseEntity]]
==== Methods ====
{| class="standard-table" style="width: 100%;"
! Function
! Signature
! Description
|-
| <code>GetFov</code>
| <code>int CTriggerCamera::GetFov()</code>
| get camera's current fov setting as integer
|-
| <code>SetFov</code>
| <code>void CTriggerCamera::SetFov(int, float)</code>
| set camera's current fov in integer degrees and fov change rate as float
|}
=== Math ===
The built-in [http://www.squirrel-lang.org/mainsite/doc/sqstdlib2.html#d0e1519 Squirrel math library].
{| class="standard-table" style="width: 100%;"
! Function
! Signature
! Description
|-
| <code>abs</code>
| <code> int abs(float ''x'')</code>
| Returns the absolute value of <code>x</code> as an integer.
|-
| <code>acos</code>
| <code> float acos(float ''x'')</code>
| Returns the arccosine of <code>x</code>.
|-
| <code>asin</code>
| <code> float asin(float ''x'')</code>
| Returns the arcsine of <code>x</code>.
|-
| <code>atan</code>
| <code> float atan(float ''x'')</code>
| Returns the arctangent of <code>x</code>.
|-
| <code>atan2</code>
| <code> float atan2(float ''x'', float ''y'')</code>
| Returns the arctangent of <code>x/y</code>.
|-
| <code>ceil</code>
| <code> float ceil(float ''x'')</code>
| Returns a float value representing the smallest integer that is greater than or equal to <code>x</code>.
|-
| <code>cos</code>
| <code> float cos(float ''x'')</code>
| Returns the cosine of <code>x</code>.
|-
| <code>exp</code>
| <code> float exp(float ''x'')</code>
| Returns the exponential value (<code>e^x</code>) of the float parameter <code>x</code>.
|-
| <code>fabs</code>
| <code> float fabs(float ''x'')</code>
| Returns the absolute value of <code>x</code> as a float.
|-
| <code>floor</code>
| <code> float floor(float ''x'')</code>
| Returns a float value representing the largest integer that is less than or equal to <code>x</code>.
|-
| <code>log</code>
| <code> float log(float ''x'')</code>
| Returns the natural logarithm of <code>x</code>.
|-
| <code>log10</code>
| <code> float log10(float ''x'')</code>
| Returns the logarithm base-10 of <code>x</code>.
|-
| <code>pow</code>
| <code> float pow(float ''x'', float ''y'')</code>
| Returns <code>x</code> raised to the power of <code>y</code>.
|-
| <code>rand</code>
| <code> int rand()</code>
| Returns a pseudorandom integer in the range 0 to <code>RAND_MAX</code>.
|-
| <code>sin</code>
| <code> float sin(float ''x'')</code>
| Returns the sine of <code>x</code>.
|-
| <code>sqrt</code>
| <code> float sqrt(float ''x'')</code>
| Returns the square root of <code>x</code>.
|-
| <code>srand</code>
| <code> void srand(float ''seed'')</code>
| Sets the starting point for generating a series of pseudorandom integers.
|-
| <code>tan</code>
| <code> float tan(float ''x'')</code>
| Returns the tangent of <code>x</code>.
|}





Revision as of 05:56, 29 June 2025

English (en)中文 (zh)Translate (Translate)


此列表包含可用于的所有与传送门2 传送门2VScript(en)游戏引擎的松鼠(en)函数。 当游戏在工具模式(en)运行时可以使用script_help指令在控制台打印出来这些。

变量

实例 类型 描述
Entities CEntities 提供服务器中已生成实体的通道。


常量

实例 类型
_charsize_ integer 1
_floatsize_ integer 4
_intsize_ integer 4
_version_ string "Squirrel 2.2.3 stable"
RAND_MAX integer 32767
PI float 3.14159


CBaseEntity

这是实体的脚本句柄类。所有生成的实体都有一个使用此或其子类之一的脚本句柄。

游戏中的所有脚本句柄都可以从[[Zh/

  1. CEntities|Entities]](en)访问实体脚本, 可以使用self关键字访问自己的脚本句柄。activatorcaller关键字可以在函数调用时访问。


使用方法

函数 特征用法 描述
__KeyValueFromInt void __KeyValueFromInt(string key, int value) 设置实体的某个Keyvalue(en)为整数。它只更改值, 但不会执行实体处理KeyValue所需的任何代码, 因此可能会产生意想不到的副作用。
__KeyValueFromFloat void __KeyValueFromFloat(string key, float value) 设置实体的某个Keyvalue(en)为浮点数。它只更改值, 但不会执行实体处理KeyValue所需的任何代码, 因此可能会产生意想不到的副作用。
__KeyValueFromString void __KeyValueFromString(string key, string value) 设置实体的某个Keyvalue(en)为字符串。它只更改值, 但不会执行实体处理KeyValue所需的任何代码, 因此可能会产生意想不到的副作用。
__KeyValueFromVector void __KeyValueFromVector(string key, Vector value) 设置实体的某个Keyvalue(en)为[[Zh/
  1. Vector|向量(或矢量)]](en)。它只更改值, 但不会执行实体处理KeyValue所需的任何代码, 因此可能会产生意想不到的副作用。
ConnectOutput void CBaseEntity::ConnectOutput(string output, string func_name) 添加一个I/O连接, 当指定的输出触发时, 该连接将调用指定的函数。
Destroy void CBaseEntity::Destroy() 消灭这个实体。
DisconnectOutput void CBaseEntity::DisconnectOutput(string output, string func_name) 从I/O事件中删除连接的脚本函数。
EmitSound void CBaseEntity::EmitSound(string sound) 从该实体处播放一段声音。
EyePosition Vector CBaseEntity::EyePosition() 获得眼睛位置绝对坐标的向量。
FirstMoveChild handle CBaseEntity::FirstMoveChild() 如果处于层次结构中,则获取则检索第一个(移动)实体的子级。
GetAngles Vector CBaseEntity::GetAngles() 获得实体转换为向量的俯仰、水平、横滚的角度数值。
GetAngularVelocity Vector CBaseEntity::GetAngularVelocity() 获取局部角速度-返回俯仰、水平、横滚的向量。
GetBoundingMaxs Vector CBaseEntity::GetBoundingMaxs() 获取一个以对象为中心的最大边框的向量。
GetBoundingMins Vector CBaseEntity::GetBoundingMins() 获取一个以对象为中心的最小边框的向量。
GetCenter Vector CBaseEntity::GetCenter() 获取对象中心的向量-绝对坐标。
GetClassname string CBaseEntity::GetClassname() 获取此实体的类名。
GetForwardVector Vector CBaseEntity::GetForwardVector() 获取实体的正向向量(+X)。
GetHealth int CBaseEntity::GetHealth() 返回实体当前血量(en)
GetLeftVector Vector CBaseEntity::GetLeftVector() 获取实体的左向量。
GetMaxHealth int CBaseEntity::GetMaxHealth() 返回实体最大血量。
GetModelKeyValues handle CBaseEntity::GetModelKeyValues() 获取此实体模型上的键值类实例。
GetModelName string CBaseEntity::GetModelName() 获取该实体模型名称。
GetMoveParent handle CBaseEntity::GetMoveParent() 如果处于层次结构中, 则检索实体的父级。
GetName string CBaseEntity::GetName() 获取此实体的Targetname。
GetOrigin Vector CBaseEntity::GetOrigin() 返回此实体的局部原点位置坐标。
GetOwner handle CBaseEntity::GetOwner() 获取此实体的所有者。
GetPreTemplateName string CBaseEntity::GetPreTemplateName() 将实体名称从模板唯一装饰(&001后缀)中删除。
GetRootMoveParent handle CBaseEntity::GetRootMoveParent() 如果处于层次结构中, 则向上遍历层次结构以查找根父级
GetScriptId string CBaseEntity::GetScriptId() 检索用于引用脚本系统中实体的唯一标识符。
GetScriptScope handle CBaseEntity::GetScriptScope() 检索与实体关联的脚本端数据。
GetSoundDuration float CBaseEntity::GetSoundDuration(string, string) 以浮点数返回声音的持续时间。接受声音名称和可选的actormodelname。
GetTeam int CBaseEntity::GetTeam() 找到这个实体所在的队伍。
GetUpVector Vector CBaseEntity::GetUpVector() 获取实体的向上向量(+Z)。
GetVelocity Vector CBaseEntity::GetVelocity() 获取实体的速度。请注意,这会读取QPhysics(en)速度值, 因此它仅适用于玩家、NPC和移动画笔实体。VPhysics对象的速度始终为零, 一种解决方法是记录多个帧上的位置, 并手动推导速度。
IsValid bool CBaseEntity::IsValid() 如果此实体有效, 则返回true。
NextMovePeer handle CBaseEntity::NextMovePeer() 返回同一继承层次结构中的下一个实体。
PrecacheSoundScript void CBaseEntity::PrecacheSoundScript(string) 预先加载一个声音以供以后播放, 应该在Preche()钩子函数中调用。
SetAbsOrigin void CBaseEntity::SetAbsOrigin(Vector) 将实体传送到此世界位置。
SetAngles void CBaseEntity::SetAngles(float, float, float) 设置实体俯仰、水平、横滚。
SetAngularVelocity void CBaseEntity::SetAngularVelocity(float, float, float) 设置局部角速度-浮点数的俯仰、水平、横滚速度。
SetForwardVector void CBaseEntity::SetForwardVector(Vector) 将实体的正向(+X)向量。
SetHealth void CBaseEntity::SetHealth(int) 设置实体当前的血量(en)
SetMaxHealth void CBaseEntity::SetMaxHealth(int) 设置实体最大血量。
SetModel void CBaseEntity::SetModel(string) 更改实体的模型, 这可用于更改prop_weighted_cube或其他类似实体的模型。模型必须预加载, 可以通过预先放置在地图的其他位置来完成。
SetOrigin void CBaseEntity::SetOrigin(Vector) 将此实体传送到给定位置。
SetOwner void CBaseEntity::SetOwner(handle) 设置实体的所有者。
SetSize void CBaseEntity::SetSize(Vector, Vector) 设置边界框大小。(比如func_brush或trigger_multiple)
SetTeam void CBaseEntity::SetTeam(int) 设置此实体所在的队伍。
SetVelocity void CBaseEntity::SetVelocity(Vector) 设置局部速度。请注意,这会设置QPhysics(en)速度值,因此它仅适用于玩家、NPC和移动画笔实体。对于VPhysics对象,这没有影响。
ValidateScriptScope bool CBaseEntity::ValidateScriptScope() 确保已创建实体的脚本作用域。 在分配给实体的作用域之前, 应该调用此函数。
entindex int CBaseEntity::entindex() 返回实体的索引值。

CBaseAnimating

继承 CBaseEntity

用于为道具等实体设置动画的脚本句柄类。

English (en)中文 (zh)Translate (Translate)

使用方法

函数 签名 描述
GetAttachmentAngles Vector CBaseAnimating(en)::GetAttachmentAngles(int id) 获取附件ID的角度(p,y,r向量)。
GetAttachmentOrigin Vector CBaseAnimating(en)::GetAttachmentOrigin(int id) 获取附件ID的原始向量。
GetObjectScaleLevel int CBaseAnimating(en)::GetObjectScaleLevel() 获取实体的缩放级别。
IsSequenceFinished bool CBaseAnimating(en)::IsSequenceFinished() 检查主序列是否播放完毕。
LookupAttachment int CBaseAnimating(en)::LookupAttachment(string) 获取命名附件的ID。
SetBodygroup void CBaseAnimating(en)::SetBodygroup(int group, int index) 设置身体组。group是目标组的索引,index是要使用的部位索引。

CBaseFlex

继承 CBaseAnimating

使用方法

函数 签名 描述
GetCurrentScene handle CBaseFlex(en)::GetCurrentScene() 返回最早激活的场景实体实例(如果有)。
GetSceneByIndex handle CBaseFlex(en)::GetSceneByIndex(int) 返回指定索引的场景实体实例。

CBasePlayer

继承 CBaseAnimating

使用方法

函数 签名 描述
IsNoclipping bool CBasePlayer(en)::IsNoclipping() 如果玩家处于穿墙模式则返回true。

CEntities

脚本实例: Entities

用于查找和迭代游戏中实体的脚本接口。

要迭代一组实体,请在适当的方法中将null传递给"previous"参数以开始迭代,或引用先前找到的实体以继续搜索。示例:

//第一种方式
local ent = null;     //变量名"ent"可自定义
while(ent = Entities.FindByName(ent, "entityname"))     //注意使用"="而非"=="
{
  //...     //每次迭代时"ent"有效则执行此处代码
}

//第二种方式
for(local ent;ent = Entities.FindByName(ent, "entityname");)     //注意分号位置
{
  //...     //每次迭代时"ent"有效则执行此处代码
}

使用方法

函数 签名 描述
CreateByClassname handle CEntities(en)::CreateByClassname(string) 通过类名创建实体
FindByClassname handle CEntities(en)::FindByClassname(handle start_ent, string classname) 按类名查找实体。传递'null'开始迭代,或引用先前找到的实体继续搜索
FindByClassnameNearest handle CEntities(en)::FindByClassnameNearest(string classname, Vector loc, float radius) 查找距离某点最近的指定类名实体
FindByClassnameWithin handle CEntities(en)::FindByClassnameWithin(handle start_ent, string classname, Vector loc, float radius) 查找半径内的指定类名实体。传递'null'开始迭代
FindByModel handle CEntities(en)::FindByModel(handle start_ent, string model) 按模型名查找实体。传递'null'开始迭代
FindByName handle CEntities(en)::FindByName(handle start_ent, string targetname) 按名称(支持通配符)查找实体。传递'null'开始迭代
FindByNameNearest handle CEntities(en)::FindByNameNearest(string targetname, Vector loc, float radius) 查找距离某点最近的指定名称实体
FindByNameWithin handle CEntities(en)::FindByNameWithin(handle, string, Vector loc, float radius) 查找半径内的指定名称实体。传递'null'开始迭代
FindByTarget handle CEntities(en)::FindByTarget(handle start_ent, string targetname) 查找具有特定target键值的实体。传递'null'开始迭代
FindInSphere handle CEntities(en)::FindInSphere(handle start_ent, Vector loc, float radius) 查找半径内的实体。传递'null'开始迭代
First handle CEntities(en)::First() 开始实体列表迭代。等同于Next(null)
Next handle CEntities(en)::Next(handle) 继续迭代,需提供先前找到的实体引用

CEnvEntityMaker

继承 CBaseEntity

脚本句柄类是env_entity_maker(en)

使用方法

函数 签名 描述
SpawnEntity void CEnvEntityMaker(en)::SpawnEntity() 在maker位置创建实体
SpawnEntityAtEntityOrigin void CEnvEntityMaker(en)::SpawnEntityAtEntityOrigin(handle) 在指定实体位置创建实体
SpawnEntityAtLocation void CEnvEntityMaker(en)::SpawnEntityAtLocation(Vector, Vector) 在指定位置和欧拉角(pitch,yaw,roll)创建实体
SpawnEntityAtNamedEntityOrigin void CEnvEntityMaker(en)::SpawnEntityAtNamedEntityOrigin(string) 在命名实体位置创建实体

CLinkedPortalDoor

[待完善]

使用方法

函数 签名 描述
GetPartnerInstance handle CLinkedPortalDoor(en)::GetPartnerInstance() 获取门链接伙伴的实例句柄
GetPartnername string CLinkedPortalDoor(en)::GetPartnername() 返回门的伙伴名称

CPlayerVoiceListener

[待完善]

使用方法

函数 签名 描述
GetPlayerSpeechDuration float CPlayerVoiceListener(en)::GetPlayerSpeechDuration(int) 返回玩家持续说话的秒数
IsPlayerSpeaking bool CPlayerVoiceListener(en)::IsPlayerSpeaking(int) 返回指定玩家是否正在说话

CPortal_Player

[待完善]

使用方法

函数 签名 描述
GetWheatleyMonitorDestructionCount int CPortal_Player(en)::GetWheatleyMonitorDestructionCount() 获取玩家摧毁Wheatley显示器的数量
IncWheatleyMonitorDestructionCount void CPortal_Player(en)::IncWheatleyMonitorDestructionCount() 增加玩家摧毁Wheatley显示器的计数
TurnOffPotatos void CPortal_Player(en)::TurnOffPotatos() 关闭Potatos材质光源
TurnOnPotatos void CPortal_Player(en)::TurnOnPotatos() 开启Potatos材质光源

CPropLinkedPortalDoor

[待完善]

使用方法

函数 签名 描述
GetPartnerInstance handle CPropLinkedPortalDoor(en)::GetPartnerInstance() 获取门链接伙伴的实例句柄
GetPartnername string CPropLinkedPortalDoor(en)::GetPartnername() 返回门的伙伴名称

CSceneEntity

继承 CBaseEntity


Vector

Represents an x/y/z position, returned/passed to many functions. It takes the 3 x/y/z positions as a parameter.
A few math operations can be applied to vectors - +/- between any two vectors, and Vector * number in that order only. All three operations return another vector. Other math operations are done with method calls:


Methods

Function Signature Description
Cross Vector Vector::Cross(Vector other) Return the vector cross product - this × other.
Dot float Vector::Dot(Vector other) Return the vector dot product - this · other.
Length float Vector::Length() Return the distance from the origin.
Length2D float Vector::Length2D() Return the distance from the origin, ignoring the Z axis.
LengthSqr float Vector::LengthSqr() Return the distance from the origin, but squared. This is faster to compute since a square root isn\'t required.
Length2DSqr float Vector::Length2DSqr() Return the distance from the origin, ignoring the Z axis and squared. This is faster to compute since a square root isn\'t required.
Norm float Vector::Norm() Modify the vector to have a length of 1, and return its original length.
ToKVString string Vector::ToKVString() Return a string in the form "X Y Z".


Other functions

Function Signature Description
Assert void Assert(exp, string message = null) Throws an exception if exp equates to false, optionally with message.
CreateProp handle CreateProp(string classname, Vector origin, string modelname, int activity) Create a prop. The class should be a prop_physics style entity.
CreateSceneEntity handle CreateSceneEntity(string) Create a scene entity to play the specified scene.
DebugDrawBox void DebugDrawBox(Vector origin, Vector mins, Vector maxs, int r, int g, int b, int alpha, float duration) Draw a debug box, for visualizing code. It's positioned at origin, with the dimensions mins/maxs Developer must be on to show this, and it'll stay around for duration seconds (or 1 frame if -1.0). The color ranges from 0-255.
DebugDrawLine void DebugDrawLine(Vector start, Vector end, int r, int g, int b, bool hitTest, float duration) Draw a debug line, for visualizing code. Developer must be on to show this, and it'll stay around for duration seconds (or 1 frame if -1.0). The color ranges from 0-255.
DoIncludeScript bool DoIncludeScript(string filename, table scope) Execute the script file "scripts/vscripts/" + filename in the scope of scope. The extension .nut can be omitted.
IncludeScript bool IncludeScript(string filename, table scope = null) Execute the script file "scripts/vscripts/" + filename in the scope of scope, this by default. The extension .nut can be omitted.
DoEntFire void DoEntFire(string target, string action, string value, float delay, handle activator, handle caller) Generate an entity I/O event.
EntFire function EntFire(target, action, value, delay, activator) Generate an entity I/O event. Value, delay and activator are optional.
EntFireByHandle void EntFireByHandle(handle target, string action, string value, float delay, handle activator, handle caller) Generate an entity I/O event. First parameter is an entity instance.
FrameTime float FrameTime() Get the time spent on the server in the last frame.
GetDeveloperLevel int GetDeveloperLevel() Gets the level of 'developer'.
GetMapIndexInPlayOrder int GetMapIndexInPlayOrder() For workshop maps, determines which index (by order played) this map is. Returns -1 if entry is not found or -2 if this is not a workshop map.
GetMapName string GetMapName() Get the name of the map.
GetNumMapsPlayed int GetNumMapsPlayed() Returns how many workshop maps the player has played through.
GetPlayerSilenceDuration float GetPlayerSilenceDuration(int player_index) Time that the specified player has been silent on the mic.
IsMultiplayer bool IsMultiplayer() Returns true if this is a multiplayer game, or false if singleplayer.
LoopSinglePlayerMaps bool LoopSinglePlayerMaps() Returns true if the cvar loopsingleplayermaps is enabled. Still works in multiplayer.
RandomFloat float RandomFloat(float min, float max) Generate a random floating point number within a range, inclusive
RandomInt int RandomInt(int min, int max) Generate a random integer within a range, inclusive
RecordAchievementEvent void RecordAchievementEvent(string name, int player_index) Earns a given achievement or increases progress.
RequestMapRating void RequestMapRating() In workshop maps, pops up the map rating dialog for user input.
RetrieveNativeSignature void RetrieveNativeSignature(string nativeFunction) [待完善]
SendToConsole void SendToConsole(string command) Send a string to the console as a command. The command is treated as coming from the host in multiplayer.
SetDucking void SetDucking(string, string, float) Set the level of an audio ducking channel.
SetMapAsPlayed int SetMapAsPlayed() For workshop maps, adds the current map to the play order and returns the new index therein. Returns -2 if this is not a workshop map.
ShowMessage void ShowMessage(string) Print a HUD message on all clients.
Time float Time() Get the current server time
TraceLine float TraceLine(Vector start, Vector end, handle ignored_ent) Given 2 points and an entity to ignore, returns fraction along line that hits the world. Does not hit entities, seemingly making the ignored entity parameter useless.
UniqueString string UniqueString(string suffix = "") Generate a string guaranteed to be unique across the life of the script VM, with an optional root string. Useful for adding data to tables when not sure what keys are already in use in that table. Equivalent to DoUniqueString(suffix).
DoUniqueString string DoUniqueString(string suffix) Generate a string guaranteed to be unique across the life of the script VM, with an optional root string. Useful for adding data to tables when not sure what keys are already in use in that table.
__ReplaceClosures void __ReplaceClosures(function script, table scope) Internal function called in script_reload_ server commands.

Singleplayer only

These functions can only be used in singleplayer games, and may cause errors if used in multiplayer.

Function Signature Description
GetPlayer handle GetPlayer() Returns the player.
GivePlayerPortalgun void GivePlayerPortalgun() Equips the player with a blue-only portalgun.
PrecacheMovie void PrecacheMovie(string) Precaches a named movie. Only valid to call within the entity's 'Precache' function called on mapspawn.
ScriptShowHudMessageAll void ScriptShowHudMessageAll(string, float) Show center print text message for specified number of seconds.
ScriptSteamShowURL bool ScriptSteamShowURL(string) Bring up the steam overlay and shows the specified URL. (Full address with protocol type is required, e.g. http://www.steamgames.com/)
TryDLC1InstalledOrCatch void TryDLC1InstalledOrCatch() Throws an exception if the Peer Review DLC isn't installed. Useless in the current PC version of the game, as Peer Review will always be present.
UpgradePlayerPortalgun void UpgradePlayerPortalgun() Upgrades the player's portalgun to shoot orange portals.
UpgradePlayerPotatogun void UpgradePlayerPotatogun() Upgrades the player's portalgun to shoot orange portals and have PotatOS impaled on it.

Multiplayer only

These functions can only be used in multiplayer games, and may cause errors if used in singleplayer.

Function Signature Description
AddBranchLevelName void AddBranchLevelName(int, string) Adds a level to the specified branch's list.
Note.png注意:In co-op maps, this function must be called at least once or players will not be able to move.
AddCoopCreditsName void AddCoopCreditsName(string) Adds a name to the coop credit's list.
AddGladosSpokenFlags void AddGladosSpokenFlags(int, int) Adds bit flags for specific lines that are tracked per session.
CoopGladosBlowUpBots void CoopGladosBlowUpBots() Blows up both robots and prevents respawning.
CoopGetNumPortalsPlaced int CoopGetNumPortalsPlaced() Returns the number of portals the players have placed so far.
CoopGetLevelsCompletedThisBranch int CoopGetLevelsCompletedThisBranch() Returns the number of levels the players have completed in their run of the current branch.
CoopGetBranchTotalLevelCount int CoopGetBranchTotalLevelCount() Returns the number of levels in the current branch.
CoopSetCameFromLastDLCMap void CoopSetCameFromLastDLCMap(bool) Set whether the players came from the last coop DLC map or not.
CoopSetMapRunTime void CoopSetMapRunTime(float runLength) Sets the time to complete a coop map from spawn to completing the puzzle. (Possibly related to the Triple Crown achievement.)
GetBluePlayerIndex int GetBluePlayerIndex() Player index of the blue player.
GetCameFromLastDLCMap bool GetCameFromLastDLCMap() Returns true if coming from the last DLC coop map (as set by CoopSetCameFromLastDLCMap).
GetCoopBranchLevelIndex int GetCoopBranchLevelIndex(int) Given the 'branch' argument, returns the current chosen level.
GetCoopSectionIndex int GetCoopSectionIndex() Section that the coop players have selected to load.
GetGladosSpokenFlags int GetGladosSpokenFlags(int) Returns bit flags for specific lines that are tracked per session.
GetHaveSeenDLCTubesReveal bool GetHaveSeenDLCTubesReveal() Get whether players have seen the DLC tubes reveal this session (as set by SetHaveSeenDLCTubesReveal).
GetHighestActiveBranch int GetHighestActiveBranch() Returns which branches should be available in the hub.
GetNumPlayersConnected int GetNumPlayersConnected() Returns how many players are connected. In normal co-op, this will almost always be 2.
GetOrangePlayerIndex int GetOrangePlayerIndex() Player index of the orange player.
GetPlayerDeathCount int GetPlayerDeathCount(int player) Returns the number of times that a specific player has died in the session.
IsBranchComplete bool IsBranchComplete(int course) Returns true if every level in the course has been completed by either player.
IsLevelComplete bool IsLevelComplete(int course, int level) Returns true if the level in the specified course is completed by either player.
IsLocalSplitScreen bool IsLocalSplitScreen() Returns true if players are in split screen.
IsPlayerBranchComplete bool IsPlayerBranchComplete(int player, int course) Returns true if every level in the course has been completed by the specified player.
IsPlayerLevelComplete bool IsPlayerLevelComplete(int, int, int) Returns true if the level in the specified course is completed by a specific player.
MarkMapComplete void MarkMapComplete(string) Marks a maps a complete for both players.
NotifySpeedRunSuccess void NotifySpeedRunSuccess(int runLength, string mapname) Tells the client that a successful speed run has been completed. (Used for the Triple Crown achievement.)
SaveMPStatsData void SaveMPStatsData() Save the multiplayer stats for the score board.
SetHaveSeenDLCTubesReveal void SetHaveSeenDLCTubesReveal() Set that players have seen the DLC tubes reveal this session.

See also