列舉 資料類輸入 或 Enum,是一個固定的項目列表。您可以通過稱為 Enum 的全球對象來存取枚值。要查看完整的枚列和其項目列表,請參閱 枚列 在 API 參考中。
取得枚列項目
要取得枚列的所有項目,請在枚列上呼叫 GetEnumItems() 方法。以下代碼示例顯示如何呼叫 GetEnumItems() 枚列上的 Enum.PartType 。
local partTypes = Enum.PartType:GetEnumItems()for index, enumItem in partTypes doprint(enumItem)end--[[Enum.PartType.BallEnum.PartType.BlockEnum.PartType.Cylinder]]
數據類輸入
EnumItem 是枚列中項目的數據類型。EnumItem 有三個屬性:
對象的某些屬性只能是某些枚列的項目。例如,一個 Shape 物件的 Part 屬性是 Enum.PartType Enum 的一個項目。以下代碼示例顯示如何打印 Enum.PartType.Cylinder EnumItem 的屬性。
-- EnumItem 稱為 Enum.PartType.Cylinder 的屬性print(Enum.PartType.Cylinder.Name) -- 圓筒print(Enum.PartType.Cylinder.Value) -- 2print(Enum.PartType.Cylinder.EnumType) -- PartType
指派枚列項目
要將 EnumItem 指定為屬性性值,請使用完整的 Enum 宣言。您也可以使用其 Value 或 EnumType 。
local Workspace = game:GetService("Workspace")local part = Instance.new("Part") -- 創建新零件part.Shape = Enum.PartType.Cylinder -- 由 EnumItem (最佳做法)part.Shape = Enum.PartType.Cylinder.Value -- 由 EnumItem 值part.Shape = 2 -- 由 EnumItem 值part.Shape = Enum.PartType.Cylinder.Name -- 依 EnumItem 名稱part.Shape = "Cylinder" -- 依 EnumItem 名稱part.Parent = Workspace