Page 1 of 1

Help with a Timer, Double, and Int [Solved]

Posted: Mon Dec 14, 2015 9:55 pm
by Dopplespots
Hello! I am very new to LÖVE, and I am having a little bit of trouble with a timer I am trying to make.

Here is my code:

Code: Select all

IntroTimer = true
StartTimer = 0

function love.load()
	
	Mainfont = love.graphics.newFont("Graphics/PixelFont.ttf", 22)
	love.graphics.setBackgroundColor(255, 255, 255)
	love.graphics.setColor(0, 0, 0)
	love.graphics.setFont(Mainfont)

end

function love.update(dt)

	if IntroTimer then
		
		StartTimer = StartTimer + dt
		
	end
	
end

function love.draw()

	love.graphics.printf("Hey.", 20, 250, 458, "center")
	love.graphics.printf(StartTimer, 10, 10, 300, "left")

end
When I print the timer in the love.draw function, on the screen it appears as a massive double!
Does anyone know/have any idea on how to make the timer just single seconds, or is that impossible?
Please help! Thank you!

-Dopplespots

Re: Help with a Timer, Double, and Int

Posted: Mon Dec 14, 2015 10:32 pm
by micha
Hi and welcome to the forum!

You can round numbers with the math.floor function (this will cut of everything after the decimal point):

Code: Select all

love.graphics.printf(math.floor(StartTimer), 10, 10, 300, "left")

Re: Help with a Timer, Double, and Int

Posted: Tue Dec 15, 2015 3:23 am
by bobbyjones
Well dt is generally below a second. But yeah you could round to a couple decimals. Use one of the functions foune here to round it to a visually pleasing format. http://lua-users.org/wiki/SimpleRound

Re: Help with a Timer, Double, and Int

Posted: Tue Dec 15, 2015 10:52 am
by unek
You can also use string.format.

Re: Help with a Timer, Double, and Int

Posted: Tue Dec 15, 2015 1:54 pm
by Beelz
I myself would also use format, as I feel it looks much cleaner... This will allow 2 digits after the decimal.

Code: Select all

local timer = string.format("%.2f", StartTimer)
love.graphics.printf(timer, 10, 10, 300, "left")

Re: Help with a Timer, Double, and Int

Posted: Wed Dec 16, 2015 3:27 am
by Dopplespots
Thank you guys so much!