Author Topic: MODULE: Day/Night Cycle (+Seasons)  (Read 1793 times)

Snarky

  • Global Moderator
  • Mittens Earl
  • Private Insultant
    • I can help with proof reading
    •  
    • I can help with translating
    •  
MODULE: Day/Night Cycle (+Seasons)
« on: 05 Jan 2014, 20:25 »
This is a quick little module for having dynamic time in your game, with day/night-cycles and seasons. The module takes care of the timekeeping and working out what that means in terms of time-of-day, season, etc. It doesn't actually make it dark at night or add flowers to your backgrounds in the spring, but the demo game shows how it can be used to create such effects. (Though you'll have to imagine the seasons.)

Simple to use:
Add spoiler tag for Hidden:
Code: Adventure Game Studio
  1. // Setup/start
  2. {
  3.   DayNight.SetTime(1, 4, 55);
  4.   DayNight.SetTimeSpeed(40);
  5.   DayNight.SetTimeRunning(true);
  6. }
  7.  
  8. // Display current time
  9. {
  10.   lblStatus.Text = String.Format("%s, Day %d (%s) %02d:%02d",
  11.                                   DayNight.GetSeasonName(),
  12.                                   DayNight.GetDay(),
  13.                                   DayNight.GetTimeOfDayName(),
  14.                                   DayNight.GetHour(),
  15.                                   DayNight.GetMinute());
  16. }
  17.  
  18. // Add an overlay to make the screen darker at night
  19. {
  20.   DayNight_TimeOfDay timeOfDay = DayNight.GetTimeOfDay();
  21.   if(timeOfDay == DayNight_Night)
  22.   {
  23.     gNightOverlay.Visible = true;
  24.   }
  25. }
Downloads:
v 1.2.0
Day/Night module
Day/Night demo game (AGS 3.3 beta project)
Demo game EXE

v 1.0.1
Day/Night module
Day/Night demo game (AGS 3.3 beta project)
Demo game EXE

Screenshots:

The same screen at night, daybreak and daytime. Precise effect up to game creators.


Demo UI to set the time and clock speed

Version info:
1.2.0 Added year count, simplified and generalized API, optimized internally. Demo game set to 800x600
1.0.1 Fixed bug in demo game, improved responsiveness of notifications when setting time of day manually
1.0.0 Initial release
« Last Edit: 13 Jul 2015, 08:26 by Snarky »

Grim

  • *Dreamer Of The Day*
    • Grim worked on a game that was nominated for an AGS Award!
Re: MODULE: Day/Night Cycle (+Seasons)
« Reply #1 on: 05 Jan 2014, 22:16 »
Nice module, but I got s crash when setting speed to 1 and running it for a while. (didn't save the error message but it was sometime in the afternoon;) and about transparency values having to be between 0 and 100 )

Snarky

  • Global Moderator
  • Mittens Earl
  • Private Insultant
    • I can help with proof reading
    •  
    • I can help with translating
    •  
Re: MODULE: Day/Night Cycle (+Seasons)
« Reply #2 on: 05 Jan 2014, 22:34 »
Ah yes, a little calculation error in the demo game. Should be fixed now. Thanks for the bug report!

Re: MODULE: Day/Night Cycle (+Seasons)
« Reply #3 on: 07 May 2018, 18:11 »
Hello, I want to use it. It didn't need a sunrise light but it's even better like that. Im not sure how to use it, besides importing the script, do I need to add this piece of code below to the global script like when the game starts or where?

Code: Adventure Game Studio
  1. // Setup/start
  2. {
  3.   DayNight.SetTime(1, 4, 55);
  4.   DayNight.SetTimeSpeed(40);
  5.   DayNight.SetTimeRunning(true);
  6. }

And speaking of the night, I tested in GIMP, basically a RGB 0,0,16 is the darkness I need where I can change opacity to 75 of 100, the Tint Screen code that makes it like that only allows 50% and that's not enough to make dark enough. So which one should I change to achieve the 75 of 100 effect? Do I need to add this code and where:


Code: Adventure Game Studio
  1. // Add an overlay to make the screen darker at night
  2. {
  3.   DayNight_TimeOfDay timeOfDay = DayNight.GetTimeOfDay();
  4.   if(timeOfDay == DayNight_Night)
  5.   {
  6.     gNightOverlay.Visible = true;
  7.   }
  8. }

Snarky

  • Global Moderator
  • Mittens Earl
  • Private Insultant
    • I can help with proof reading
    •  
    • I can help with translating
    •  
Re: MODULE: Day/Night Cycle (+Seasons)
« Reply #4 on: 08 May 2018, 18:12 »
Hello, I want to use it. It didn't need a sunrise light but it's even better like that. Im not sure how to use it, besides importing the script

Hi Imc7r,

I would recommend you to take a look at the demo game to see how the module can be used.

Quote
do I need to add this piece of code below to the global script like when the game starts or where?

Code: Adventure Game Studio
  1. // Setup/start
  2. {
  3.   DayNight.SetTime(1, 4, 55);
  4.   DayNight.SetTimeSpeed(40);
  5.   DayNight.SetTimeRunning(true);
  6. }

Yes, you need something like that to initialize the module. Whether it should be in game_start() really depends on your game. Probably in most cases you wouldn't actually start the clock until some specific point in the game (e.g. after an intro cutscene), so that's where you should call SetTimeRunning().

Quote
And speaking of the night, I tested in GIMP, basically a RGB 0,0,16 is the darkness I need where I can change opacity to 75 of 100, the Tint Screen code that makes it like that only allows 50% and that's not enough to make dark enough. So which one should I change to achieve the 75 of 100 effect?

There are a few different ways to create a night effect (tinting, overlays, palette effects, alternate backgrounds...). The demo game just uses the simplest possible one: an overlay (in the form of a full-screen, non-clickable GUI). In your case, I would create an image the size of your game resolution, fill it with 0,0,16 blue, and import it into AGS as a sprite. Create a GUI called gNightOverlay. Make it the size of your screen with x,y coordinates 0,0. Set it to non-clickable and its background to that sprite.

Once you've done that, you just need to display the GUI at 75% opacity when it's night, which...

Quote
Do I need to add this code and where:

Code: Adventure Game Studio
  1. // Add an overlay to make the screen darker at night
  2. {
  3.   DayNight_TimeOfDay timeOfDay = DayNight.GetTimeOfDay();
  4.   if(timeOfDay == DayNight_Night)
  5.   {
  6.     gNightOverlay.Visible = true;
  7.   }
  8. }

Yes. You also need to add a line gNightOverlay.Transparency = 25; so that it's at 75% opacity. I would probably make a function for it, say drawNightOverlay(), and call it from repeatedly_execute_always(). That's how it's done in the demo game.

Once you've got that working, you can take a look at the drawNightOverlay() function there (it's in the GlobalScript) to see how you can also fade the darkness in and out during evening and morning. But I would take it one step at a time. If you can make it be dark at night and light during the day, that's a great first step.

If you have trouble defining and calling functions, I recommend you go through the tutorial and intro to scripting in the manual. Good luck!
« Last Edit: 08 May 2018, 21:06 by Snarky »

Re: MODULE: Day/Night Cycle (+Seasons)
« Reply #5 on: 13 May 2018, 23:31 »
Thanks.. I decided to use even simpler version as you said creating the GUI with image and setting it in function repeatedly_execute_always() then since it didn't work to set myGUI.transparency to myGUI.transparency--; to reduce to 25 since the value did not want to decrease, I used a variable even float to delay the count from 100 to 25 for even slower than 1 second, then it worked like Fade quite smooth enough.

Add spoiler tag for Hidden:
Code: Adventure Game Studio
  1. bool nightFall;
  2. float lightTransparency;
  3. float durationDay;
  4. float durationNight;
  5.  
  6. function repeatedly_execute_always() {
  7.  
  8.   if (FloatToInt(durationDay, eRoundNearest) < 200 && nightFall == false )
  9.   {
  10.     durationDay++;  
  11.     gNightOverlay.Visible = false;
  12.     lightTransparency = 100.00;
  13.   }
  14.  
  15.   if (FloatToInt(durationDay, eRoundNearest) == 200)
  16.   {
  17.     gNightOverlay.Visible = true;
  18.    
  19.     //0.01 is around 3 mins of transition to full darkness
  20.     lightTransparency = lightTransparency - 1.00;
  21.  
  22.     if (lightTransparency > 25.0) { gNightOverlay.Transparency = FloatToInt(lightTransparency, eRoundNearest); }
  23.   }
  24.  
  25.   if (gNightOverlay.Transparency == 26)
  26.   {
  27.     durationDay = 0.0;
  28.     durationNight++;
  29.     nightFall = true;
  30.   }
  31.  
  32.   if (FloatToInt(durationNight, eRoundNearest) == 200)
  33.   {
  34.     lightTransparency = lightTransparency + 1.00;
  35.     if (lightTransparency < 100.0) { gNightOverlay.Transparency = FloatToInt(lightTransparency, eRoundNearest); }
  36.   }
  37.  
  38.   if (gNightOverlay.Transparency == 100 && nightFall == true)
  39.   {
  40.     durationDay++;
  41.     durationNight = 0.0;
  42.     nightFall = false;
  43.   }
  44.  
  45. }
  46.  
« Last Edit: 14 May 2018, 00:06 by Imc7r »

Vincent

  • Jump back into the pond
    • I can help with play testing
    •  
    • I can help with scripting
    •  
    • I can help with translating
    •  
Re: MODULE: Day/Night Cycle (+Seasons)
« Reply #6 on: 14 May 2018, 12:28 »
Offtopic question: what would be the best way to implement the months too into your module? I know that we talked about this before, I would like to do it with their actual lengths at the moment. However, my purpose to keep track of the month lenghts would be to display properly their name when the 'dayOfMonth' change, any suggestion on how to do that?