• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker Daily goals - comparing date and time

Bee

Member
I have a game with daily goals, which I would like to reset at 3am every day (PST or whatever). I've been fiddling around with the existing functions and just can't wrap my head around it.

So I get the current date when a new game is started or when a game is loaded. If the game is loaded, how do I check if the time has gone past 3am from the day before?

As an added bonus, I also need to figure out how to get the time remaining to 3am so I can display how much time is left to complete the goal.

I'm frustrated I can't figure this out. Can somebody please help me?
 

chamaeleon

Member
I have a game with daily goals, which I would like to reset at 3am every day (PST or whatever). I've been fiddling around with the existing functions and just can't wrap my head around it.

So I get the current date when a new game is started or when a game is loaded. If the game is loaded, how do I check if the time has gone past 3am from the day before?

As an added bonus, I also need to figure out how to get the time remaining to 3am so I can display how much time is left to complete the goal.

I'm frustrated I can't figure this out. Can somebody please help me?
Your question is not quite well specified. "Now" (as the time the game is started) is always going to be "past 3am the day before".. Are you asking if there is a 3am between some previous datetime stored and "now", and for the "bonus" how long it is from "now" until the next 3am?

For the first part, maybe this untested code? If there are corner cases that doesn't work out due to one or both of t1 and t2 being 3am, I'll leave those as something resolve.
Code:
var t1 = saved_datetime; // Read from ini or whatever
var t2 = date_current_datetime();

if (date_second_span(t1, t2) >= 86400)
{
    return true; // Yes, at least one 3am is between t1 and t2
}
else
{
    var t1_y = date_get_year(t1);
    var t1_m = date_get_month(t1);
    var t1_d = date_get_day(t1);

    if (date_get_hour(t1) < 3)
        var t_3am = date_create_datetime(t1_y, t1_m, t1_d, 3, 0, 0);
    else
        var t_3am = date_create_datetime(t1_y, t1_m, t1_d+1, 3, 0, 0);

    if (date_compare_datetime(t1, t_3am) < 0 && date_compare_datetime(t2, t_3am) > 0)
        return true; // Yes, 3am is between t1 and t2
}
return false; // No, there is no 3am between t1 and t2
The datetime functions should be able to help you out with the second part too.. If t2 is earlier than t_3am, getting the number of seconds between those two points in time will give you how much time remains, and then you display this in a suitable manner.
 
Last edited:

rIKmAN

Member
Depending on the value of your daily goals, you may want to look into checking a server for the actual current time, doing it locally against the device time is easily tricked by changing the time on the phone.
 

Bee

Member
chamaeleon - Oh my thank you! That's what I was missing... the create date. I wasn't sure how to create a date and time to compare to so thank you so much for that. It's incredibly helpful.

rlKmAN, I was wondering about that myself. How do you get a server time? Sorry for my newbness.
 
Top