How to shorten a string?

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
BoopDeePoop
Prole
Posts: 39
Joined: Sun Dec 30, 2012 4:15 am

How to shorten a string?

Post by BoopDeePoop »

Here is what I have:

Code: Select all

function love.load()
      time_passed = 0
end

function love.update(dt)
     time_passed = time_passed + dt
end

function love.draw()
     love.graphics.print("You've been playing for " .. time_passed .. " seconds.", 5, 45)
end
How do I make the seconds just a single digit rather than a whole load of them?
Santos
Party member
Posts: 384
Joined: Sat Oct 22, 2011 7:37 am

Re: How to shorten a string?

Post by Santos »

You can use math.floor from Lua's math library.

Code: Select all

function love.load()
      time_passed = 0
end

function love.update(dt)
     time_passed = time_passed + dt
end

function love.draw()
     love.graphics.print("You've been playing for " .. math.floor(time_passed) .. " seconds.", 5, 45)
end
For more info on the standard library functions, you can check out the math and string library tutorials on the lua-users wiki, and the manual. Hope this helps! :)
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: How to shorten a string?

Post by bartbes »

string.format might help you.
User avatar
mcjohnalds45
Prole
Posts: 18
Joined: Sat Jun 02, 2012 12:08 pm

Re: How to shorten a string?

Post by mcjohnalds45 »

Use love.timer.getTime() to return the seconds since startup and math.floor() to round it down to a whole number, so change line six to:

Code: Select all

time_passed = math.floor(love.timer.getTime())
Or alternatively in this case just change line ten to the following and forget about time_passed.

Code: Select all

love.graphics.print("You've been playing for " .. math.floor(love.timer.getTime()).. " seconds.", 5, 45)
I would also recommend taking a quick look at lua's math and string library.
User avatar
BoopDeePoop
Prole
Posts: 39
Joined: Sun Dec 30, 2012 4:15 am

Re: How to shorten a string?

Post by BoopDeePoop »

Thanks everyone here for helping me. I got it to work so thanks again.
love.graphics.print("You've been playing for " .. math.floor(love.timer.getTime()).. " seconds.", 5, 45)
I did that and it did exactly what I wanted.
User avatar
miko
Party member
Posts: 410
Joined: Fri Nov 26, 2010 2:25 pm
Location: PL

Re: How to shorten a string?

Post by miko »

mcjohnalds45 wrote:Use love.timer.getTime() to return the seconds since startup and math.floor() to round it down to a whole number, so change line six to:

Code: Select all

time_passed = math.floor(love.timer.getTime())
If this time_passed variable was used for something else (like calculating player's position), then that would be a bad advice, because the game would not be smooth anylonger.
mcjohnalds45 wrote: Or alternatively in this case just change line ten to the following and forget about time_passed.

Code: Select all

love.graphics.print("You've been playing for " .. math.floor(love.timer.getTime()).. " seconds.", 5, 45)
And that is the correct solution.
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
Santos
Party member
Posts: 384
Joined: Sat Oct 22, 2011 7:37 am

Re: How to shorten a string?

Post by Santos »

Note that love.timer.getTime doesn't quite return the time since love.load, so you might want to do something like this:

Code: Select all

function love.load()
    start = love.timer.getTime()
end

function love.draw()
	love.graphics.print("You've been playing for " .. math.floor(love.timer.getTime() - start).. " seconds.", 5, 45)
end
Post Reply

Who is online

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