CollectionService 管理具有 标签 的实例群组(收集)。标签是用于从服务器复制到客户端的实例的一组字符串。当保存地点时,它们也会序列化。
CollectionService 的主要用途是注册具有特定标签的实例,您可以使用它们来扩展其行为。如果你发现自己在很多不同的实例上添加了相同的脚本,使用 CollectionService 的脚本可能会更好。
标签可以通过此类方法添加或移除,例如 AddTag() 或 RemoveTag() 。它们还可以直接在 Studio 中管理通过实例属性的 标签 部分,或通过内置的 标签编辑器 工具。
复制
当标签复制时, 所有标签在实例上同时复制 。因此,如果您从客户端设置标签在实例上,然后从服务器上添加/删除不同的标签 不同 标签在同一实例上,客户端的本地标签在实例上将被覆盖。在 StreamingEnabled 地点,实例可以在离开客户端的流区时卸载。如果这样的实例重新进入传输区域,属性和标签将从服务器重新同步。这可能导致由 LocalScripts 所做的更改被覆盖/删除。
概要
方法
对 Instance 应用标签。
返回体验中的所有标签的阵列。
返回一个信号,该信号在给定标签被添加到实例时发射。
返回一个信号,该信号在给定标签从实例中删除时发生。
返回游戏中带有给定标签的实例阵列。
获取给定实例应用的所有标签的阵列。
检查实例是否有给定的标签。
从实例中移除标签。
活动
当标签被添加到实例时发生火灾,添加的标签是该标签在该场景的唯一出现。
当标签从实例中删除时,移除的标签已不再在任何地场景使用。
属性
方法
AddTag
该方法对 Instance 应用标签,如果标签已应用到该实例,则不做任何操作。成功添加标签会触发由 GetInstanceAddedSignal() 创建的信号,其标签与指定的标签相同。
警告
服务器稍后添加或移除了该实例上的标签,客户端添加的标签将被丢弃,因为服务器复制所有标签并覆盖以前的标签。
当标记实例时,常见的是一些资源被用来给标签提供功能,例如事件连接或表。为了防止内存泄漏,清理这些(断开、设置为 nil 等)当标签不再需要时是个好主意。在调用 RemoveTag() 、调用 Instance:Destroy() 或在与 GetInstanceRemovedSignal() 返回的信号连接的函数中执行时,执行此操作。
参数
返回
GetInstanceAddedSignal
给予标签(字符串),此方法返回一个信号,其在两个条件下发射:
标签被分配到 DataModel 中的实例使用 CollectionService:AddTag() 或 Instance:AddTag() 。
带有给定标签的实例被添加为 DataModel 的子孙,例如通过设置 Instance.Parent 或类似来。
使用相同标签对此方法进行后续调用返回相同的信号对象。还需要考虑调用 GetTagged() 以获取已拥有标签的实例列表(因此如果它们已经在 DataModel 中,将不会触发事件)。
还看到 GetInstanceRemovedSignal() ,返回在类似条件下发生的事件。
参数
要观察的标签。
返回
当您将标签添加到实例时发生的事件。
代码示例
This code sample causes any BasePart with the tag Deadly to kill any Humanoid that touches it. It does this using a common pattern with CollectionService to listen for all parts with the tag and make a connection, then disconnect the connection when the tag is removed.
local CollectionService = game:GetService("CollectionService")
local tag = "Deadly"
local function onDeadlyPartTouched(otherPart)
if not otherPart.Parent then
return
end
local humanoid = otherPart.Parent:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid.Health = 0
end
end
-- Save the connections so they can be disconnected when the tag is removed
local connections = {}
local function onInstanceAdded(object)
-- Confirm that the object with this tag is a BasePart
if object:IsA("BasePart") then
connections[object] = object.Touched:Connect(onDeadlyPartTouched)
end
end
local function onInstanceRemoved(object)
-- If there is a stored connection on this object, disconnect/remove it
if connections[object] then
connections[object]:Disconnect()
connections[object] = nil
end
end
-- Listen for this tag being applied to objects
CollectionService:GetInstanceAddedSignal(tag):Connect(onInstanceAdded)
CollectionService:GetInstanceRemovedSignal(tag):Connect(onInstanceRemoved)
-- Also detect any objects that already have the tag
for _, object in pairs(CollectionService:GetTagged(tag)) do
onInstanceAdded(object)
end
GetInstanceRemovedSignal
给予标签(字符串),此方法返回一个信号,其在两个条件下发射:
标签从 DataModel 中的实例中移除,使用 CollectionService:RemoveTag() 或 Instance:RemoveTag() 。
带有给定标签的实例被移除为 DataModel 的后裔,例如通过取消 Instance.Parent 或类似操作。
使用相同标签对此方法进行后续调用返回相同的信号对象。信号对于清理曾经拥有标签的实例使用的资源有用,例如断开连接。
还看到 GetInstanceAddedSignal() ,返回在类似条件下发生的事件。
参数
要观察的标签。
返回
当您从实例中删除标签时发生的事件。
代码示例
This code sample causes any BasePart with the tag Deadly to kill any Humanoid that touches it. It does this using a common pattern with CollectionService to listen for all parts with the tag and make a connection, then disconnect the connection when the tag is removed.
local CollectionService = game:GetService("CollectionService")
local tag = "Deadly"
local function onDeadlyPartTouched(otherPart)
if not otherPart.Parent then
return
end
local humanoid = otherPart.Parent:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid.Health = 0
end
end
-- Save the connections so they can be disconnected when the tag is removed
local connections = {}
local function onInstanceAdded(object)
-- Confirm that the object with this tag is a BasePart
if object:IsA("BasePart") then
connections[object] = object.Touched:Connect(onDeadlyPartTouched)
end
end
local function onInstanceRemoved(object)
-- If there is a stored connection on this object, disconnect/remove it
if connections[object] then
connections[object]:Disconnect()
connections[object] = nil
end
end
-- Listen for this tag being applied to objects
CollectionService:GetInstanceAddedSignal(tag):Connect(onInstanceAdded)
CollectionService:GetInstanceRemovedSignal(tag):Connect(onInstanceRemoved)
-- Also detect any objects that already have the tag
for _, object in pairs(CollectionService:GetTagged(tag)) do
onInstanceAdded(object)
end
GetTagged
该方法返回一个阵列的实例,其标签为 DataModel 的子孙。使用 CollectionService:RemoveTag() 或 Instance:RemoveTag() 移除标签可确保此方法不会返回它们。
如果您想检测标签的所有实例, both present 和 未来,使用此方法循环过实例,同时还与由 GetInstanceAddedSignal() 返回的信号进行连接。
该方法不保证返回的实例的任何顺序。此外,实例可能会被分配给它们指定的标签,但不是 DataModel 的子孙,例如其父是 nil;此方法不会返回这些实例。
参数
要搜索的标签。
返回
带有标签的所有实例的阵列。
代码示例
This code sample causes any BasePart with the tag Deadly to kill any Humanoid that touches it. It does this using a common pattern with CollectionService to listen for all parts with the tag and make a connection, then disconnect the connection when the tag is removed.
local CollectionService = game:GetService("CollectionService")
local tag = "Deadly"
local function onDeadlyPartTouched(otherPart)
if not otherPart.Parent then
return
end
local humanoid = otherPart.Parent:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid.Health = 0
end
end
-- Save the connections so they can be disconnected when the tag is removed
local connections = {}
local function onInstanceAdded(object)
-- Confirm that the object with this tag is a BasePart
if object:IsA("BasePart") then
connections[object] = object.Touched:Connect(onDeadlyPartTouched)
end
end
local function onInstanceRemoved(object)
-- If there is a stored connection on this object, disconnect/remove it
if connections[object] then
connections[object]:Disconnect()
connections[object] = nil
end
end
-- Listen for this tag being applied to objects
CollectionService:GetInstanceAddedSignal(tag):Connect(onInstanceAdded)
CollectionService:GetInstanceRemovedSignal(tag):Connect(onInstanceRemoved)
-- Also detect any objects that already have the tag
for _, object in pairs(CollectionService:GetTagged(tag)) do
onInstanceAdded(object)
end
GetTags
给予一个 Instance , 这个方法返回一个阵列的字符串,这些字符串是应用于实例的标签。
当您想要一次使用多个实例标签时,这个方法很有用,但检查单个标签的存在是不高效的。为此,使用 HasTag() 检查单个标签。
参数
标签应返回的实例。
返回
给定实例应用的标签的一组字符串。
代码示例
This code sample demonstrates adding, removing and querying a tag from an object using CollectionService.
local CollectionService = game:GetService("CollectionService")
local Workspace = game:GetService("Workspace")
local object = Workspace.Part
-- Add a tag
CollectionService:AddTag(object, "Deadly")
-- Query for a tag
if CollectionService:HasTag(object, "Deadly") then
print(object:GetFullName() .. " is deadly")
end
-- List tags on an object
local tags = CollectionService:GetTags(object)
print("The object " .. object:GetFullName() .. " has tags: " .. table.concat(tags, ", "))
-- Remove a tag
CollectionService:RemoveTag(object, "Deadly")
HasTag
该方法返回是否给定的 Instance 有标签。
通过扩展,任何由调用 GetTags() 返回的标签在实例上使用此方法时将返回 true 。
参数
返回
实例是否拥有标签。
代码示例
This code sample demonstrates adding, removing and querying a tag from an object using CollectionService.
local CollectionService = game:GetService("CollectionService")
local Workspace = game:GetService("Workspace")
local object = Workspace.Part
-- Add a tag
CollectionService:AddTag(object, "Deadly")
-- Query for a tag
if CollectionService:HasTag(object, "Deadly") then
print(object:GetFullName() .. " is deadly")
end
-- List tags on an object
local tags = CollectionService:GetTags(object)
print("The object " .. object:GetFullName() .. " has tags: " .. table.concat(tags, ", "))
-- Remove a tag
CollectionService:RemoveTag(object, "Deadly")
RemoveTag
该方法从实例中移除标签。成功移除标签会触发由 GetInstanceRemovedSignal() 创建的信号,其中包含给定的标签。
当移除标签时,常见的是一些资源被用来给标签提供功能,例如事件连接或表。为了防止内存泄漏,清理这些(断开、设置为 nil 等)当标签不再需要时是个好主意。
参数
返回
代码示例
This code sample demonstrates adding, removing and querying a tag from an object using CollectionService.
local CollectionService = game:GetService("CollectionService")
local Workspace = game:GetService("Workspace")
local object = Workspace.Part
-- Add a tag
CollectionService:AddTag(object, "Deadly")
-- Query for a tag
if CollectionService:HasTag(object, "Deadly") then
print(object:GetFullName() .. " is deadly")
end
-- List tags on an object
local tags = CollectionService:GetTags(object)
print("The object " .. object:GetFullName() .. " has tags: " .. table.concat(tags, ", "))
-- Remove a tag
CollectionService:RemoveTag(object, "Deadly")