Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
5

Custom classes with Instance.new? [closed]

Asked by
bobder2 135
10 years ago

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:

01Cat = {} --New cat base table
02 
03Cat.metatable = {__index = { --Setting the default values
04    alive = true,
05    legs = 4,
06    name = "Artemis"
07}}
08 
09Cat.new = function(specialsetvalues) --When you call this you can supply a table to change the default values
10    if specialsetvalues then --If you want to do what the comment above said then
11        setmetatable(specialsetvalues, Cat.metatable) --Sets the metatable of the optional table
12        return specialsetvalues --Returns it
13    end
14    specialsetvalues = {} --Makes a new table if you don't supply an optional table
15    setmetatable(specialsetvalues, Cat.metatable) --Sets that table's metatable
View all 22 lines...

This would output James

However, what I want is for this (Assuming all of the above code is referenced when using Instance.new("Cat")):

1jeff = Instance.new("Cat")
2jeff.name = "Jeff"
3print(jeff.name)

to output Jeff

Locked by sgsharp and BlueTaslem

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

2 answers

Log in to vote
2
Answered by 10 years ago

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.

01local CustomObjects = {Cat=Cat}
02 
03local OldInstance = Instance
04Instance = {["new"]= function(class,tab)
05    for o,n in pairs(CustomObjects) do
06        if class == o then
07            return n.new(tab)
08        end
09    end
10    OldInstance.new(class,tab)
11end}
12 
13james = Instance.new("Cat",{name = "james"})
14print(james.name) --Prints james' name
15print(james.legs) --Prints 4
16print(james.alive) --Prints true
17Instance.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.

01local OldInstance = Instance
02Instance = {["new"]= function(class,tab)
03    for o,n in pairs(getfenv()) do
04        if type(n) == "table" then
05            if n["new"] and class == o then
06                return n.new(tab)
07            end
08        end
09    end
10    OldInstance.new(class,tab)
11end}
0
Thanks! This is a pretty neat way of doing things! bobder2 135 — 10y
Ad
Log in to vote
1
Answered by
algo160 10
10 years ago

Unfortunatley, no.

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

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

0
Lame, thanks anyways. :p bobder2 135 — 10y