Page 1 of 1

HUMP class and arguments

Posted: Sat Feb 04, 2017 10:58 am
by c4p14in3_
Hello, I've got a "problem" with HUMP class lib
What I want to do is :

Code: Select all

Obj = class{}

function Obj:init()
	self.x = 100
	self.add = function(var) self.x = self.x + var end
end
I know I can do :

Code: Select all

function Obj:add(var)
	self.x = self.x + var
end
But I don't like this syntax ...

So i'd like to know if there is another class lib that support my (horrible) syntax, thanks :)

Re: HUMP class and arguments

Posted: Sat Feb 04, 2017 11:09 am
by Tjakka5

Code: Select all

local Class = require("hump.class")

local Obj = Class()
function Obj:init()
   self.x = 100
end

function Obj:add(n)
   self.x = self.x + n
end

return Obj
This is the only real way you can do it.
The first example you gave creates a new function for every object, which kind of defeats the point of the Class system.

What do you not like about the syntax?

Re: HUMP class and arguments

Posted: Sat Feb 04, 2017 1:30 pm
by c4p14in3_
Because when I use the HUMP/timer lib I need to use the class constructor to declare function.


This don't work:

Code: Select all

Obj = class{}

function Obj:init()
	self.x = 0
end

function Obj:timeFunction()
	self.x = self.x + self.var
end

function Obj:add(var)
	self.var = var
	timer.after(1, self.timeFunction)
end
But this work:

Code: Select all

Obj = class{}

function Obj:init()
	self.x = 0
	self.timeFunction = function() self.x = self.x + self.var end
end

function Obj:add(var)
	self.var = var
	timer.after(1, self.timeFunction)
end
It's no big deal since I can make every method who don't need argument in the constructor but I can do what I want without using a library so I was wondering if I could do it with. (like this)

Code: Select all

local Obj = {}
	function Obj.new()
		local self = {}
			self.x = 0
			self.func = function(var) self.x + var end
		return self
	end
	
return Obj


Re: HUMP class and arguments

Posted: Sat Feb 04, 2017 3:45 pm
by bartbes
c4p14in3_ wrote:

Code: Select all

Obj = class{}

function Obj:init()
	self.x = 100
	self.add = function(var) self.x = self.x + var end
end
I don't have a copy of hump.class lying around so I'm too lazy to test it, but that looks like it would work. What's the problem?

Re: HUMP class and arguments

Posted: Sat Feb 04, 2017 4:07 pm
by Inny
You don't need a class library to do your syntax.

Code: Select all

function Obj()
  local self = {}
  self.x = 100
  self.add = function(var) self.x = self.x + var end
  return self
end

instance = Obj()

Re: HUMP class and arguments

Posted: Sat Feb 04, 2017 7:46 pm
by s-ol
Your problem doesn't lie in hump.class, it's how you use timer.after. timer.after doesn't add a 'self' argument so you need to wrap/bind your function:

Code: Select all

Obj = class{}

function Obj:init()
   self.x = 0
end

function Obj:timeFunction()
   self.x = self.x + self.var
end

function Obj:add(var)
   self.var = var
   timer.after(1, function() self:timeFunction() end)
end
Also i wouldn't recommend to store 'var' in self for the one second, because if :add() get's called a lot there will be bugs.

Re: HUMP class and arguments

Posted: Sun Feb 05, 2017 12:30 am
by c4p14in3_
Oh okay, thanks a lot !