How can I make a daily rewards system in gdscript

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

It’s that I want to make the shopkeeper give 300 coins to the player every day but I have tried and I can’t, help pls

What have you tried? What’s your method of implementing this mechanic? What does your scene look like? Have you used any code?

Ertain | 2021-10-26 08:09

That every day a chest appears in the corner of the store and that by pressing you get the 300 coins and then it locks for the next 24 hours

RubRub | 2021-10-26 16:13

:bust_in_silhouette: Reply From: Wakatta

The workflow for such a system is as follows

func on_shop_scene_opened():
    #get last time chest collected and current time 
    var old_time = get_last_saved_time()
    var new_time = OS.get_datetime()

    #compare times to see if 24hrs has elapsed
    var ready = (new_time.hour - old_time.hour) >= 24:

    #assuming chest node is a texture button here and enable or disable here
    #you can also use show() or hide() based on ready var

func on_chest_pressed():
    #update coins
    $player_node.coins += 300

    #set max coins collectable, not needed
    $player_node.coins = clamp($player_node.coins, 0, $player_node.coins_max)
    #log time chest reward collected to be retrieved by get_last_saved_time()

The above pseudo code assumes fair play but is susceptible to time hacks (i.e changing the system’s clock)

There are many sites dedicated to keeping time (e.g. Time)
and you can simply do HTTP_requests to resolve the time however gives your game the additional requirement of internet connectivity

oh thank you very much nice night

RubRub | 2021-10-27 02:06