User:NXTBoy/Scripts/"new" operator: Difference between revisions
From Legacy Roblox Wiki
< User:NXTBoy | Scripts
>NXTBoy No edit summary |
>NXTBoy No edit summary |
||
Line 9: | Line 9: | ||
if type(properties) == "table" then | if type(properties) == "table" then | ||
for property, value in pairs(properties) do | for property, value in pairs(properties) do | ||
--Children | |||
if type(property) == "number" then | if type(property) == "number" then | ||
value.Parent = instance | value.Parent = instance | ||
--Events | |||
elseif type(value) == "function" then | |||
instance[property]:connect(function(...) value(instance, ...) end) | |||
--Properties | |||
else | else | ||
instance[property] = value | instance[property] = value |
Latest revision as of 09:37, 12 February 2012
_G.new = function(typeName) local instance -- Instances if pcall(function() instance = Instance.new(typeName) end) then print("Instance") return function(properties) if type(properties) == "table" then for property, value in pairs(properties) do --Children if type(property) == "number" then value.Parent = instance --Events elseif type(value) == "function" then instance[property]:connect(function(...) value(instance, ...) end) --Properties else instance[property] = value end end end return instance end -- Built in classes elseif getfenv()[typeName] and type(getfenv()[typeName].new) == "function" then return getfenv()[typeName].new -- User classes defined in _G elseif _G[typeName] and type(_G[typeName].new) == "function" then return _G[typeName].new -- What is this I don't even else error("Unknown type \""..typeName..."\"", 2) end end
Usage as follows:
local new = _G.new local m = new "Model" { Name = "Test", Parent = workspace, new "Part" { Name = "My Part", BrickColor = new "BrickColor"("Bright red"), Size = new "Vector3"(2, 2, 2) } } local p = new "Part"() p.Parent = m