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

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
NKI
Prole
Posts: 2
Joined: Sun Jan 07, 2018 12:19 am

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

Post 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.
Attachments
issue.love
The entire project
(2.14 KiB) Downloaded 128 times
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

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

Post 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!
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
NKI
Prole
Posts: 2
Joined: Sun Jan 07, 2018 12:19 am

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

Post by NKI »

Thanks! I did not know the diference between the colon and the point 😁.
Post Reply

Who is online

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