How do I create a timer in seconds.milliseconds format?

0 favourites
  • 4 posts
From the Asset Store
Fully commented source code/event sheet & sprites to create a space shooter game
  • This one's been driving me mad, especially because it seems like it should be so simple...

    I'm trying to display and save a timer in seconds.milliseconds format (00.00) but I can't seem to figure out a way to keep it uniform. For example, if the time is 15.21, it's fine. If it's 15.20, it will display as 15.2, which creates an annoying flickering effect as the timer flies through.

    I did some searching on here and found this solution: "Time: " & int(timer)&"."&zeropad(round((timer-int(timer))*100),2)

    After spending some more time with it, however, I've noticed that it adds an extra zero in some situations (15.200) which is only slightly less annoying than my original setup. My hacky solution was to set the Text object's wrapping property to "Character" and resize the box so you'd never notice the fifth place, but then I realized that doesn't really work if the player goes up past a hundred seconds...

    Any thoughts? It seemed like the thread I got that zeropad solution from was the best answer I could find, but I wanted to ask the question on here before giving up completely and sticking with it.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Hey noisetank,

    Lets assume our number is stored in a variable named "num", and we want to format it as an integer part, always followed by two decimal digits, even when either or both are zeros.

    There are a few ways to go about this.

    The method I'm showing below converts the number to text and then inserts a "." into the correct spot.

    We create a text variable named "digits".

    digits = zeropad( round( num * 100 ) , ( 2 + 1 ) )

    Formated string = left( digits , ( len( digits ) - 2 ) ) & "." & right( digits , 2 )

    Done.

    To make it clearer what is going on in the above steps, I've created two examples below that show the evaluation of the expressions from beginning to end, so you can see how each part of the expression works.

    Example #1

    If num = 888.123

    digits = zeropad( round( 888.123 * 100 ) , ( 2 + 1 ) )

    digits = zeropad( round( 88812.3 ) , ( 2 + 1 ) )

    digits = zeropad( 88812 , ( 2 + 1 ) )

    digits = "88812"

    Formated string = left( "88812" , ( len( "88812" ) - 2 ) ) & "." & right( "88812" , 2 )

    Formated string = left( "88812" , ( len( "88812" ) - 2 ) ) & "." & "12"

    Formated string = left( "88812" , ( len( "88812" ) - 2 ) ) & "." & "12"

    Formated string = left( "88812" , ( 5 - 2 ) ) & "." & "12"

    Formated string = left( "88812" , 3 ) & "." & "12"

    Formated string = "888" & "." & "12"

    Formated string = "888.12"

    Example #2

    If num = 0.071

    digits = zeropad( round( 0.071 * 100 ) , ( 2 + 1 ) )

    digits = zeropad( round( 7.1 ) , ( 2 + 1 ) )

    digits = zeropad( 7 , ( 2 + 1 ) ) // This zero pad makes sure we have at least 1 digit in the 1's place, even if it's a "0".

    digits = "007"

    Formated string = left( "007" , ( len( "007" ) - 2 ) ) & "." & right( "007" , 2 )

    Formated string = "0" & "." & "07"

    Formated string = "0.07"

    Hope that helps.

  • That works great! Thank you for the help (and the detailed examples so I actually understand what it's doing).

  • fisholith thank you for offering this explanation but there is something about this that's totally going over my head. I get the general idea of how you're doing this, but am not sure I would actually put this in an event sheet. I have a timer that would be in 0.0 format.

    in the attachment you can see i have a timer integer which is the actual timer that is being used in the game mechanics.. my assumption is that the digits text variable in your example (i calle TimerText) is a kind of dummy variable that just displays the number variable in the appropriate way.

    in this screenshot, if i disable the 3rd line what i see is basically a decimal-less version of the countdown, so that is at least doing something to the number.. but i don't know how to actually apply the "Formatted string" lines of code you show as an example because it seems like they are what is responsible for the shifting of decimals..

    but even if that's not the case, your examples all reference a specific number in the formula.. i don't know what my timer number is at any give moment so i can't say timer.123 or timer.3 you're example talks about specific numbers, sorry I'm so confused by this.

    NOTE: Also when I have that third line in the screenshot active it just goes blank.. I just wanted to mention that i know it's wrong, i just wanted to try something as close as I could make to what you were showing in your reply.

    Thanks!

    Caleb

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)