This inspired me to write a small thing for setting lots of properties or running many methods at once with syntax similar to what you’re looking for.
I don’t know if I’d ever actually use this. Too weird with too many hidden behaviors for me. I’ll probably stick with explicit loops. Here’s what the syntax looks like:
collect(Part1, Part2).BrickColor = collect(BrickColor.new('Really red'), BrickColor.new('Cyan'))
A more extended example
local Part1 = Instance.new("Part")
local Part2 = Instance.new("Part")
local parts = collect(Part1, Part2)
parts.Name = collect("Part 1", "Part 2")
parts.BrickColor = collect(BrickColor.new('Really red'), BrickColor.new('Cyan'))
parts.Transparency = 0.5
parts.TopSurface = "Smooth"
parts.BottomSurface = "Smooth"
parts.Anchored = true
parts.CFrame = collect(CFrame.new(2, 2, 0), CFrame.new(-2, 2, 0))
parts.Parent = workspace
wait(1)
parts:Resize(collect({Enum.NormalId.Top, 1}, {Enum.NormalId.Back, 2}))
wait(1)
parts:Destroy()
The programming behind collect
local collectMeta
collectMeta = {
__newindex = function(this, key, value)
if type(value) == "table" and getmetatable(value) == collectMeta then
for i = 1, #this do
this[i][key] = value[(i - 1)%(#value) + 1]
end
else
for i = 1, #this do
this[i][key] = value
end
end
end,
__index = function(this, key)
return function(innerThis, val1, ...)
if innerThis == this then
if type(val1) == "table" and getmetatable(val1) == collectMeta then
for i = 1, #this do
this[i][key](this[i], unpack(val1[(i - 1)%(#val1) + 1]))
end
else
for i = 1, #this do
this[i][key](this[i], val1, ...)
end
end
elseif type(innerThis) == "table" and getmetatable(innerThis) == collectMeta then
for i = 1, #this do
this[i][key](unpack(innerThis[(i - 1)%(#innerThis) + 1]))
end
else
for i = 1, #this do
this[i][key](innerThis, val1, ...)
end
end
end
end
}
local function collect(...)
return setmetatable({...}, collectMeta)
end
return collect
Some neat things about collect
:
- You can set all properties to the same thing using
.Property = value
- You can set all properties to different things with
.Property = collect(value1, value2, ...)
- You can set all properties such that it’s a repeating pattern with
.Property = collect(value1, value2)
. For the third object, it will use value1, for the fourth, value2, for the fifth, value1, etc.
- You can call methods with the same features as above
- You can call methods referencing the objects using
:Method(...)
- You can call methods not referencing the objects using
.Method(...)
- You can get the values back using
unpack(collection)
- e.g.
Part1, Part2 = unpack(parts)
- You can loop through the collection just like you would a regular list.
Lua is pretty flexible, isn’t it?