枚数

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

列表数据输入型 枚列Enum,是一个固定的列表项目。您可以通过称为 Enum 的全球对象访问枚数。要查看完整列表的枚和其项目,请参阅 API 参考中的

获取枚举项目

要获取枚数的所有项目,请在枚数上调用 GetEnumItems() 方法。以下代码示例显示了如何调用 GetEnumItems() 枚列上的 Enum.PartType


local partTypes = Enum.PartType:GetEnumItems()
for index, enumItem in partTypes do
print(enumItem)
end
--[[
Enum.PartType.Ball
Enum.PartType.Block
Enum.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) -- 2
print(Enum.PartType.Cylinder.EnumType) -- PartType

分配枚举项目

要将 EnumItem 作为属性值,使用完整的 Enum 声明。您也可以使用其 ValueEnumType


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