• 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!

SOLVED Real time Day-Night cycle

J

JohnSebek

Guest
So I made a day-night cycle by FriendlyCosmonaut and I wanted to change it so the cycle would be real-time. So if your pc has 15:40 the game time would be 15:40. I was using the date_get variables. But it was no use. I tried to set up it several times. But the day kept being too fast and not aligned to real-time. Here´s my code that I am using as a base(i tried to change the variables reals,realh,realm, etc. to mysecond,myhour, etc. But this does not seem to work either.

CREATE:
GML:
//time cycle

myminute = date_get_minute(date_current_datetime());

myhour = date_get_hour(date_current_datetime());

mysecond = date_get_second(date_current_datetime());

realh = 0;

reals = 0;

realm = 0;

time_increment = mysecond

day = 1;
season = 1;
year = 1;

//DAYNIGHT
darkness = 0;
light_colour = c_black;

guiWidth = room_width;
guiHeight = room_height;
STEP:
GML:
///DAY NIGHT

//Time setup

reals += time_increment

realm = reals/60

realh = realm/60

darkness = realh/24


//cycle check

if(realh >=24){

reals = 0;

day +=1;

if(day > 30){

day = 1

season +=1;

if(season >4){

season = 1;

year +=1;

}

}

}
DRAW:
GML:
//draw daycycle

var c = light_colour;

draw_set_alpha(darkness);

draw_rectangle_color(0,0,guiWidth,guiHeight,c,c,c,c,false);
 

curato

Member
I think you are over complicating it. why don't you get the time in the create event like you are doing now and set an alarm for what ever interval of time is important to the game one second if you really care about seconds or do a whole min and just get the time again instead of trying to calculate it manually.

If you just want to play and do it manually, use an alarm every second then add the second to the time.
 
J

JohnSebek

Guest
I think you are over complicating it. why don't you get the time in the create event like you are doing now and set an alarm for what ever interval of time is important to the game one second if you really care about seconds or do a whole min and just get the time again instead of trying to calculate it manually.

If you just want to play and do it manually, use an alarm every second then add the second to the time.
But how does the alarm calculates the real-world minute?
 

Nidoking

Member
I see you've mastered the "copy code I don't understand from somewhere and paste it wherever it looks like it fits" skill.

So, you're grabbing the current date, storing it in a bunch of variables, then... not doing anything with it. Most of it, anyway. You're taking the number of seconds of the time at game start, and then using that as the number of in-game seconds to advance the in-game timer each step. So if you happen to start the game at an exact minute, your timer will never advance at all. If you start it one second before a whole minute, you'll advance almost a complete in-game minute every step. If you want the real-world time, then use the real-world time you grabbed from date_get. Get it again whenever you need to know what it is. And please take the time to learn what you're doing. It will benefit everyone, mostly you.
 
J

JohnSebek

Guest
I see you've mastered the "copy code I don't understand from somewhere and paste it wherever it looks like it fits" skill.

So, you're grabbing the current date, storing it in a bunch of variables, then... not doing anything with it. Most of it, anyway. You're taking the number of seconds of the time at game start, and then using that as the number of in-game seconds to advance the in-game timer each step. So if you happen to start the game at an exact minute, your timer will never advance at all. If you start it one second before a whole minute, you'll advance almost a complete in-game minute every step. If you want the real-world time, then use the real-world time you grabbed from date_get. Get it again whenever you need to know what it is. And please take the time to learn what you're doing. It will benefit everyone, mostly you.
I just left the variables there so ppl could understand what I tried to do. It's not like I just put the variables there and did nothing with it. But when I put the date_get variables the day cycle was 60 secs long or did not happen at all.I also removed the /60 from the step event so the time could advance as in real-time.But the result was the same
So the code looked something like this:


GML:
mysecond += time_increment

myminute = mysecond

myhour = myminute

darkness = myhour


//cycle check

if(myhour >=24){

mysecond = 0;
 

Nidoking

Member
mysecond += time_increment
Yes, yes, I saw that the first time. What is the value of time_increment? Really think about that. It should be easy because I told you exactly what you're doing wrong. Dividing by 60 is not the issue, and removing it won't help because you're starting with the wrong number in the first place. Think about what you're doing. Think about WHY you're doing it. Think about what you want to be doing. Think about HOW you would do that. Understand that code is not magical and it does exactly what you tell it to do, no more, no less. Understand that it is doing what you told it to do, and you told it to do the wrong thing. What is the right thing? Tell it to do that instead.
 
J

JohnSebek

Guest
Yes, yes, I saw that the first time. What is the value of time_increment? Really think about that. It should be easy because I told you exactly what you're doing wrong. Dividing by 60 is not the issue, and removing it won't help because you're starting with the wrong number in the first place. Think about what you're doing. Think about WHY you're doing it. Think about what you want to be doing. Think about HOW you would do that. Understand that code is not magical and it does exactly what you tell it to do, no more, no less. Understand that it is doing what you told it to do, and you told it to do the wrong thing. What is the right thing? Tell it to do that instead.
I don't know. I understand what the code does. But I just don't know how to handle the variables of real-time from the date_get.
 

chamaeleon

Member
I don't know. I understand what the code does. But I just don't know how to handle the variables of real-time from the date_get.
What is your definition of "day" and "night"? When does night end and day start, and when does day end and night start?
 
J

JohnSebek

Guest
What is your definition of "day" and "night"? When does night end and day start, and when does day end and night start?
well, I would want the day/night to proceed as on the pc clock.so the day would start at 6 am and the night would start at 6 pm.The way I would like it to work is just like in animal crossing.
 

chamaeleon

Member
well, I would want the day/night to proceed as on the pc clock.so the day would start at 6 am and the night would start at 6 pm.The way I would like it to work is just like in animal crossing.
So...
GML:
var hour = date_get_hour(date_current_datetime());

if (hour >= 6 && hour < 18)
    day = true;
else
    day = false;
And use "day"'s true (it is daytime) or false (it is nighttime) value to influence whatever you need to react to whether it is day or night.

Yes, I know it's possible to write
GML:
day = (hour >= 6 && hour < 18);
 
J

JohnSebek

Guest
So...
GML:
var hour = date_get_hour(date_current_datetime());

if (hour >= 6 && hour < 18)
    day = true;
else
    day = false;
And use "day"'s true (it is daytime) or false (it is nighttime) value to influence whatever you need to react to whether it is day or night.

Yes, I know it's possible to write
GML:
day = (hour >= 6 && hour < 18);
This works well.But I have some issues when the darkness comes.It just pops in and pops out.And before I used this
reals += time_increment

realm = reals/60

realh = realm/60

darkness = realh/24


So the darkness was slowly coming in and out.
 
Last edited by a moderator:

chamaeleon

Member
Yes, this works perfectly! Now I just need to make the darkness slowly fade in and out as the night comes.
GML:
var h = date_get_hour(date_current_datetime());
var m = date_get_minute(date_current_datetime());
var s = date_get_second(date_current_datetime());
var sec_in_hour = 60*m + s;
var dusk_fraction = 1;
   
if (h >= 18 && h < 19) {
    dusk_fraction = sec_in_hour/3600;
    // Use dusk_fraction's 0 .. 1 value to do something gradually
    // between 6pm and 7pm
}
 
J

JohnSebek

Guest
GML:
var h = date_get_hour(date_current_datetime());
var m = date_get_minute(date_current_datetime());
var s = date_get_second(date_current_datetime());
var sec_in_hour = 60*m + s;
var dusk_fraction = 1;
  
if (h >= 18 && h < 19) {
    dusk_fraction = sec_in_hour/3600;
    // Use dusk_fraction's 0 .. 1 value to do something gradually
    // between 6pm and 7pm
}
Oh yea this worked! i used mostly the var sec_in_hour = 60*m + s; and put it in my code i have now and now it works as i want it! Now i see waht i was doing wrong.Thank you very much.
 
Top