0 Favourites

How do I work with timers?

  • Hello,

    I have a problem with the timers option in Construct 2. I wanted to create a timer in my project. Let's say when a player presses a button the timer goes on. It stays like that forever, or at least until the player presses the button that restarts it. This means that even when the game is off the timer will keep going. Given these conditions, the timer would need to consist of days, minutes and seconds.

    Now I've created a text object and I gave it a timer behaviour. I played with it for a while but to no luck. I am not really sure what expressions I should use and in what way I could create this kind of a scenario. To add to the confusion, aside from the timer behaviour there is a "timer" expression.

    So yeah, really struggling here.

    Any help?

  • if you want it to keep counting while you are out of the app you need to consider the system day and time instead.

    Save it when he clicks and then compare it to what it is when the users clicks again.

    Rexrainbow made a nice plugin for date and time you can use.

    https://www.scirra.com/forum/viewtopic.php?t=63492&start=0

  • So you are suggesting that I should create a global variable that will save the time once the user leaves the app and then compare the time the user entered the app again with the saved time?

    I think I can make that happen.

    I am not sure how to set the timer yet, I guess I need to play with the plug-in a bit

  • Yes that is what I am suggesting. You can use webstorage to persist the value between game sessions.

  • Yeah, good idea. I think I will be able to do that.

    The problem I have now is I don't know how to set the timer. Every time I start it it's just stuck at 0 or 1. Like, the commands for the timer are so confusing that it's driving me crazy.

  • I am currently not home. But you can have a look at the .capx linked in this topic.

    https://www.scirra.com/forum/viewtopic.php?f=147&t=88746&p=702329&hilit=calendar#p702329

    I know it uses the date plugin.

    Let me know if that does not help you, then I can throw something together sometime this evening.

  • I am currently not home. But you can have a look at the .capx linked in this topic.

    https://www.scirra.com/forum/viewtopic.php?f=147&t=88746&p=702329&hilit=calendar#p702329

    I know it uses the date plugin.

    Let me know if that does not help you, then I can throw something together sometime this evening.

    I looked at the example you sent me. It seemed pretty complicated at first but once analyzed further it didn't seem that bad. My concern is the actual usability of the .capx given that the example only shows today's date (unless I did something wrong). I don't think I would be needing that in my app, instead, I simply want to show the time since the button has pressed (for example) and count it until the player presses another button. That time might be 20 days 35 hours, 23 mins and 13 seconds or may just be 10 minutes. No rules to that.

    Sorry if I am bothering you too much, it's not that I am a complete noob to construct, but this thing is really making huge problems for me, and given that it is a key to the app I am building, I am really looking into any options for solving it.

    thank you once again!

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • Try this: https://dl.dropboxusercontent.com/u/2560922/mobiledev/forum/saveTimestamp.capx

    It gets the unix timestamp (time since Jan 01 1970) in milliseconds and stores it in webstorage.

    Second click (and the once after) will update the difference in time since the first click.

    Hope that clears it up a bit for you.

    And p.s. you cannot bother me. I know how it is to be stuck on something. I just hope I can supply some help.

  • Ok, so I found a much easier way to do it.

    I created 4 global variables.

    -seconds

    -minutes

    -hours

    -days

    I then created an event for each and every one of them. Every 1 second add 1 to seconds. Sub-event, compare "seconds" = 60 then set value to 1. Every 60 seconds my "seconds" timer resets to 1. At this point I am figuring out how to compare the "minutes" variable with the "seconds" variable. So when "seconds" reaches 60 I add 1 to the "minutes"

    Repeat the process for hours and day of course.

    I feel I am so close!

  • I am a bit confused.... have you looked at the example I posted? you will get the interval between the two clicks in milliseconds.

    then just convert the milliseconds to whatever you want: days, hours, minutes, seconds etc.

    I do not see a reason why you would do all the events...

  • There you go.. I updated the .capx to also do the conversion to days, hours, minutes and seconds for you

    https://dl.dropboxusercontent.com/u/2560922/mobiledev/forum/saveTimestamp.capx

    As you can see there is no need to store anything else then the unix timestamp.

  • There you go.. I updated the .capx to also do the conversion to days, hours, minutes and seconds for you

    https://dl.dropboxusercontent.com/u/2560922/mobiledev/forum/saveTimestamp.capx

    As you can see there is no need to store anything else then the unix timestamp.

    OMG. Now I get it!

    I don't know what else I can say besides thank you. Perhaps offer you a bitcoin donation for all your help?

  • I don't know what else I can say besides thank you. Perhaps offer you a bitcoin donation for all your help?

    thx for the offer, but no thanks. I am just happy to help.

    I have the philosophy that if I can help answer some questions, then the smart people we have in this forum might have more time to help answer my own questions

  • awesome example ,

    but i have a problem. I would like that after two days or twelve minutes was created a sprite in the screen,,,, it´s possible???

    i don´t get it.

    thanks

  • tchem That should be possible yes.

    You can just compare the number of the timestamp to see if it is bigger then either 2 days or twelve minutes, and show the sprites if that is the case.

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