How can I measure the time since the game was last opened?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Me

So, I’m making a city building game similar to clash of clans. When the player builds something, a timer appears on top of the building showing how much time it takes to finish (e.g. 10 minutes). I’m now trying to figure out how to make the timer continue when the game is closed or the computer is shut down.

How can I measure the time since the game was last opened?

I considered using OS.get_datetime() but I’m new to godot and don’t really know how that works.

Thanks in advance!

:bust_in_silhouette: Reply From: russiniet

when you exit out of the game, just save the current time into a savefile. To get the time, use OS.get_datetime(). When you open the game again, check the time the second time. Then, subtract current time from previous time and you will get the amount of time passed since your last play through. Even though, if your game is dealing with real money (micro transactions), then I really wouldn’t recommend doing so because it’s very easy to edit the save file (even if it’s encrypted). You should use servers to keep track of that.