Page 1 of 1

How to shorten a string?

Posted: Sun Dec 30, 2012 4:20 am
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?

Re: How to shorten a string?

Posted: Sun Dec 30, 2012 6:36 am
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! :)

Re: How to shorten a string?

Posted: Sun Dec 30, 2012 11:56 am
by bartbes
string.format might help you.

Re: How to shorten a string?

Posted: Mon Dec 31, 2012 2:07 am
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.

Re: How to shorten a string?

Posted: Mon Dec 31, 2012 2:10 am
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.

Re: How to shorten a string?

Posted: Mon Dec 31, 2012 12:29 pm
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.

Re: How to shorten a string?

Posted: Mon Dec 31, 2012 8:03 pm
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