Continue the progress/data even when the game is off

Good morning. I am creating a resource generation game. The basis of this game is the generation of resources and the construction time of the buildings.
The base gameplay is based on the fact that if a construction takes eight hours to build, even if the game is turned off, the time continues and when the player turns the game back on, he will see how the world in which he is playing has continued to go on and the resources have been increasing and everything he has left unfinished has been finished.
To do this I need to save the data and keep it updated, is there any way?
I put a reference image where you can see the four main variables together with the ‘waiting time’. ‘Materia’ and ‘Materia2’, which store a total data, and ‘MateriaPH’ and ‘Materia2PH’ (resources per hour), which add every second a constant amount and can be improved.
Thank you for reading :slight_smile:

Base everything off RealTime - e.g. make a record of the starting time (real) of the building, then you can find the real time duration by subtracting that start time from the real time…

To add to what RecourseDesign says… instead of adding a constant amount every second, the simple solution is to store the real-world time that the construction started (as RD notes), and then handle the calculation by doing “take the current real-world time, subtract the time started, that’s how many seconds have elapsed” and calculating the materials used so far that way.

THAT SAID. How I personally would probably handle this is with a bit more abstraction.

The reason I would use an abstraction is so that if I wanted to add something like a “fast forward” button to the game – if you had nothing else you needed to do and wanted to just advance time – I could do that.

If I wanted to do this globally, I could just make a special ‘timestamp’ for the game world, representing the in-game date (like how strategy games will start at year 0 and advance to year 2000 or whatever). Then when you save the game, you save the real world timestamp alongside the game one, and when you load the game you take the current real-world time and subtract the saved real-world time; oh, they last played 86400 seconds ago, so you add that many seconds to the game-world time and continue on.

Then you could base your construction progress on that in-game time, and by virtue of it not being directly tied to real-world time you could speed it up, slow it down, skip it ahead, whatever you need – all without having to muck with your computer’s actual date/time setting.

All of the builder games work by determining the time at which the building will finish (as a real timestamp,) when starting to build, and storing that. You can then easily calculate whether the building has finished or not by looking at the clock, and you can easily calculate how much time is left.
If there’s an “accelerate” action, then you can apply that be converting the times to “current time left,” then dividing that in half or whatever, and then adding that duration to the current time to make the new “time at which it will finish.”

When you load the level later, because the clock is now later, buildings will have progressed.
You will obviously have to display building progress as a function of “time left” rather than trying to step some animation forward or somesuch.