HUMP class and arguments

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
c4p14in3_
Prole
Posts: 4
Joined: Wed Jan 04, 2017 10:52 pm

HUMP class and arguments

Post 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 :)
User avatar
Tjakka5
Party member
Posts: 243
Joined: Thu Dec 26, 2013 12:17 pm

Re: HUMP class and arguments

Post 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?
c4p14in3_
Prole
Posts: 4
Joined: Wed Jan 04, 2017 10:52 pm

Re: HUMP class and arguments

Post 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

User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: HUMP class and arguments

Post 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?
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: HUMP class and arguments

Post 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()
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: HUMP class and arguments

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

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
c4p14in3_
Prole
Posts: 4
Joined: Wed Jan 04, 2017 10:52 pm

Re: HUMP class and arguments

Post by c4p14in3_ »

Oh okay, thanks a lot !
Post Reply

Who is online

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