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

Is it possible to create a code Interface?

Asked by 9 years ago

By this I mean Lua already has Object oriented Programming, especially using Roblox's ModuleScripts. Is it possible to create an Interface, much like Java, so objects can implement undefined functions?

0
could you provide a hypothetical example? 1waffle1 2908 — 9y
0
what do you mean by implement undefined functions? 1waffle1 2908 — 9y
0
Take a look at roblox's Part. It inherits all properties of FormFactorPart, which inherits properties of BasePart, which inherits PVInstance, Which inherits Instance. randomsmileyface 375 — 9y
0
Do you mean creating objects in plain Lua, or do you mean changing actual Roblox objects? because Roblox doesn't let you change (or access) any of the already existing ones. 1waffle1 2908 — 9y
View all comments (2 more)
0
I mean creating objects with inheritance in Lua. randomsmileyface 375 — 9y

2 answers

Log in to vote
0
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
9 years ago

It is possible. Here's a Roblox wiki article on OOP: http://wiki.roblox.com/index.php?title=OOP

For inheritance, define a class instantiation function in terms of a superior class. Here's an example:

vector={
    new=function(...)
        local components={...}
        local length=#components
        return setmetatable({class="vector"},{
            __index=function(t,k)
                if type(k)=="number"then
                    return components[k]
                end
            end,
            __newindex=function(t,k,v)
                if type(k)=="number"then
                    if type(v)=="number"then
                        components[k]=v
                        return
                    else
                        error("Must be a number")
                    end
                end
                error("Cannot set "..k)
            end,
            __add=function(a,b)
                if a.class==b.class and a.length==b.length then
                    local t={}
                    for i=1,#a do
                        t[i]=a[i]+b[i]
                    end
                    return vector.new(unpack(t))
                end
            end,
            __sub=function(a,b)
                if a.class==b.class and a.length==b.length then
                    local t={}
                    for i=1,#a do
                        t[i]=a[i]-b[i]
                    end
                    return vector.new(unpack(t))
                end
            end,
            __tostring=function()
                return table.concat(components,", ")
            end
        })
    end
}

vector3={
    new=function(x,y,z)
        local object=vector.new(x,y,z)
        rawset(object,"magnitude",math.sqrt(x*x+y*y+z*z))
        rawset(object,"class","vector3")
        local meta=getmetatable(object)
        meta.__add=function(a,b)
            if a.class==b.class then
                return vector3.new(a.x+b.x,a.y+b.y,a.z+b.z)
            end
        end
        meta.__sub=function(a,b)
            if a.class==b.class then
                return vector3.new(a.x-b.x,a.y-b.y,a.z-b.z)
            end
        end
        meta.__index=function(t,k)
            if k=="x"then return x
            elseif k=="y"then return y
            elseif k=="z"then return z
            end
        end
        local old=meta.__newindex
        meta.__newindex=function(t,k,v)
            if type(v)=="number"then
                if k=="x"or k=="y"or k=="z"then
                    if k=="x"then
                        x=v
                    elseif k=="y"then
                        y=v
                    elseif k=="z"then
                        z=v
                    end
                    rawset(object,"magnitude",math.sqrt(x*x+y*y+z*z))
                    return
                end
            end
            return old(t,k,v)
        end
        return object
    end
}

local v=vector3.new(12,23,34)
print(v.x)
local a=vector3.new(23,12,1)
local b=v+a
print(b)
0
If you want to create a userdata object like Roblox, you can use newproxy, but it's been deprecated since the creation of Lua 5.1 (which roblox uses.) 1waffle1 2908 — 9y
0
Yeah I believe the link you posted also uses newProxy in order to create objects randomsmileyface 375 — 9y
0
But I need to ask, does this work with private variables (AKA, variables bound to the .new function)? I randomsmileyface 375 — 9y
0
Because if I don't use private variables, using your above script, I can do some stupid thing like v.magnitude = "YOLO" and magnitude's value is overriden with YOLO randomsmileyface 375 — 9y
0
If you store variables inside the function and not in the table and you set __newindex so variables can't be set, that would work. 1waffle1 2908 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

Without the help of an external library (or writing your own if you're so inclined), no this isn't possible. There are libraries in Lua that do make this possible, but it is not a feature of Lua. Lua is not intended to be object oriented.

0
Lua has metatables and creatable userdata. It is possible. 1waffle1 2908 — 9y
0
@1waffle1 Right, I didn't say it wasn't possible, I said you'd have to write your own implementation for it, or use somebody else's implementation, which is completely accurate. Object oriented programming/inheritance is not feature of Lua, but you can simulate it. DreadPirateRobux 655 — 9y
0
When I said external libraries, I meant something like SECL ( https://github.com/bartbes/love-misc-libs/blob/master/SECL/full.lua ) DreadPirateRobux 655 — 9y

Answer this question