How to change main menu screen according to system time?

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
Alvsvartr
Newbie
Posts: 20
Joined: Wed Aug 17, 2016 9:31 am
Soundcloud: nebelwand-ua
Location: Ukraine
Contact:

How to change main menu screen according to system time?

#1 Post by Alvsvartr » Sat Aug 20, 2022 8:10 am

Hi all,

So basically, I plan to change the main menu background 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 achieve this? Thanks.

User avatar
m_from_space
Veteran
Posts: 302
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: How to change main menu screen according to system time?

#2 Post by m_from_space » Sat Aug 20, 2022 8:58 am

Since Renpy is in symbiosis with Python, you can just use Python to get the current time via the datetime module. Format it to your needs and then change your main_menu screen according to the variable.

Code: Select all

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:
        ...

Alvsvartr
Newbie
Posts: 20
Joined: Wed Aug 17, 2016 9:31 am
Soundcloud: nebelwand-ua
Location: Ukraine
Contact:

Re: How to change main menu screen according to system time?

#3 Post by Alvsvartr » Sat Aug 20, 2022 11:44 am

m_from_space wrote:
Sat Aug 20, 2022 8:58 am
Since Renpy is in symbiosis with Python, you can just use Python to get the current time via the datetime module. Format it to your needs and then change your main_menu screen according to the variable.
Thank you for your reply.
I'm trying to use it correctly, but it doesn't want to work fine.

So, as the first step, I changed gui.rpy in the following way (I'm not sure in it):

Code: Select all

define gui.main_menu_background = ["images/start1.png", "images/start2.png", "images/start3.png", "images/start4.png"]
Then I went to screens.rpy and used your suggestion as follows:

Code: Select all

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:
        show start1
    if time_hours >= 11 and time_hours < 18:
        show start2
    if time_hours >= 18 and time_hours < 21:
        show start3
    if time_hours >= 21 and time_hours < 6:
        show start4
  
I've tried to use direct ways to start*.png files, but the result still is a mistake.
Be so kind as to help me. Thanks.

User avatar
m_from_space
Veteran
Posts: 302
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: How to change main menu screen according to system time?

#4 Post by m_from_space » Mon Aug 22, 2022 1:52 am

Alvsvartr wrote:
Sat Aug 20, 2022 11:44 am
So, as the first step, I changed gui.rpy in the following way (I'm not sure in it):

Code: Select all

define gui.main_menu_background = ["images/start1.png", "images/start2.png", "images/start3.png", "images/start4.png"]
No, please revert that, we won't need that config variable, since we replace it anyway. Also you cannot just put a list of strings there and hope it will do something. ;)

Code: Select all

define gui.main_menu_background = None
Alter the main menu screen inside screens.rpy:

Code: Select all

init python:
    from datetime import datetime

screen main_menu():
    tag menu
    style_prefix "main_menu"

    python:
        time_object = datetime.now()
        time_hours = int(time_object.strftime("%H"))

    if time_hours >= 6 and time_hours < 11:
        add "start1"
    elif time_hours >= 11 and time_hours < 18:
        add "start2"
    elif time_hours >= 18 and time_hours < 21:
        add "start3"
    else:
        add "start4"
        
    use navigation
You might want to do the same inside "game_menu" screen.

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3636
Joined: Mon Dec 14, 2015 5:05 am
Location: Your monitor
Contact:

Re: How to change main menu screen according to system time?

#5 Post by Imperf3kt » Mon Aug 22, 2022 5:41 am

Note that the above won't reflect changes until the player restarts the game and the time is checked at runtime. There is a way to make it check in real time, but I don't remember the method currently.
Last edited by Imperf3kt on Wed Aug 24, 2022 10:06 am, edited 1 time in total.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor
Free Android GUI - Updated occasionally
Twitter
Imperf3kt Blackjack - a WIP blackjack game for Android made using Ren'Py

User avatar
m_from_space
Veteran
Posts: 302
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: How to change main menu screen according to system time?

#6 Post by m_from_space » Mon Aug 22, 2022 6:22 am

Imperf3kt wrote:
Mon Aug 22, 2022 5:41 am
Note that the above won't reflect changes until the player restarts the game and the time is checked at runtime. There is a way to make it check in real time, but I don't remember the method currently.
Isn't this screen redrawn like other screens? Like when you go to some game_menu screen and back to the main_menu? I mean it's not like this is supposed to change when the player sits hours inside the menu, right?

The time is checked every time the screen is drawn, not just at the start of the game.

edit: You could let it check in "realtime" including a timer that loops and checks after every minute if you want to.

Alvsvartr
Newbie
Posts: 20
Joined: Wed Aug 17, 2016 9:31 am
Soundcloud: nebelwand-ua
Location: Ukraine
Contact:

Re: How to change main menu screen according to system time?

#7 Post by Alvsvartr » Tue Aug 23, 2022 2:26 pm

m_from_space wrote:
Mon Aug 22, 2022 1:52 am

Code: Select all

define gui.main_menu_background = None
Oh, I see! I was a bit tired at the moment and added just a transparent image here =)) I've used manual selection to find the command "add". It was something! Thank you!

And I'd like to add one more variant to make things going:

At first, let's go to the gui.rpy:

Code: Select all

init python:
    import datetime

define gui.main_menu_background = ["images/start1.png", "images/start2.png", "images/start3.png", "images/start4.png"]
Then we should go to the screens.rpy, and make the following:

Code: Select all

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:
        [...]
And that's it =) All thanks go to anne O'nymous from f95zone.to.

Moreover, there's one more alternative:

Code: Select all

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")
Thanks go to crabsinthekitchen from f95zone.to.

So, we have at least three alternatives, yet we can choose all of them.

Alvsvartr
Newbie
Posts: 20
Joined: Wed Aug 17, 2016 9:31 am
Soundcloud: nebelwand-ua
Location: Ukraine
Contact:

Re: How to change main menu screen according to system time?

#8 Post by Alvsvartr » Wed Aug 24, 2022 4:04 am

Imperf3kt wrote:
Mon Aug 22, 2022 5:41 am
Note that the above won't reflect changes until the player restarts the game and the time is checked at runtime. There is a way to make it check in real time, but I don't remember the method currently.
I checked it, and the solution provided by anne O'nymous changes the image in real-time.

User avatar
m_from_space
Veteran
Posts: 302
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: How to change main menu screen according to system time?

#9 Post by m_from_space » Wed Aug 24, 2022 4:32 am

Alvsvartr wrote:
Wed Aug 24, 2022 4:04 am
Imperf3kt wrote:
Mon Aug 22, 2022 5:41 am
Note that the above won't reflect changes until the player restarts the game and the time is checked at runtime. There is a way to make it check in real time, but I don't remember the method currently.
I checked it, and the solution provided by anne O'nymous changes the image in real-time.
Well, it's basically the same solution as I proposed, I just didn't know that you can get the hours out of datetime this easily. My solution doesn't need a list on the other hand, I don't get why this was even suggested. You just don't need a config variable that is there for making changes more easy.

The ConditionSwitch is neat, but on the other hand calls datetime.now() multiple times, so it's not the most efficient (not that it really matters).

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Majestic-12 [Bot]