列表数据输入型 枚列 或 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 的属性。
-- 枚列项目 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