I've recently discovered the usefulness of metatables and I was wondering if it's possible to make new objects available to instance.new with them. Here's an example of what I'm wanting because I'm horrible at explaining things with kitties for an example:
Cat = {} --New cat base table
Cat.metatable = {__index = { --Setting the default values
alive = true,
legs = 4,
name = "Artemis"
}}
Cat.new = function(specialsetvalues) --When you call this you can supply a table to change the default values
if specialsetvalues then --If you want to do what the comment above said then
setmetatable(specialsetvalues, Cat.metatable) --Sets the metatable of the optional table
return specialsetvalues --Returns it
end
specialsetvalues = {} --Makes a new table if you don't supply an optional table
setmetatable(specialsetvalues, Cat.metatable) --Sets that table's metatable
return specialsetvalues --Returns it
end
james = Cat.new({name = "James"}) --Creates a new cat with the variable james, his name is also James
print(james.name) --Prints james' name
print(james.legs) --Prints 4
print(james.alive) --Prints true
This would output James
However, what I want is for this (Assuming all of the above code is referenced when using Instance.new("Cat")):
jeff = Instance.new("Cat")
jeff.name = "Jeff"
print(jeff.name)
to output Jeff

1 comment
Thanks! This is a pretty neat way of doing things!