Page 1 of 1

love.timer.getFPS() is sometimes returning a table and sometimes a number

Posted: Fri Jul 06, 2018 8:55 am
by NKI
Today i am making my own timer system in Love2d, but in one function to change the delay of the timer, when i use the love.timer.getFPS() it returns me a table and when i use it on a love.graphics.print() it returns me a number. This is actually what i am doing:

Code: Select all

	--Timer returning function
	function Timer(frames)
	return {
		frames = frames,
		update = function(fr)
			frames = fr
		end
	}
	
	local t = Timer(60)
	
	function love.update(dt)
		t:update(love.timer.getFPS()) -- This love.timer.getFPS returns me a table
	end
	
	function love.draw()
		love.graphics.print(love.timer.getFPS(), 0, 20) -- This returns me a number
	end
	
I am using Löve2d 0.11.0.
This is the simplified code, the .love is attached to the post.

Re: love.timer.getFPS() is sometimes returning a table and sometimes a number

Posted: Fri Jul 06, 2018 9:13 am
by zorg
Hi and welcome to the forums!

You are another member of the "not knowing how/when to use the colon (:) notation club"!

Now, that's not something to be sad about, but learning how it works helps!

Basically, you defined your update function (in the Timer constructor function) to have one parameter, fr.
Then, you are calling it with the colon notation, which will pass a hidden parameter, the table you called the function on, into the function before any other parameters.

In short, you are setting t.frames to be t itself.

the simplest fix would be to swap : out for a simple dot (.)

alternatively, if you do want "object orientation" flavor in your code, you could just write another parameter before fr in that function definition (call it self or obj or whatever you want, it doesn't matter, in this case, let's go with self), and set self.frames to fr instead!

Re: love.timer.getFPS() is sometimes returning a table and sometimes a number

Posted: Fri Jul 06, 2018 9:31 am
by NKI
Thanks! I did not know the diference between the colon and the point 😁.