there's some games that show a message when the user comes back, like "Welcome back, you have been out for 3 hours". or when you see a ads Video that gives bonus points for 2 hours, but if you close the app, the clock keeps running... how to do that?

You don't need to run code while the game is closed to do that. Simply store the timestamp returned by OS.get_unix_time() in a file every time the game is exited, then load that file when the game starts and compare it with the current timestamp. (The Unix time represents the number of seconds elapsed since 1 January 1970.)

You will want to ensure the timestamp file saving function is always run before quitting too; see Handling quit requests in the documentation.

@Calinou said: You don't need to run code while the game is closed to do that. Simply store the timestamp returned by OS.get_unix_time() in a file every time the game is exited, then load that file when the game starts and compare it with the current timestamp. (The Unix time represents the number of seconds elapsed since 1 January 1970.)

You will want to ensure the timestamp file saving function is always run before quitting too; see Handling quit requests in the documentation.

thanks for the answer, and that's what i thought.

but what if the game gives 1 point per hour that the game is closed.. and what if the player manually changes its phone time to 2030 the player can 'hack' the game this way?

but what if the game gives 1 point per hour that the game is closed.. and what if the player manually changes its phone time to 2030 the player can 'hack' the game this way?

They probably could, but you could potentially get around this by getting the time from a server instead. This would require the application to connect to the internet every time it opens, but other than that I'm not sure how you'd get around the player manually changing the time on their device.

You could try using Google's public NTP server and parse it using something like this article (written in C) . It would need to be translated to GDScript, or you could use GDNative.

I found a link to the article here, where someone asked how to use Google's public NTP programmatically.

I have no idea if you can use google's NTP server commercially though.

5 years later