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

Is it possible to create a code Interface?

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?

6 comments

0

could you provide a hypothetical example?

0

what do you mean by implement undefined functions?

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.

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.

0

I mean creating objects with inheritance in Lua.

2 answers

✓ Accepted answer

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)

5 comments

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.)

0

Yeah I believe the link you posted also uses newProxy in order to create objects

0

But I need to ask, does this work with private variables (AKA, variables bound to the .new function)? I

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

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.

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.

3 comments

0

Lua has metatables and creatable userdata. It is possible.

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.

0

When I said external libraries, I meant something like SECL ( https://github.com/bartbes/love-misc-libs/blob/master/SECL/full.lua )

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