A
Size: a a a
A
A
A
S
function randenum(enum, len)
local key, value = next(enum)
for i = 1, math.random(len) do
key, value = next(enum, key)
if not key then -- зацикливание
key, value = next(enum)
end
end
return key, value
end
local color, value = randenum(brickColorEnum, 5)
Рулеточка крутится, рандомная фигня мутится.A
A
S
D
D
D
D
D
S
function Enum(...)
local o = setmetatable({}, {
__newindex = function(self, key, value)
local index = type(value) == 'number' and value or #self + 1
rawset(self, key, value)
rawset(self, index, key)
end
})
for i = 1, select("#", ...) do
o[select(i, ...)] = i
end
end
color = Enum()
color.red = 1
color.green = 2
color.blue = true
print(color[3]) --> "blue"
print(#color) --> 3
Тут правда крайне не желательно добавлять дырки, типа color.red = 1Потому что следующие ключи без индекса, вроде
color.green = 255
color.blue = trueмогут добавиться как на позицию 2 так и в 256, в зависимости от луёвой реализации.
BC
BC
YK
BC
VB