對象是 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 返回真值,如果對物件的類別與 相等於 或是 子類別 的給定類別。此功能與其他語言中的 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