How to show time in minutes, seconds, milliseconds in my game?

1 favourites
  • 10 posts
From the Asset Store
An educational game for Times Table. An easy to use template for developers to build larger games
  • How to show time in minutes, seconds, milliseconds in my game?

  • The easiest way I think is to just use a little JavaScript block.

    If you create a Date object without any parameters then it will be initialised with the current time.

    let now = new Date();
    

    The Date object has a lot of methods for getting things like the date, month, seconds, etc. as well as formatted text of the current date or time. You can get the values you specified using:

    let now = new Date();
    
    let minutes = now.getMinutes();
    let seconds = now.getSeconds();
    let milliseconds = now.getMilliseconds();
    

    If you haven't used JS before you can insert a new script block into the event sheet by right clicking on the event you want to add it to and selecting "Add > Add Script". It will run every time that event block does, just like an action would. You can modify local variables using "localVars." then the name of the variable, like so:

    If alternatively you just want to print the current time you might be interested in

    let now = new Date();
    let time_string = now.toLocaleTimeString('en-US');
    
  • Hi!

    Thank you for your replay!

    I have no experience with coding at all.

    On the internet, the software is widely promoted as a game engine for noncoders.

    I just need to finish this one project and after that, I probably won't lay my hands on it again.

    I have implemented the time score uppon some other tutorial.

    I have also added the screenshot feature and after that, I can't reset the time anymore :(

    Can you take a look?

    dropbox.com/preview/Public/SRECKO_wip_5.c3p

  • It's also perfectly possible to do without any knowledge of coding, but I consider the JS versions simpler ( working with time is not easy ). There are a number of expressions related to time available. If you take a look at the documentation for system expressions under the heading "time" you will see them. The one you want would be unixtime, which returns the number of milliseconds since the epoch (1st of January 1970). We unfortunately don't have expressions to convert this into hours/minutes/seconds etc. so you have to apply some maths to get a useful answer. If you would rather use this method here are the expressions for converting unixtime into a more usable format.

    Seconds = floor(unixtime / 1000) % 60

    Minutes = floor(unixtime / (60 * 1000)) % 60

    Hours = floor(unixtime / (60 * 60 * 1000) % 24

    If your a little confused how those work basically they take the number of milliseconds since the epoch. Next they change it to the correct unit by dividing it ( 1000 ms in a second so divide by 1000, 60 seconds in a minute so divide by 60, etc. ). Then the value is floored (rounded down) so it's a whole number. Finally we use the modulo operator to wrap the value, keeping it in the range 0-60. In the seconds example this changes the value from second since the epoch, to seconds since the last minute.

    One gotcha here is that that conversion doesn't include any timezone correction, so they are for UTC.

    I'm happy to take a look at your project, but I don't appear to be able to access it from that link.

  • I am sorry if I was not able to express myself clearly as English is not my primary language.

    I have reuploaded the game again:

    dropbox.com/home/Public

    The player has to finish the game as soon as possible. "Every tick" should be enough. I just need decimals

    to avoid equal results. It is not about the real world time, sorry :)

    About the screenshot feature; I will remove it: it seems like buy now everyone knows how to make a screenshot with their self phone and it wasn't working when I uploaded the game on netlify.com.

    The main problem I have now is that the game doesn't reset the time.

  • I'm still having issues opening your project. I think the link you are giving me only works for you, try using "share" and then "copy link".

  • Ups, sry.

    Try this one please:

    drive.google.com/open

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I found your issue. You have a variable called "TimeStartLayout" but you are not using it. You use the "time" expression, which returns the number of seconds since the game started. In the "on start of layout" event set "TimeStartLayout" to the value of "time", then before updating the clock do "time - TimeStartLayout". This will give you the actual duration.

    Here is your project with the fix

    send.firefox.com/download/e11e8d269006292f

  • It's working! Thank you! :)))

  • Glad I could help.

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