对象是 Roblox 类型系列的基础类。 所有其他类型的类别都继承了所有对象的成员。 不能直接创建对象。
概要
方法
获取当对象的某个属性发生变更时触发的事件。
如果对象的类型与指定的类型匹配或继承,返回 true。
活动
在对象的属性变更后立即发射,并且有一些限制。
属性
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 用检查属性名称的函数。随后对该方法在同一对象上使用时,返回同一事件。
Class.ValueBase 对象,例如 IntValue 和 StringValue ,使用修改的 0> Class.Object.Changed|Changed0> 事件,该发射其 ValueBase3> 属性的内容。因此,此方法提供了检测其他属性对象的方法。
注意,该事件不会将任何参数传递给连接的函数,因此更改的属性必须在脚本中直接读取。
限制
这个方法返回的事件不会为物理相关更改,例如当 Class.BasePart.
此外,返回的事件可能不会在更常更改属性的情况下发生,或/或它可能不会发生为此类属性。 建议您仔细测试影响游戏逻辑的属性更改。
参数
连接到的属性。
返回
一个信号,每当属性发生变更时触发。
代码示例
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 如果对象的类是 相当于 或给定类的子类。 此函数与其他语言中的 继承操作器 类似,并且是 类型检查 的形式。
最常见的情况是,这个函数用于测试对象是否是某种部件,例如 Part 或 WedgePart ,其继承自
local function paintFigure(character, color)
-- 遍过角色子对象的子对象
for _, child in character:GetChildren() do
-- 过滤非零件对象,例如衬衫、裤子和人形
-- R15 使用 MeshPart 和 R6 使用 Part,所以我们在这里使用 BasePart 来检测 both:
if child:IsA("BasePart") then
child.BrickColor = color
end
end
end
paintFigure(game.Players.Player.Character, BrickColor.new("Bright blue"))
由于所有类从 Object 继承,调用 object:IsA("Object") 总是会返回 true。
参数
对象的类会检查的对象类。 敏感于大小。
返回
描述是否匹配对象的类或是否是给定类的子类。
代码示例
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,此事件仅发生 when 1> Class.ValueBase1> 对象的 4> Value4> 属性发生变更。 要检测其他变更在 7> Class.Value
限制
此事件不会为物理相关更改,例如当 CFrame
此外,此事件可能不会在每个更改属性的频繁修改后发生,或/或它可能不会发生。 建议您在更改游戏逻辑的属性影响下测试的时候,请小心测试。
参数
更改的属性的名称。
代码示例
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