Skip to content

TagService ​

概览 ​

字段值
KindService · 服务 Service
Realmcommon

继承关系 ​

  • Unit(5 属性 / 28 函数 / 6 事件)
    • [TagService](6 函数 / 2 事件)

> Service 继承 Unit 仅表示运行时类型关系;作者可用 API 以当前 Service 页面显式列出的成员为准,不自动包含 Unit 的创建、层级或销毁能力。

TagService 是全局标签服务,用于给空间单位附加字符串标签,并基于标签做成员判定与集合检索。它提供添加、移除、判定、列举单个单位标签以及反查某标签下全部单位的能力,并在本端标签增删时触发事件。

适用场景 ​

常用于给单位打上业务标记后做分类筛选,例如效果系统在施加效果前用 HasTag 判断目标单位是否已带有某标签,或按标签批量取出同类单位统一处理。

使用要点 ​

先通过 game:GetService("TagService") 取得服务实例,再调用 AddTag(unit, tag) 打标签、RemoveTag(unit, tag) 移除标签;用 HasTag(unit, tag) 或 GetTags(unit) 查询单个单位的标签状态,用 GetAllTags() 列出全部标签、GetTagged(tag) 反查带有该标签的单位集合;需要响应变化时连接 TagAdded 与 TagRemoved 事件,并在不再需要时断开连接。

注意事项 ​

TagService 的 AddTag/RemoveTag 当前以本端状态变更为主,跨客户端同步与事件广播语义需以运行包实测为准。当前运行包还存在客户端初始化完成后新增标签未进入 GetTagged 本地索引的反馈,无论标签由服务端还是客户端添加,都不要把客户端 GetTagged 结果视为完整动态注册表。若业务要求服务端改 Tag 后客户端稳定感知,建议通过公开的 RemoteEvent 发送显式同步消息,并由业务层维护对象集合。

代码示例 ​

安全创建临时单位并验证标签 ​

lua
-- @runtime client
local TagService = game:GetService('TagService')
local World = game:GetService('World')
local unit = World:CreateUnit('WorldUnit', {})
if unit == nil then return end
TagService:AddTag(unit, 'DemoTag')
print('标签已添加:', TagService:HasTag(unit, 'DemoTag'))
unit:Destroy()

关联类型 ​

事件 (2) ​

TagAdded ​

签名:TagAdded(tag: String) 触发:local

当任意 SpaceUnit 实例首次被添加某个标签时触发,提供被添加的标签字符串。

回调参数类型说明
tagString新增的标签

示例代码

监听标签首次出现

lua
-- @runtime client
local TagService = game:GetService('TagService')
local World = game:GetService('World')
local unit = World:CreateUnit('WorldUnit', {})
if unit == nil then return end
local connection = TagService.TagAdded:Connect(function(tag) print('新标签:', tag) end)
TagService:AddTag(unit, 'DemoAddedTag')
connection:Disconnect()
unit:Destroy()

TagRemoved ​

签名:TagRemoved(tag: String) 触发:local

当任意 SpaceUnit 实例的某个标签被完全移除时触发,提供被移除的标签字符串。

回调参数类型说明
tagString移除的标签

示例代码

监听标签完全移除

lua
-- @runtime client
local TagService = game:GetService('TagService')
local World = game:GetService('World')
local unit = World:CreateUnit('WorldUnit', {})
if unit == nil then return end
TagService:AddTag(unit, 'DemoRemovedTag')
local connection = TagService.TagRemoved:Connect(function(tag) print('标签已移除:', tag) end)
TagService:RemoveTag(unit, 'DemoRemovedTag')
connection:Disconnect()
unit:Destroy()

函数 (6) ​

AddTag ​

签名:AddTag(unit: SpaceUnit, tag: String) -> void

为指定组件添加一个字符串标签。添加后该组件即拥有此标签,可通过 HasTag 查询、GetTags 列举,也可被 GetTagged 反查。

参数类型说明
unitSpaceUnit组件
tagString标签

返回值 void

示例代码

为临时单位添加标签

lua
-- @runtime client
local TagService = game:GetService('TagService')
local World = game:GetService('World')
local unit = World:CreateUnit('WorldUnit', {})
if unit == nil then return end
TagService:AddTag(unit, 'Enemy')
print('是否拥有 Enemy:', TagService:HasTag(unit, 'Enemy'))
unit:Destroy()

RemoveTag ​

签名:RemoveTag(unit: SpaceUnit, tag: String) -> void

从指定组件上移除一个已添加的标签。

参数类型说明
unitSpaceUnit组件
tagString标签

返回值 void

示例代码

从临时单位移除标签

lua
-- @runtime client
local TagService = game:GetService('TagService')
local World = game:GetService('World')
local unit = World:CreateUnit('WorldUnit', {})
if unit == nil then return end
TagService:AddTag(unit, 'Temporary')
TagService:RemoveTag(unit, 'Temporary')
print('移除后仍存在:', TagService:HasTag(unit, 'Temporary'))
unit:Destroy()

HasTag ​

签名:HasTag(unit: SpaceUnit, tag: String) -> Bool (是否拥有标签)

判断指定组件是否拥有某个标签,返回布尔值表示判断结果。

参数类型说明
unitSpaceUnit组件
tagString标签

返回值 Bool (是否拥有标签)

示例代码

检查临时单位的标签

lua
-- @runtime client
local TagService = game:GetService('TagService')
local World = game:GetService('World')
local unit = World:CreateUnit('WorldUnit', {})
if unit == nil then return end
TagService:AddTag(unit, 'Interactive')
print('是否可交互:', TagService:HasTag(unit, 'Interactive'))
unit:Destroy()

GetTags ​

签名:GetTags(unit: SpaceUnit) -> Array<String> (标签列表)

获取指定组件当前拥有的所有标签,并以数组形式返回标签字符串列表。

参数类型说明
unitSpaceUnit组件

返回值 Array<String> (标签列表)

示例代码

读取临时单位的全部标签

lua
-- @runtime client
local TagService = game:GetService('TagService')
local World = game:GetService('World')
local unit = World:CreateUnit('WorldUnit', {})
if unit == nil then return end
TagService:AddTag(unit, 'NPC')
for _, tag in ipairs(TagService:GetTags(unit)) do print('单位标签:', tag) end
unit:Destroy()

GetAllTags ​

签名:GetAllTags() -> Array<String> (标签列表)

获取当前游戏中所有已存在的标签列表。

返回值 Array<String> (标签列表)

示例代码

读取当前端已有的全部标签名

lua
-- @runtime client
local TagService = game:GetService('TagService')
for _, tag in ipairs(TagService:GetAllTags()) do
    print('当前标签:', tag)
end

GetTagged ​

签名:GetTagged(tag: String) -> Array&lt;SpaceUnit&gt; (组件列表)

获取当前拥有指定标签的所有组件,并以数组形式返回组件列表。

⚙ GetTagged 返回当前端已建立索引的标签快照。当前运行包实测客户端初始化完成后新增的标签对象可能不会进入该结果;关键业务应维护显式对象集合或通过公开的 RemoteEvent 同步,不能把 GetTagged 当作完整动态注册表。

参数类型说明
tagString标签

返回值 Array<SpaceUnit> (组件列表)

示例代码

读取当前端已有的标签快照

lua
-- @runtime client
local TagService = game:GetService('TagService')
local taggedSnapshot = TagService:GetTagged('Enemy')
for _, unit in ipairs(taggedSnapshot) do
    print('当前索引中的对象:', unit.Name)
end
-- 动态新增对象请由业务层另行维护和同步,不要把该快照当作完整注册表