(SOLVED) The time that passes while the game is closed

I have a problem in my game with creating time that goes on even when the game is closed. I have three text objects, one for hours, one for minutes, and one for seconds, and when 24 hours have passed, the player should be able to click collect and collect the Daily Check In reward. And then again the same next day etc. Can anyone help me how to do this?

Hi. No counter can keep on running when game is closed.

However wha you want is possible.
All you need is record the current time (hour, day, year) when closing the game and record it in the game storage.

When opening the game, you take the current time and substract the recorded time.

You’ll get the elapsed time while app is closed.

Thank you for your answer. But how to do that?

I’m not sure since I never did it. So I can only explain roughly.

https://wiki.gdevelop.io/gdevelop5/all-features/expressions-reference

Here you’ll find the expressions to get the current time.

When clicking on the button to close your game, you make variables record the current time. And store it.

I don’t know about storage but you can find info on forum threads or wiki.

When opening game, computer look at the current time, and compare it with the one in the storage.
The difference is the elapsed time.

There might be GDevelop example about this elapsed time when app is closed…? Maybe since it’s a often needed feature. I don’t know.
Anyways, take a look at

  • times expressions (that can be written into variables)
  • storage

I am glad you asked this because it gave me a chance to play with an extension I was recently delighted to notice, but have not looked into yet as I’m working on another part of my project at the moment.

The extension is called Time Formatting by Bouh, so first install this extension into your project.

If you are like me you like using “timestamp” when dealing with time for ease and convenience in formula, but you dread formatting it into something that makes sense to the player if some part in your game calls for the value to be seen by the player.

Enter this brilliant extension and our problem is solved forever. Simply divide your timestamp value by 1000 and let this extension do the tedious calculation.

Here is how I used it with some notes:

And I love it, I can’t thank someone enough for making it. It is perfect for my project when I get to that part. Also hopefully it is useful in yours too.

To find this extension, when you get to the part about modifying text, set to, then search timefor and it pops up.

To find the current time expressions you can do Time(“timestamp”) like that or follow this picture and note do not search for time in the search bar where the red words show up, hit the expression builder and search for time in the search box that pulls up.

Oh also I wasn’t sure about adding the BlueGem without a trigger once, but in my tests it only added just the 1 gem like this.

1 Like

@Lucky-j I tried it, but it doesn’t work. Hours text object which is supposed to show time going is on 00:00:00 and it is doesn’t changing. What I did wrong?




Hmmm, mine works and says I still have 15 hours and 39 minutes before I can collect my next blue gem. The only thing I can see different in yours is that I did not actually make a global variable RewardTime in the side panel, I just created it in the code by naming it into existence. TempVar scene variable I did the same way. But if I did make a global variable RewardTime I would put it to number and not string. Try putting yours on to number, see if that matters. I’m not sure it does matter but I always use number when I mean a number variable and string if it’s text expression.

Also, you have to click CollectDailyCheckIn1 the first time, or it’s at 0 so it won’t show a count down time.

Also in your beginning scene event you want to check if RewardTime exists in storage DailyReward, the way it reads now you are checking for a variable you didn’t create in a storage that doesn’t exist. So try that too, but I don’t think it would affect the time showing ingame, it would just not load it when you’re starting up. (You can toggle disabled this whole thing for testing so that each time you open the preview it will let you click the button again)

Also there is a greater than in the timestamp+3600000 which should be a less or = to but that won’t prevent the time from displaying it would just always add the extra 00: even when not needed.

Also I notice you are using on mouse down where I used mouse release. But I just tested that by disabling the part that reads from my storage and it doesn’t seem to matter, it still displays time and still only gives 1 blue gem.

Let me know if any of that works.

@Lucky-j You were right, I need to click on CollectDailyCheckIn1 for the count down starts. I did everything you said and now it works, but when I close the preview on mobile and I go preview it again, it’s again 00:00:00 and I need to click again on CollectDailyCheckIn1 for it to start.

Hmmm, I can’t ever get anything to preview on my mobile. Hopefully someone that knows about that will shed some insight on what’s going on for you.

Make sure you don’t have the read event still toggle disabled (if you had done that for testing)

@Lucky-j I think I don’t have it disabled. Which event is that? Also I tried to preview it on laptop in GDevelop too, it’s the same like on mobile…

Make sure you use the read and write values with storage. Don’t mix text and numbers with those actions.

What is the value that’s saved to storage? To find it, open Developer Tools, choose application tab, and click on Storage > Local Storage > file://

image

@MrMen I found it, but I’m so confused now :sweat_smile:. I don’t know what to do next

It really does sound like there’s a disconnect in the storage somewhere. Can you post your revised events?

Look for the entry GDJS_DailyReward. In my case, I set it to 1234:

image

to get

image


Can you screen snip you one?

I"m looking for the event that looks like this:

Here is the original file, it might help to compare? The only thing I changed was the name of the storage, because it would conflict with yours if the storage is the same name.

https://drive.google.com/file/d/1MkaiChEhPNWEnCCL8e9nwN7aO0oJmQOv/view?usp=sharing

Here is a revamped version where I changed the time to 20 seconds, added a save feature for BlueGem and changed the color of the collect text, oh and let the user click either the collect text or the checkmark sprite to get the reward.

https://drive.google.com/file/d/1ztndVQT29KxVA_-vRy0zgjlaJ9JnLrJQ/view?usp=sharing

@Lucky-j Now it’s working! Time goes on even when the game is closed. The problem are only blue gems (Sapphire). For some reason, when I clicked on collect, it didn’t added 1, it added 1 milion something :sweat_smile: :sweat_smile:

It’s ok I see the problem immediately this time - though the users might not think it is a problem hehe

Look here:

See you are adding all those milliseconds stored into Sapphire. Change that to SapphireVar

You might have to delete your storage to get it back to normal.

1 Like

@Lucky-j Oh, I see it now. I’ll fix it. Thank you and everyone else here who helped or tried to help. It means much :blush:

2 Likes

I’m so glad it’s working!

1 Like