How do I track the passage of time when the game is closed?

  • Hello, I'm working on a real estate game where players buy, sell, and rent properties to make money. They are given a certain amount of *Energy* every day to work with and that energy regenerates over time. When the player is not playing the game, their Energy is still depleting. Does anyone know how to track the passage of time when the game is closed? Thanks!

  • The system expression "unixtime" could help you with that.

    Save it to the local storage when you exit the game, and on start of your game subtract the current unixtime from the one you saved, this will give you the amount of milliseconds that went by since you last exited your game (or saved the unixtime at the time).

  • Thank you that is very helpful. You said "web storage", do you mean local storage or are those one and the same?

  • I do indeed mean Local Storage, sorry I wasn't all awaken at the time of my answer I guess.

    Editing it in my previous post to prevent further confusion.

  • quick question, i think u already solved your issue by this time .... is your app mobile or pc? if is mobile i have a detection question for you... how do you detect the app is closed? or the user has the intent of closing? compare if app is unfocused?

  • It's going to be a mobile app, and that's an excellent question. Honestly, I'm not sure yet. I think unfocused because a player might still have that app open but not be playing it, in which case the function still needs to run. But the function would have to work in both scenarios, unfocused and closed.

    How do I detect either of those?

  • It's going to be a mobile app, and that's an excellent question. Honestly, I'm not sure yet. I think unfocused because a player might still have that app open but not be playing it, in which case the function still needs to run. But the function would have to work in both scenarios, unfocused and closed.

    How do I detect either of those?

    that's why i asked also, cause i never done it before, was thinking u already solved it, from my knowledge if is unfocused the app won't run any background events cause automatically pauses itself unless im mistaken.

    and if u use the mobile app service thing button to force close the app is again unfocused so it won't know is being closed... this is a tricky thing to do on mobile. unless u can detect the input of mobile that shows all apps running and then quickly tell the app to save progress when the user touched that input... but probably saving progress every 1 minute would do the trick. but then ul have a 1 minute gap glitch where people could just get fast energy by closing and reopening it again, before it saves.

    id have to test it out though.

  • I believe "unixtime" expression is only available in Construct 3.

    In C2 you need to use these addons:

    construct.net/en/forum/extending-construct-2/addons-29/plugin-system-date-and-time-41036

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • It's going to be a mobile app, and that's an excellent question. Honestly, I'm not sure yet. I think unfocused because a player might still have that app open but not be playing it, in which case the function still needs to run. But the function would have to work in both scenarios, unfocused and closed.

    How do I detect either of those?

    This blog post covers this with the solution, maybe you could flag it up for an Android feature request?

    https://proandroiddev.com/detecting-when-an-android-app-backgrounds-in-2018-4b5a94977d5c

    Feature request link below

    https://construct3.ideas.aha.io/

  • Let's say the game only has one layout. If I used the event on layout closed would that suffice? Or would it still not detect if the game is unfocused?

  • You don't need to detect when the app is closed. Simply save the time to local storage every minute, or even every 10 seconds if you want more precision. Writing to local storage is fast, just make sure you are not writing and reading from local storage at the same time.

  • Haha a cheap solution, but to the user there's be almost no difference. I like it, thank you.

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