How to continue a countdown timer

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

Hi everyone!
I am making a game where, after the player is killed it would take 3hrs for him to be alive again. But I want that this time continues even if the player has closed the game. The same as when we upgrade a building in clash of clans the timer continues even after we close the game

:bust_in_silhouette: Reply From: Asthmar

This question has a good answer https://forum.godotengine.org/67347/let-a-timer-still-run-if-the-game-is-closed

What will happen if player changes time and date of the desktop or mobile?

Help me please | 2021-06-10 06:27

Nothing should happen to break your game, but they will be able to skip time. You may be able to code something to stop that, but I’m not sure.

Asthmar | 2021-06-10 08:21

:bust_in_silhouette: Reply From: wyattb

Save time and when they restart your game read the time from an NTP server. But still depends how critical this is. You would have to deal with time zones and app must have internet access.

How will I save and read time from the server please give a bit more detailed answer?

Help me please | 2021-06-10 15:32

In general when your game stops it should save the time to a file. When it starts up again it loads the stop time from the file and compares it to an online time server. You can make an http request to retrieve the user’s time from a time server such as http://worldtimeapi.org. You need to know the user’s time zone. E.g. https://worldtimeapi.org/api/timezone/America/New_York

Here is an example

var current_datetime
var previous_stop_time

func _ready():
	# Load the last stop time from permanent storage
	# previous_stop_time =  ....

	# Create an HTTP request node and connect its completion signal.
	var http_request = HTTPRequest.new()
	add_child(http_request)
	http_request.connect("request_completed", self, "_http_request_completed")

	# Perform a GET request. The URL below returns JSON
	var error = http_request.request("https://worldtimeapi.org/api/timezone/America/New_York")
	if error != OK:
	  push_error("An error occurred in the HTTP request.")
	pass


# Called when the HTTP request is completed.
func _http_request_completed(result, response_code, headers, body):
	var response = parse_json(body.get_string_from_utf8())
	current_datetime=response["datetime"]
    #
	# Compare previous_stop_time with current_time
    #
	pass

wyattb | 2021-06-10 20:56

Thank you for the help!

Help me please | 2021-06-11 02:32