Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Custom classes with Instance.new?

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

2 answers

✓ Accepted answer

You cannot directly modify the Instance.new function but you may replace it with your own. First I save a copy of the Instance table in the variable OldInstance. This gives us the ability to be able to create instances after the table has been reassigned.

In this script all the custom functions that you want it to work withit must be placed inside the table.

local CustomObjects = {Cat=Cat}

local OldInstance = Instance
Instance = {["new"]= function(class,tab)
	for o,n in pairs(CustomObjects) do
		if class == o then
			return n.new(tab)
		end
	end
	OldInstance.new(class,tab)
end}

james = Instance.new("Cat",{name = "james"})
print(james.name) --Prints james' name
print(james.legs) --Prints 4
print(james.alive) --Prints true
Instance.new("Part",Workspace) --Proving that using the function normally still works.

This is a variant of the script above that searches all the variables in the environment to see if a table has the key new. If it does it treats it as a custom object. If you wish you may use a different key to authenticate a table as a custom object so there are no mix ups.

This script will be less efficient than the one above.

local OldInstance = Instance
Instance = {["new"]= function(class,tab)
	for o,n in pairs(getfenv()) do
		if type(n) == "table" then
			if n["new"] and class == o then
				return n.new(tab)
			end
		end
	end
	OldInstance.new(class,tab)
end}

1 comment

0

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

Unfortunatley, no.

Roblox has true classes, and "jeff" is just a metatable.

Unfortunatley, metatables and Roblox Classes may never mix.....

1 comment

0

Lame, thanks anyways. :p

This discussion is preserved as it was. New answers, comments, and votes are closed.