Ren'Py How to change main menu screen according to system time?

Alvsvartr

New Member
Aug 20, 2022
4
0
Hi all,

So basically, I want to change the main menu image four times a day:
- from 6:00 AM to 11:00 AM - the first image
- from 11:00 AM to 06:00 PM - the second image
- from 06:00 PM to 09:00 PM - the third image
- from 09:00 PM to 06:00 AM - the fourth image

It doesn't look like an utterly impossible task, yet I have no particular idea.

Could you please help me with how I can do this? Thanks.

P.S. Should I go to gui.rpy to do the following?
Code:
define gui.main_menu_background = ["images/start1.png", "images/start2.png", "images/start3.png", "images/start4.png"]
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,969
16,222
Could you please help me with how I can do this? Thanks.
In screens.rpy, add this:
Python:
init python:
    import datetime

define gui.main_menu_background = ["images/start1.png", "images/start2.png", "images/start3.png", "images/start4.png"]

Then, edit the "main_menu" screen:
Python:
screen main_menu():

    ## This ensures that any other menu screen is replaced.
    tag menu

    style_prefix "main_menu"

    # <<<
    #add gui.main_menu_background
    $ ct = datetime.datetime.now().hour
    if 6 <= ct < 11:
        add gui.main_menu_background[0]
    elif 11 <= ct < 18:
        add gui.main_menu_background[1]
    elif 18 <= ct < 21:
        add gui.main_menu_background[2]
    else:
        add gui.main_menu_background[3]
    # >>>

    ## This empty frame darkens the main menu.
    frame:
        [...]

Edit: A typo.
 
Last edited:
  • Like
Reactions: gojira667

Alvsvartr

New Member
Aug 20, 2022
4
0
Thank you very much! I'll try it a bit later. I'm a bit tired since I've managed to achieve the result in an alternative way. Be so kind as to review how bad it really is.

I've spent tons of time choosing the correct function after
Code:
if time_hours >= 6 and time_hours < 11:
The answer below was kindly provided by m_from_space from Lemmasoft.renai.
Code:
init python:
    from datetime import datetime
    
screen main_menu():
    python:
        time_object = datetime.now()
        time_hours = int(time_object.strftime("%H"))
        time_minutes = int(time_object.strftime("%M"))

    if time_hours >= 6 and time_hours < 11:
Soooo, I've updated it into:
Code:
init python:
    from datetime import datetime

screen main_menu():
    python:
        time_object = datetime.now()
        time_hours = int(time_object.strftime("%H"))
        time_minutes = int(time_object.strftime("%M"))

    if time_hours >= 6 and time_hours < 11:
        add "Start1.png"
    if time_hours >= 11 and time_hours < 18:
        add "Start2.png"
    if time_hours >= 18 and time_hours < 21:
        add "Start3.png"
    if time_hours >= 21 and time_hours < 6:
        add "Start4.png"
But my previously made piece of code in gui.rpy
Code:
define gui.main_menu_background = ["images/start1.png", "images/start2.png", "images/start3.png", "images/start4.png"]
conflictied with the above sample.

And I used this one with a transparent image =)
Code:
define gui.main_menu_background = "images/start0.png"
I understand that it is a bit stupid, but it works.

Thank you very much one more time!
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,969
16,222
Code:
        time_object = datetime.now()
        time_hours = int(time_object.strftime("%H"))
It's not your doing, but it really annoy me when people do this.

The copy of the adaptation of some code doing something totally different. This when few minutes looking at the documentation regarding would have told him that there's a method already returning the hour (and in fact any element of the date and time) directly in integer.
When the code rely on something as complex as "transform an integer (the timestamp) into a string, then transform this string into an integer", one should firstly wonder if there isn't a way to simplify all this.

But, well, as I said, it's not your doing. You're the one asking for help, therefore not the one at fault here.
 
  • Like
Reactions: Alvsvartr

Alvsvartr

New Member
Aug 20, 2022
4
0
anne O'nymous, oh, I see! Thank you very much for your time. And for such detailed additional information (my RenPy path looks utterly long yet fascinating).

Your code works perfectly (if someone else will use it and face troubles, the numbering in "[]" should be 0, 1, 2, 3 (noways, it's not a reproach).

Thank you one more time, and good luck!
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,969
16,222
(noways, it's not a reproach).
I have took it that way, don't worry. I type most of the code on the fly, and it's really easy to make a mistake time to time. I'm more wondering why I don't make more often, than why I made one.

This said, thanks to the heads up, I edited my message to correct this error.
 

crabsinthekitchen

Well-Known Member
Apr 28, 2020
1,565
9,079
should also work
Code:
init python:
    import datetime

define gui.main_menu_background = ConditionSwitch("datetime.datetime.now().hour >= 21", "gui/main_menu_night.png",
    "datetime.datetime.now().hour >= 18", "gui/main_menu_evening.png",
    "datetime.datetime.now().hour >= 11", "gui/main_menu_afternoon.png",
    "datetime.datetime.now().hour >= 6", "gui/main_menu_morning.png",
    "True", "gui/main_menu_night.png")
 

Alvsvartr

New Member
Aug 20, 2022
4
0
should also work
Code:
init python:
    import datetime

define gui.main_menu_background = ConditionSwitch("datetime.datetime.now().hour >= 21", "gui/main_menu_night.png",
    "datetime.datetime.now().hour >= 18", "gui/main_menu_evening.png",
    "datetime.datetime.now().hour >= 11", "gui/main_menu_afternoon.png",
    "datetime.datetime.now().hour >= 6", "gui/main_menu_morning.png",
    "True", "gui/main_menu_night.png")
Sounds cool but shows black background only =(
Could you please help with it?