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

Csn constructors exist in a module object?

Asked by 9 years ago

Is this code legit in Lua?

local obj = {}
obj.new = function()

end
return obj

2 answers

Log in to vote
0
Answered by 9 years ago

Yes, it is completely legit. However, you may find it productive to simply return the constructor function unless you have a good reason for needing a table where the only member is a 'new' function.

0
Thanks a lit! Now I can make new APIs for my models of the game Aerodos12 70 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

You can do so.

local obj = {}
obj.__index = obj
obj.new = function(val)
    local newobj = {}
    setmetatable(newobj,obj)
    newobj.val = val
    return newobj
end

Now, you can create an object of that class.

b = obj.new("string")

Finally, print out the key called val.

print(b.val)

string

Answer this question