I know it's a dumb question but I searched on the Internet and there's nothing that fits well for me.
Also I come from js where things wheren't so difficult

Code: Select all
function constructor(x, y)
return {
x = x or math.random()*100,
y = y or math.random()*100
}
end
-- Then create instances
local objects = {}
for i = 1, 10 do
objects[i] = constructor()
print(i, objects[i].x, objects[i].y)
end
Code: Select all
planet = {}
planet.proto = {
x = 0,
y = 0,
r = 15,
m = 1,
t = 0,
hasAtmosphere = true,
speed = 50,
}
planet.mt = {
__index = planet.proto
}
function planet.new(vars)
local set = {}
setmetatable(set, planet.mt)
for k, v in pairs(vars) do
set[k] = v
end
return set
end
Code: Select all
local planet1 = planet.new({x = 30, m = 2})
print(planet1.hasAtomosphere) -> true
print(planet1.x) -> 30
print(planet1.speed) -> 50
Code: Select all
local Class = setmetatable ( { }, { __call = function ( class, ... ) return class.new ( ... ) end } )
Class.__index = Class
Class.__call = function ( self ) return Class.new ( self ) end
function Class.new ( other )
local self = setmetatable ( { }, Class )
self.var = other and other.var or 0
return self
end
Users browsing this forum: Google [Bot] and 10 guests