How to make a simple Constructor in lua

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Aconeron
Prole
Posts: 2
Joined: Sun Apr 16, 2017 5:29 pm

How to make a simple Constructor in lua

Post by Aconeron »

Hi guys, I'm here to ask what I have to do to make a constructor in lua.
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 :D
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Re: How to make a simple Constructor in lua

Post by Beelz »

Bare minimum could be something along the lines of:

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

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
User avatar
Davidobot
Party member
Posts: 1226
Joined: Sat Mar 31, 2012 5:18 am
Location: Oxford, UK
Contact:

Re: How to make a simple Constructor in lua

Post by Davidobot »

While what the above poster is certainly a solution, a more elegant one can be achieved with metatables, and more specifically, the __index part of metatables.
There is what is called a "prototype". They are exactly what you ate looking for - default values for a table.

EDIT:
Example usage:

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
This way you can pass only the variables you need to planet.new. Also make sure not to add the sets to the table where the metatable is stored.

Usage:

Code: Select all

local planet1 = planet.new({x = 30, m = 2})
print(planet1.hasAtomosphere) -> true
print(planet1.x) -> 30
print(planet1.speed) -> 50
PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: How to make a simple Constructor in lua

Post by raidho36 »

I just do it like this.

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
So if you call class as a function, it produces new instance. And if you call instance as a function, it produces its clone.
Aconeron
Prole
Posts: 2
Joined: Sun Apr 16, 2017 5:29 pm

Re: How to make a simple Constructor in lua

Post by Aconeron »

Thanks everyone! I didn't excpect some much answers!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 4 guests