Track time while game off

Hi!

I’m trying to find out atm how I could track time while game is shut down. Like if player does something that starts a timer for and example player puts a building in to making that takes 1,5h to construct, how do I tack it so that I could A) give a push notification when the construction is ready and B) have the construction done or show correct remaining time when opening the app again?

Do I need some backend shenanigans to do this or is it possible to do this offline? The game in it self doesen’t need online so this would be not prefered…

Thanks!

This should work:

  • create a variable of type Date Time, let’s call it Previous Time
  • use Now to obtain current system time, set Previous Time to Now
  • save this var to a Save Game Object

When you’re ready to check the time:

  • load the variable from a save game slot
  • Now - Previous Time = time elapsed
  • format the result as minutes (or whatever you desire)
  • if result > 90 then construction has finished

edit: did not notice it was for mobile, not sure if the above is applicable, should be

You can use the schedule local notification at time node for, I’m guessing it’s only for mobiles though (seems logical), For PC I am unsure.

Same as Everyone - edit: did not notice it was for mobile, not sure if the above is applicable, should be

Yeh, this worked well, thanks!

This seems to be working also, Thanks!

Good day, indeed!

I don’t think I understood that. I can see you’re attempting loading and saving the game 60 times per second (or more!) - during Tick. That cannot be right.


You say

when the energy ends it is impossible to play

But it seems we are accumulating energy rather than depleting it.

Perhaps you could break it down, like so:

  • player has 5energy
  • player loses 1 energy every second
  • when we lose all energy, the game is over


I don’t understand where date time comes into this. We have load / save because we need to compare it to a previous state after loading the game?

I did deplete energy when pressing the play button. total energy is 15. When it ends, you can’t play until you have to wait until the energy is restored, I wrote a recovery condition (if less than 15, then there is a recovery) it remains only to make a timer so that, for example, 1 energy is restored every 5 minutes and then you can play again, I’m all did except the timer. I need to make it work when the game is not running. I found that this system is done through the data type “Date time” but I do not know how to use it, unfortunately I did not find the drawings. in the 2nd screenshot of the drawing where “delay” you need something else instead, since “delay” only works when the game is running, I hope I explained it clearly. if it is not clear, I can record a video of how it works and send you a link. In general, I want energy to regenerate over time (every 5 minutes + 1 energy, up to 15), even if the game is closed.

  • do not use Tick + Delay, especially for this - you’re loading your game 60 times per second, flood the Delay every frame and then save the game every 30s.

  • create an event for saving the game
  • create an event for loading the game
  • use a timer to count the energy when the game runs

  • before player quits the game, save the amount of energy they had and a timestamp
  • when they load the game, check the amount of energy saved and calculate how much time has passed since last save
  • if you want to have a cap on the amount of energy that can be restored, you could clamp it

It could look close to this:

1 Like

Thank you very much! it worked, but my variable stopped depleting, when you press “play”. It only restores to 15, I wrote the logic in the “player controller” and tied “load game” to the “event tick” “save game” is tied to the button, which is associated with the exit from games, right?

For the 3rd time. :smiley: - do not do this. Tick runs every frame, typically 60 times per second. Create events and load / save game only when it is needed.

1 Like

The logic of counting energy should be completely separate from loading and saving the state of the game.

1 Like

if I understand you correctly, then yes, I made a separate slot for loading and saving energy. but I can’t figure out what to attach “load game” to because it doesn’t work with “event begin play” but only works with “event tick” :sweat_smile:

I think there is a big misunderstanding, actually. We’re not talking about save game slots.

  • when the game starts, check if a save game object exists
  • if it does, load it
  • if it does not, create one
  • if you loaded it, run the logic I suggested for calculating time passed
  • the logic of adding energy while the game runs should be completely separate from the above, these are 2 different things

And yes, Load game should be on Begin Play. It absolutely should not be on Tick. The official docs are pretty clear regarding how to set it up:

1 Like

If the player’s energy can be unlimited, just keep adding it with a timer. If there’s limit regarding how much they can regenerate while offline, clamp it:

1 Like