+1 vote

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

in Engine by (884 points)

2 Answers

+1 vote
Best answer

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.

by (722 points)
selected by

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

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

Thank you for the help!

+1 vote
by (659 points)

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

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.