Ren'Py Simple Day Night Cycle

GoodOldChap

New Member
Sep 5, 2021
10
2
I've been working on creating a day and night schedule, but every example that I find is too complicated for what I want. The plan is to have three different states, Morning, Afternoon and Evening, and then move the day forwards when the player does something and then adding a day to the counter at the end. How could I implement this in Ren'py?
 

MissFortune

I Was Once, Possibly, Maybe, Perhaps… A Harem King
Respected User
Game Developer
Aug 17, 2019
5,379
8,646
It really doesn't get simpler than this:

If you're still struggling, I'd say find a game you like that does something similar to what you want to achieve see how they did it - or if they're on the forum/active on social media, then DM them and ask. Most developers are pretty friendly in this space and willing to help where they can.
 

GNVE

Active Member
Jul 20, 2018
724
1,185
If you're still struggling, I'd say find a game you like that does something similar to what you want to achieve see how they did it
To be honest if you are struggling with this then you should do an intro in Python course. This is basic stuff and you will make your life so much easier if you have a basic understanding of Python.
 

MissFortune

I Was Once, Possibly, Maybe, Perhaps… A Harem King
Respected User
Game Developer
Aug 17, 2019
5,379
8,646
To be honest if you are struggling with this then you should do an intro in Python course. This is basic stuff and you will make your life so much easier if you have a basic understanding of Python.
Which I should probably do myself at this point. :KEK:
 
  • Haha
Reactions: GNVE

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,964
16,209
[...] but every example that I find is too complicated for what I want.
I'm pretty sure that 79flavors and me have wrote so many examples of this that you can find more than one that purely rely on a single variable and pure Ren'Py.
 

79flavors

Well-Known Member
Respected User
Jun 14, 2018
1,607
2,256
How simple do you need?

Trust me, I get it. Something someone else finds simple, can look hard when you first see it.
The solution is to experiment. Write it wrong, write it wrong again and keep going until you find something you are happy with (or give up).
Every example I've written about time of day type solutions... I'd write differently.

So let's go really simple...

Python:
default dnc = 0
default cycle = "Morning"

define daynight_list = ["Morning", "Afternoon", "Evening"]

init python:

    def advance_dnc():

        store.dnc += 1
        store.dnc %= 3
        store.cycle = daynight_list[store.dnc]

        return

label start:

    "*** START ***"

label the_loop:

    menu:
        "It's currently [cycle]."
        "Advance":
            $ advance_dnc()
        "Exit":
            jump the_end

    jump the_loop

label the_end:

    "*** THE END ***"
    return

So a little explanation:

I use define to create variables that will never change (while the script is running) and default for variables that WILL change.

[ and ] are used to create a . (see my signature for more reading too).

def advance_dnc(): within a init python: block, just lets me create a new python function to do something. In this case to "advance the day-night-cycle".

var1 += 1 adds 1 to an existing variable called var1. var2 += 6 would add 6.
var1 %= 3 does a calculation (in this case, mod 3). Also see .

Okay... that one is going to need a bit more explaining.

a = x mod y returns the remainder when x is divided by y and stores it in a.
You probably use modulo every day without thinking about it when you look at a clock in the afternoon (mod 12).
new_hour = hour % 12... 13:00 is 1pm, 14:00 is 2pm. 13 / 12 = 1 remainder 1. 14 / 12 = 1 remainder 2.

In this case, lists within python start to be numbered at 0 and we want "Morning", "Afternoon" and "Evening". 3 periods of time. 0, 1, 2. When we get to 3 (that is 2 + 1 = 3), we want it too loop back to 0. Modulo is great for this.

But I could have written:

Python:
        if store.dnc > 2:
            store.dnc = 0

I prefixed variables with store. within the python function to avoid RenPy/python assuming where the variable is kept. Variables within python scripts can be local or global. Prefixing stuff with store. removes all doubt (and is often required).

Implementing this within your game would only require you to add a $ advance_dnc() line anywhere the player does something which would advance the day/night cycle.

There are ways to write the same code without using python functions using def, but they would require using a different mechanism call and return - which is in theory simpler - but I've seen TOO many new RenPy programmers screw up. So much so, I was tempted to not even mention it.

Here are some more complicated solutions. Some allow for 12 hours per day and keeping track of if it's Monday, Tuesday, etc.

https://f95zone.to/threads/renpy-time-advance-issue.35438/post-2695277
https://f95zone.to/threads/resetting-variables-based-on-variable-value-issue.59667/
https://f95zone.to/threads/have-an-...now-how-to-write-the-code.75796/#post-5848292
 
Last edited:

GoodOldChap

New Member
Sep 5, 2021
10
2
How simple do you need?

Trust me, I get it. Something someone else finds simple, can look hard when you first see it.
The solution is to experiment. Write it wrong, write it wrong again and keep going until you find something you are happy with (or give up).
Every example I've written about time of day type solutions... I'd write differently.

So let's go really simple...

Python:
default dnc = 0
default cycle = "Morning"

define daynight_list = ["Morning", "Afternoon", "Evening"]

init python:

    def advance_dnc():

        store.dnc += 1
        store.dnc %= 3
        store.cycle = daynight_list[store.dnc]

        return

label start:

    "*** START ***"

label the_loop:

    menu:
        "It's currently [cycle]."
        "Advance":
            $ advance_dnc()
        "Exit":
            jump the_end

    jump the_loop

label the_end:

    "*** THE END ***"
    return

So a little explanation:

I use define to create variables that will never change (while the script is running) and default for variables that WILL change.

[ and ] are used to create a . (see my signature for more reading too).

def advance_dnc(): within a init python: block, just lets me create a new python function to do something. In this case to "advance the day-night-cycle".

var1 += 1 adds 1 to an existing variable called var1. var2 += 6 would add 6.
var1 %= 3 does a calculation (in this case, mod 3). Also see .

Okay... that one is going to need a bit more explaining.

a = x mod y returns the remainder when x is divided by y and stores it in a.
You probably use modulo every day without thinking about it when you look at a clock in the afternoon (mod 12).
new_hour = hour % 12... 13:00 is 1pm, 14:00 is 2pm. 13 / 12 = 1 remainder 1. 14 / 12 = 1 remainder 2.

In this case, lists within python start to be numbered at 0 and we want "Morning", "Afternoon" and "Evening". 3 periods of time. 0, 1, 2. When we get to 3 (that is 2 + 1 = 3), we want it too loop back to 0. Modulo is great for this.

But I could have written:

Python:
        if store.dnc > 2:
            store.dnc = 0

I prefixed variables with store. within the python function to avoid RenPy/python assuming where the variable is kept. Variables within python scripts can be local or global. Prefixing stuff with store. removes all doubt (and is often required).

Implementing this within your game would only require you to add a $ advance_dnc() line anywhere the player does something which would advance the day/night cycle.

There are ways to write the same code without using python functions using def, but they would require using a different mechanism call and return - which is in theory simpler - but I've seen TOO many new RenPy programmers screw up. So much so, I was tempted to not even mention it.

Here are some more complicated solutions. Some allow for 12 hours per day and keeping track of if it's Monday, Tuesday, etc.

https://f95zone.to/threads/renpy-time-advance-issue.35438/post-2695277
https://f95zone.to/threads/resetting-variables-based-on-variable-value-issue.59667/
https://f95zone.to/threads/have-an-...now-how-to-write-the-code.75796/#post-5848292
Thank you, having it laid out in such as way was helpful in internalizing it.