对象是 Roblox 类别层次中的所有类别的基础类。Roblox 引擎定义的每个其他类都继承了 Object 的所有成员。无法直接创建对象。
概要
方法
获取一个事件,当对象的某个属性发生变更时触发。
如果对象的类匹配或继承某个类,返回真值。
活动
在对象的属性发生变更后立即发射,但有一些限制。
属性
ClassName
一个只读的字符串,代表这个 Object 属于的类。
这个属性可以与各种用于识别对象类输入的其他函数一起使用,例如 Object:IsA() 或 Instance:FindFirstChildOfClass() 。
请注意,此属性仅供阅读,不能由脚本更改。想要更改对象类别的开发人员必须创建一个新的 Object 。
与 Object:IsA() 不同,ClassName 可用于检查对象是否属于特定类,忽略类继承。例如:
for _, child in workspace:GetChildren() doif child.ClassName == "Part" thenprint("Found a Part")-- will find Parts in model, but NOT TrussParts, WedgeParts, etcendend
方法
GetPropertyChangedSignal
该方法返回与 Changed 事件相同的行为的事件,除了它仅在指定的属性发生变化时发射。一般来说,使用此方法而不是连接到 Changed 以检查属性名称是一个好主意。对同一个对象上的相同属性名称的后续调用此方法返回相同的事件。
ValueBase 对象,例如 IntValue 和 StringValue , 使用修改的 Changed 事件,其内容为其 Value 属性的内容。因此,这个方法提供了检测这些对象其他属性的变化的方法。
请注意,这个事件不会向连接的函数传递任何参数,因此更改的属性值必须直接在脚本中阅读。
限制
由此方法返回的事件不会触发物理相关的更改,例如当 , , , , 或 物理改变的属性由于重力导致。要检测这些属性的变化,请考虑使用物理基础的事件,例如 RunService.PreSimulation 。
此外,返回的事件可能不会在很频繁地更改属性的每次修改中发射,或者可能不会在这些属性上发射。建议您仔细测试可能影响游戏逻辑的属性变更。
参数
要连接的属性。
返回
一种信号,每当属性发生变更时都会发射。
代码示例
This code sample demonstrates how to save a value before a changed event fires on it in order to get more information about a change.
local part = Instance.new("Part")
local currentColor = part.BrickColor
local function onBrickColorChanged()
local newColor = part.BrickColor
print("Color changed from", currentColor.Name, "to", newColor.Name)
currentColor = newColor
end
part:GetPropertyChangedSignal("BrickColor"):Connect(onBrickColorChanged)
part.BrickColor = BrickColor.new("Really red")
part.BrickColor = BrickColor.new("Really blue")
This code sample demonstrates the equivalence of the Changed event and event returned by GetPropertyChangedSignal.
local part = Instance.new("Part")
local function onBrickColorChanged()
print("My color is now " .. part.BrickColor.Name)
end
local function onChanged(property)
if property == "BrickColor" then
onBrickColorChanged()
end
end
part:GetPropertyChangedSignal("BrickColor"):Connect(onBrickColorChanged)
part.Changed:Connect(onChanged)
-- Trigger some changes (because we connected twice,
-- both of these will cause two calls to onBrickColorChanged)
part.BrickColor = BrickColor.new("Really red")
part.BrickColor = BrickColor.new("Institutional white")
IsA
IsA 返回 true 如果对象的类等于 或等于给定类的子类 。这个函数与其他语言中的 instanceof 运营商相似,是 类型反编译 的一种形式。要忽略类继承,直接测试 ClassName 属性。要检查本地 Luau 数据类型(数字、字符串等),请使用函数 type 和 typeof 。
最常见的情况下,这个函数被用来测试对象是否是某种部分,例如 Part 或 WedgePart ,这些对象从 BasePart (抽象类)继承。例如,如果您的目标是将角色的所有肢体更改为同一颜色,您可能会使用 GetChildren 循环过孩子们,然后使用 IsA 过滤缺少 BasePart 属性的非 BrickColor 对象:
local Players = game:GetService("Players")
local function paintFigure(character, color)
-- 循环过角色的子对象
for _, child in character:GetChildren() do
-- 过滤掉非零件对象,例如衬衫、裤子和人形
-- R15使用MeshPart和R6使用零件,因此我们在这里使用基础零件来检测两者:
if child:IsA("BasePart") then
child.BrickColor = color
end
end
end
paintFigure(Players.Player.Character, BrickColor.new("Bright blue"))
由于所有类都从 Object 继承,调用 object:IsA("Object") 将始终返回真值。
参数
对象的类将被检查的类别。区分大小写。
返回
描述对象的类是否匹配或是否是给定类的子类。
代码示例
Demonstrates determining the class of Workspace using Instance:IsA()
Workspace is of class 'Workspace', so the first statement is true. Workspace is not of class 'BasePart', so the second statement is false. Workspace inherits from Instance and therefore is of class 'Instance', so the third statement is true.
local Workspace = game:GetService("Workspace")
print(Workspace:IsA("Workspace")) -- true
print(Workspace:IsA("BasePart")) -- false
print(Workspace:IsA("Instance")) -- true
活动
Changed
该事件在对象属性更改后立即发生,并且在大多数情况下都能正常运行(请参阅下面的限制)。更改后的属性新值不是 传递为参数 ,因此必须使用object[property]。例如:
object.Changed:Connect(function(property)
print("The new property's value is", object[property])
end)
如果您仅对特定属性的更改感兴趣,请考虑使用 GetPropertyChangedSignal() 方法进行替换。
对于 ValueBase 对象,例如 IntValue 和 StringValue,此事件仅在对象的 Value 属性发生变更时触发。要检测 ValueBase 对象的其他变化,请使用 GetPropertyChangedSignal() 取代。
限制
此事件不会发射物理相关的更改,例如当 、 、 、 或 物理属性的改变由于重力导致。要检测这些属性的变化,请考虑使用物理基础的事件,例如 RunService.PreSimulation 。
此外,此事件可能不会在很频繁地更改属性的每次修改中触发,或者可能不会触发这全部属性。建议您仔细测试可能影响游戏逻辑的属性变更。
参数
更改的属性名称。
代码示例
This sample demonstrates the subtleties of the Changed event on normal objects and "-Value" objects.
-- Demonstrate the Changed event by creating a Part
local part = Instance.new("Part")
part.Changed:Connect(print)
-- This fires Changed with "Transparency"
part.Transparency = 0.5
-- Similarly, this fires Changed with "Number"
part.Name = "SomePart"
-- Since changing BrickColor will also change other
-- properties at the same time, this line fires Changed
-- with "BrickColor", "Color3" and "Color3uint16".
part.BrickColor = BrickColor.Red()
-- A NumberValue holds a double-precision floating-point number
local vNumber = Instance.new("NumberValue")
vNumber.Changed:Connect(print)
-- This fires Changed with 123.456 (not "Value")
vNumber.Value = 123.456
-- This does not fire Changed
vNumber.Name = "SomeNumber"
-- A StringValue stores one string
local vString = Instance.new("StringValue")
vString.Changed:Connect(print)
-- This fires Changed with "Hello" (not "Value")
vString.Value = "Hello"
This code sample demonstrates the Changed event firing within a parent object.
local object = script.Parent
local function onChanged(property)
-- Get the current value of the property
local value = object[property]
-- Print a message saying what changed
print(object:GetFullName() .. "." .. property .. " (" .. typeof(value) .. ") changed to " .. tostring(value))
end
object.Changed:Connect(onChanged)
-- Trigger a simple change in the object (add an underscore to the name)
object.Name = "_" .. object.Name