跳至正文
版本:最新版 - a1.156.0 ⚖️

基础实体

Entity 是所有类的基础母版,所有这些实体都共享本页面中所描述的方法和事件。

👪Base Class🧑‍💻API Source
👪Base Class
This is a Base Class, an abstract definition that shares common methods and events with it's child classes. You cannot spawn it, and you cannot call anything on the Base Class name itself. Use any class that inherits it.

Classes that share methods and events from this Base Class: Billboard, Blueprint, Cable, Canvas, Character, CharacterSimple, Decal, Gizmo, Grenade, InstancedStaticMesh, Light, Melee, Particle, Player, Prop, SceneCapture, StaticMesh, Sound, TextRender, Text3D, Trigger, VehicleWheeled, VehicleWater, Weapon, WebUI, Widget, Widget3D.

🗿 静态函数​

ReturnsNameDescription
table of Base EntityGetAll返回一个包含调用此方法的类中所有实体的表
Base EntityGetByIndex返回该类中指定索引处的特定实体
integerGetCount返回该类当前存在的实体数量
table of tableGetInheritedClasses获取使用 继承系统 创建的、直接继承自该类的所有子类列表
iteratorGetPairs返回一个包含该类所有实体的迭代器,用于 pairs() 循环
table or nilGetParentClass如果该类是使用 继承系统 创建的,则获取其父类
tableInherit使用 继承系统 继承此类
booleanIsChildOf如果该类是使用 继承系统 创建的,检查此类是否为另一个类的子类
functionSubscribe为该类的所有实体订阅一个 事件
functionSubscribeRemote订阅从服务器调用的自定义远程事件
Unsubscribe退订此包内该类中此 事件 的所有回调,或者仅取消传入的特定回调

Moderate

GetAll​

返回一个包含调用此方法的类中所有实体的表
local ret = <AnyEntityClass>.GetAll()

Returns

TypeDescription
table of Base Entity包含所有实体的表副本

Optimal

GetByIndex​

返回该类中指定索引处的特定实体
local ret = <AnyEntityClass>.GetByIndex(index)

Parameters

TypeParameterDefaultDescription
integerindex Required parameter 实体的索引

Returns

TypeDescription
Base Entity该索引处的实体

Optimal

GetCount​

返回该类当前存在的实体数量
local ret = <AnyEntityClass>.GetCount()

Returns

TypeDescription
integer该类的实体数量

Moderate

GetInheritedClasses​

获取使用 继承系统 创建的、直接继承自该类的所有子类列表
local ret = <AnyEntityClass>.GetInheritedClasses(recursively?)

Parameters

TypeParameterDefaultDescription
booleanrecursively?false
是否返回所有层级的继承子类

Returns

TypeDescription
table of table所有子类表

Optimal

GetPairs​

返回一个包含该类所有实体的迭代器,用于 pairs() 循环。这是一个比 GetAll() 性能更高的方法,因为它直接返回访问实体的迭代器,而不是创建并返回实体表副本。

注: 在 GetPairs() 循环内部销毁实体会导致可迭代对象在执行过程中改变大小。如果你需要进行循环并销毁操作,请使用 GetAll()。
local ret = <AnyEntityClass>.GetPairs()

Returns

TypeDescription
iterator包含该类所有实体的迭代器

Optimal

GetParentClass​

如果该类是使用 继承系统 创建的,则获取其父类
local ret = <AnyEntityClass>.GetParentClass()

Returns

TypeDescription
table or nil父类

Slow

Inherit​

使用 继承系统 继承此类
local class = <AnyEntityClass>.Inherit(name, custom_values?)

Parameters

TypeParameterDefaultDescription
stringname Required parameter 新类的名称
tablecustom_values?{}
一个可选的表,包含要在继承的类表中设置的自定义值

Returns

TypeDescription
table新类的表

Optimal

IsChildOf​

如果该类是使用 继承系统 创建的,检查此类是否为另一个类的子类
local ret = <AnyEntityClass>.IsChildOf(class)

Parameters

TypeParameterDefaultDescription
tableclass Required parameter 要检查的另一个类

Returns

TypeDescription
boolean通过 <a href='/docs/core-concepts/scripting/inheriting-classes'>继承系统</a> 创建的此类是否为另一个类的子类

Optimal

Subscribe​

为该类的所有实体订阅一个 事件
local ret = <AnyEntityClass>.Subscribe(event_name, callback)

Parameters

TypeParameterDefaultDescription
stringevent_name Required parameter 要订阅的事件名称
functioncallback Required parameter 触发事件时要调用的函数

Returns

TypeDescription
function传进来的回调函数(如果你的回调是匿名函数,这对于以后退订很有用)

Optimal

SubscribeRemote​

订阅从服务器调用的自定义远程事件
local ret = <AnyEntityClass>.SubscribeRemote(event_name, callback)

Parameters

TypeParameterDefaultDescription
stringevent_name Required parameter 要订阅的事件名称
functioncallback Required parameter 触发事件时要调用的函数

Returns

TypeDescription
function传入的回调函数(如果回调函数是匿名函数,则便于日后取消订阅)

Optimal

Unsubscribe​

退订此包内该类中此 事件 的所有回调,或者仅取消传入的特定回调
<AnyEntityClass>.Unsubscribe(event_name, callback?)

Parameters

TypeParameterDefaultDescription
stringevent_name Required parameter 要退订的事件名称
functioncallback?nil
可选,要退订的特定回调函数

🦠 函数​

ReturnsNameDescription
BroadcastRemoteEvent直接在此实体上向所有玩家调用自定义远程事件
BroadcastRemoteInRadiusEvent直接针对此实体调用自定义远程事件,并将该事件传递给半径范围内的所有玩家
CallRemoteEvent直接在此实体上向特定玩家调用自定义远程事件
CallRemoteEvent直接在此实体上调用自定义远程事件
CallRemotePlayersEvent直接在此实体上调用自定义远程事件,并将事件传递给玩家列表
Destroy销毁该实体
FinishSpawn如果此实体是以延迟 spawn_mode 生成的,则完成生成过程并将该实体发送至客户端
table of stringGetAllValuesKeys获取所有存储值的键名列表
tableGetClass获取该实体的类
integerGetID获取该实体的全网通用网络 ID(在客户端和服务器上相同)
anyGetValue获取此实体上存储在给定键处的 值
booleanHasAuthority获取本地上下文是否对此实体拥有主控(如果由客户端生成则为 true,由服务器生成则为 false)
booleanIsA递归检查此实体是否继承自某个类
booleanIsBeingDestroyed如果该实体正在被销毁,则返回 true
booleanIsSpawned获取此实体是否已完成生成
booleanIsValid如果该实体有效(即未被销毁且指向一个有效的实体),则返回 true
SetSpawnMode从继承类的构造函数中覆盖此实体完成生成的时间点
SetValue在此实体中设置一个值
functionSubscribe在这个特定实体上订阅一个事件
functionSubscribeRemote在这个特定实体上订阅一个从服务器调用的自定义远程事件
Unsubscribe退订此包内该实体中此 事件 的所有回调,或者仅取消传入的特定回调

Moderate

BroadcastRemoteEvent​

直接在此实体上向所有玩家调用自定义远程事件
🆕Updated Function
This method was recently updated in ver. 1.139. See the Compatibility Versions page for more information.
my_entity:BroadcastRemoteEvent(event_name, reliability?, args...?)

Parameters

TypeParameterDefaultDescription
stringevent_name Required parameter 用于触发事件的事件名称
Reliabilityreliability?Reliability.Reliable
网络可靠性规则
anyargs...?nil
传递给事件的参数

Moderate

BroadcastRemoteInRadiusEvent​

直接针对此实体调用自定义远程事件,并将该事件传递给半径范围内的所有玩家
my_entity:BroadcastRemoteInRadiusEvent(event_name, radius, reliability?, args...?)

Parameters

TypeParameterDefaultDescription
stringevent_name Required parameter 用于触发事件的事件名称
floatradius Required parameter 发送此事件的半径
Reliabilityreliability?Reliability.Reliable
网络可靠性规则
anyargs...?nil
传递给事件的参数

Moderate

CallRemoteEvent​

直接在此实体上向特定玩家调用自定义远程事件
🆕Updated Function
This method was recently updated in ver. 1.139. See the Compatibility Versions page for more information.
my_entity:CallRemoteEvent(event_name, player, reliability?, args...?)

Parameters

TypeParameterDefaultDescription
stringevent_name Required parameter 用于触发事件的事件名称
Playerplayer Required parameter 接收此事件的远程玩家
Reliabilityreliability?Reliability.Reliable
网络可靠性规则
anyargs...?nil
传递给事件的参数

Moderate

CallRemoteEvent​

直接在此实体上调用自定义远程事件
🆕Updated Function
This method was recently updated in ver. 1.139. See the Compatibility Versions page for more information.
my_entity:CallRemoteEvent(event_name, reliability?, args...?)

Parameters

TypeParameterDefaultDescription
stringevent_name Required parameter 用于触发事件的事件名称
Reliabilityreliability?Reliability.Reliable
网络可靠性规则
anyargs...?nil
传递给事件的参数

Moderate

CallRemotePlayersEvent​

直接在此实体上调用自定义远程事件,并将事件传递给玩家列表
my_entity:CallRemotePlayersEvent(event_name, players, reliability?, args...?)

Parameters

TypeParameterDefaultDescription
stringevent_name Required parameter 用于触发事件的事件名称
table of Playerplayers Required parameter 发送该事件的目标远程玩家列表
Reliabilityreliability?Reliability.Reliable
网络可靠性规则
anyargs...?nil
传递给事件的参数

Moderate

Destroy​

销毁该实体
my_entity:Destroy()

Moderate

FinishSpawn​

如果实体是以 SpawnMode.AfterConstructor 或 SpawnMode.Manual 生成的,则完成生成过程并将实体发送至客户端。请在配置完实体后调用此函数以提高生成效率。

注:对于通过 SpawnMode.AfterConstructor 构造的继承类实体,会在其构造函数返回时自动完成生成。
my_entity:FinishSpawn()

Moderate

GetAllValuesKeys​

获取所有存储值的键名列表
local ret = my_entity:GetAllValuesKeys()

Returns

TypeDescription
table of string包含所有存储值键名的列表

See also GetValue, SetValue.


Optimal

GetClass​

获取该实体的类
local class = my_entity:GetClass()

Returns

TypeDescription
table此实体的类

Optimal

GetID​

获取该实体的全网通用网络 ID(在客户端和服务器上相同)
local id = my_entity:GetID()

Returns

TypeDescription
integer此实体的通用网络 ID

Optimal

GetValue​

获取此实体上存储在给定键处的值。请参考 实体值 获取更多信息
local value = my_entity:GetValue(key, fallback)

Parameters

TypeParameterDefaultDescription
stringkey Required parameter 键
anyfallback Required parameter 如果键不存在时的备用值

Returns

TypeDescription
any指定键的值,若键不存在则返回回退值

See also GetAllValuesKeys, SetValue.


Optimal

HasAuthority​

获取本地上下文是否对此实体拥有主控(如果由客户端生成则为 true,由服务器生成则为 false)
local ret = my_entity:HasAuthority()

Returns

TypeDescription
boolean如果由客户端生成则为 true,由服务器生成则为 false

Moderate

IsA​

递归检查此实体是否继承自某个类
local ret = my_entity:IsA(class)

Parameters

TypeParameterDefaultDescription
tableclass Required parameter 类

Returns

TypeDescription
boolean此实体是否属于该类或继承自该类

Optimal

IsBeingDestroyed​

如果该实体正在被销毁,则返回 true。
你可以在诸如 Drop 的事件中检查此状态,以判断一个可拾取物是否是因为即将被销毁而被丢弃
local ret = my_entity:IsBeingDestroyed()

Returns

TypeDescription
boolean此实体是否正在被销毁

Optimal

IsSpawned​

获取此实体是否已完成生成。

以延迟 SpawnMode 生成的实体仅会在调用 FinishSpawn() 后或继承类构造函数返回时才完成生成,其他模式下生成的实体会立即完成生成。
local ret = my_entity:IsSpawned()

Returns

TypeDescription
boolean实体是否已完成生成

Optimal

IsValid​

如果该实体有效(即未被销毁且指向一个有效的实体),则返回 true
local is_valid = my_entity:IsValid()

Returns

TypeDescription
boolean此实体是否有效

Optimal

SetSpawnMode​

从继承类的构造函数中覆盖此实体完成生成的时间点。

仅接受 SpawnMode.AfterConstructor 和 SpawnMode.Manual,且仅在该实体尚未完成生成时生效。
my_entity:SetSpawnMode(spawn_mode)

Parameters

TypeParameterDefaultDescription
SpawnModespawn_mode Required parameter 仅接受 SpawnMode.AfterConstructor 和 SpawnMode.Manual

Optimal

SetValue​

在此实体中设置一个值,该值可以被任何包访问(如果是从服务器调用,则可以跨客户端选择性同步)

请参考实体值进一步了解
my_entity:SetValue(key, value, sync_on_clients?)

Parameters

TypeParameterDefaultDescription
stringkey Required parameter 键
anyvalue Required parameter 要设置的值,可以是任何可序列化类型(函数除外)
booleansync_on_clients?false
服务器端参数,如果启用,将此值与所有客户端同步

See also GetAllValuesKeys, GetValue.


Optimal

Subscribe​

在这个特定实体上订阅一个事件
local ret = my_entity:Subscribe(event_name, callback)

Parameters

TypeParameterDefaultDescription
stringevent_name Required parameter 要订阅的事件名称
functioncallback Required parameter 触发事件时要调用的函数

Returns

TypeDescription
function传入的回调函数(如果回调函数是匿名函数,则便于日后取消订阅)

Optimal

SubscribeRemote​

在这个特定实体上订阅一个从服务器调用的自定义远程事件
local ret = my_entity:SubscribeRemote(event_name, callback)

Parameters

TypeParameterDefaultDescription
stringevent_name Required parameter 要订阅的事件名称
functioncallback Required parameter 触发事件时要调用的函数

Returns

TypeDescription
function传入的回调函数(如果回调函数是匿名函数,则便于日后取消订阅)

Optimal

Unsubscribe​

退订此包内该实体中此 事件 的所有回调,或者仅取消传入的特定回调
my_entity:Unsubscribe(event_name, callback?)

Parameters

TypeParameterDefaultDescription
stringevent_name Required parameter 要退订的事件名称
functioncallback?nil
可选,要退订的特定回调函数

🚀 事件​

NameDescription
ClassRegister当使用 继承系统 注册一个新类时触发
Destroy当实体被销毁时触发
Spawn当实体被生成/创建时触发
ValueChange当实体通过 :SetValue() 改变了值时触发

ClassRegister​

当使用 继承系统 注册一个新类时触发
<AnyEntityClass>.Subscribe("ClassRegister", function(class)
-- ClassRegister was called
end)

Arguments

TypeArgumentDescription
tableclass被继承的类

Destroy​

当实体被销毁时触发
<AnyEntityClass>.Subscribe("Destroy", function(self)
-- Destroy was called
end)

Arguments

TypeArgumentDescription
Base Entityself被销毁的实体

Spawn​

当实体被生成/创建时触发
<AnyEntityClass>.Subscribe("Spawn", function(self)
-- Spawn was called
end)

Arguments

TypeArgumentDescription
Base Entityself被生成的实体

ValueChange​

当实体通过 :SetValue() 改变了值时触发
<AnyEntityClass>.Subscribe("ValueChange", function(self, key, value)
-- ValueChange was called
end)

Arguments

TypeArgumentDescription
Base Entityself值刚刚发生改变的实体
stringkey所使用的键
anyvalue被设置的新值