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

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
User avatar
Dopplespots
Prole
Posts: 4
Joined: Wed Nov 25, 2015 12:35 am

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

Post 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
Attachments
The timer is a massive double
The timer is a massive double
MassiveDouble.png (4.22 KiB) Viewed 5165 times
Last edited by Dopplespots on Sat Aug 12, 2017 8:37 pm, edited 1 time in total.
Well okay then.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Help with a Timer, Double, and Int

Post 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")
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

Re: Help with a Timer, Double, and Int

Post 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
User avatar
unek
Citizen
Posts: 86
Joined: Fri Oct 12, 2012 8:43 pm
Location: Poland
Contact:

Re: Help with a Timer, Double, and Int

Post by unek »

You can also use string.format.
DRAW DRAW - online painting app CLICK NUMBERS - cool game with numbers THEME LOVEFRAMES - a collection of my themes HOST FOR FREE - free hosting for online LOVE games STALK PEOPLE - a map of LOVE users
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Re: Help with a Timer, Double, and Int

Post 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")

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
User avatar
Dopplespots
Prole
Posts: 4
Joined: Wed Nov 25, 2015 12:35 am

Re: Help with a Timer, Double, and Int

Post by Dopplespots »

Thank you guys so much!
Well okay then.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 1 guest