Inactivity Timer in a RenPy Game

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.
Message
Author
xyz2501x
Newbie
Posts: 1
Joined: Sat Oct 01, 2016 3:02 pm
Contact:

Inactivity Timer in a RenPy Game

#1 Post by xyz2501x »

Greetings. I have somewhat of an advanced question I'm hoping someone could help me with.

I'm currently trying to create a trivia game using the RenPy engine. So far I have everything down expect one small thing. I'd like the game to have an inactivity timer of sorts. Say, three minutes have passed and no buttons have been pressed ► send the player back to the main menu.

Is something like this possible? I am aware that you can use timers in RenPy. But how do I make it so that the timer starts if there's no activity on the player's part? Thanks in advance!

User avatar
Evildumdum
Regular
Posts: 191
Joined: Sun Jan 18, 2015 8:49 am
Projects: ApoclypseZ
Contact:

Re: Inactivity Timer in a RenPy Game

#2 Post by Evildumdum »

You need a while loop. Set it to +1 to a variable then pause for a second. i'll knock you up a mock version.

Code: Select all

init:
    Timer = 0
    EndTime = 180

start:
    python:
        while Timer <= EndTime:
            Timer += 1
            renpy.pause(1)
    "You Fail"
Of course you can rig it up with screens and bars and such to make it pretty, but this is the basic idea.
"If at first you don't succeed, try hitting it with a shoe."

User avatar
PyTom
Ren'Py Creator
Posts: 16088
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Inactivity Timer in a RenPy Game

#3 Post by PyTom »

Actually, a screen might be better. since you can interact with the game while it's going on.

Code: Select all

screen main_menu_timer():
   timer 180.0 action MainMenu(confirm=False)

label reset_timer():
    hide screen main_menu_timer
    show screen main_menu_timer
    return
You can then do:

Code: Select all

call reset_timer
To start or reset the timer.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

User avatar
Evildumdum
Regular
Posts: 191
Joined: Sun Jan 18, 2015 8:49 am
Projects: ApoclypseZ
Contact:

Re: Inactivity Timer in a RenPy Game

#4 Post by Evildumdum »

Lemme get out my note pad. I might use this myself.
"If at first you don't succeed, try hitting it with a shoe."

ketchup
Newbie
Posts: 4
Joined: Fri Feb 09, 2018 1:23 pm
Contact:

Re: Inactivity Timer in a RenPy Game

#5 Post by ketchup »

I want to use this for inactivty through the whole game. Is there a way to do it on clicks?

Like if someone hasn't clicked for five minutes it goes back to the main menu?

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: Inactivity Timer in a RenPy Game

#6 Post by kivik »

Any particular reason you want to do this? It's a game design choice that benefits nobody and can harm players who needed to go AFK. I genuinely don't recall a single game that I've ever played which would actually abandon game progress from player inaction during gameplay - and I think it's for good reasons.

I'd strongly recommend you don't do this.


If you do want to implement that, please make sure you save the game first. And you'll want to look into screen keys: https://www.renpy.org/doc/html/screens.html#key (look at the customising keymap page for the mouse keys)

Show the screen at all times, and have mouse click action call the reset_timer label.

ketchup
Newbie
Posts: 4
Joined: Fri Feb 09, 2018 1:23 pm
Contact:

Re: Inactivity Timer in a RenPy Game

#7 Post by ketchup »

Hi!

The reason I want to do this is because I made a game for an exhibition! So I wanted it to be able to restart in the case that a player abandoned the station halfway through. This way new players would be able to start right away! Makes sense?

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: Inactivity Timer in a RenPy Game

#8 Post by kivik »

Fair enough. Have you read the documentation I linked? Do you need further help?

ketchup
Newbie
Posts: 4
Joined: Fri Feb 09, 2018 1:23 pm
Contact:

Re: Inactivity Timer in a RenPy Game

#9 Post by ketchup »

This seems to do the trick!

Posting it for anyone who would want to use it. (I realize I am in a very small minority hahah)


screen inactivity_timer():
timer 5.0 action MainMenu(confirm=False)
if 'mousedown_1':
call reset_timer
label reset_timer():
hide screen inactivity_timer
show screen inactivity_timer

initialzero
Regular
Posts: 27
Joined: Fri Jul 08, 2016 6:53 am
Contact:

Re: Inactivity Timer in a RenPy Game

#10 Post by initialzero »

ketchup wrote: Sun Apr 22, 2018 6:58 pm This seems to do the trick!

Posting it for anyone who would want to use it. (I realize I am in a very small minority hahah)


screen inactivity_timer():
timer 5.0 action MainMenu(confirm=False)
if 'mousedown_1':
call reset_timer
label reset_timer():
hide screen inactivity_timer
show screen inactivity_timer
I tried this code but the statement with if it cant be used.

After starting the game, the menu just go back to mainmenu. And if I start the game again, the screen statement doesnt repeat. Maybe the mousedown_1 is not correct?

Could somebody please help me to make it run for the latest version?

(I need this code for an exhibition! If the player dont play it again, the game should be reset to go back to mainmenu)

User avatar
Alex
Lemma-Class Veteran
Posts: 3090
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Inactivity Timer in a RenPy Game

#11 Post by Alex »

initialzero wrote: Fri Jul 23, 2021 7:28 pm ...
Try it like

Code: Select all

screen main_menu_timer():
    timer 180.0 action MainMenu(confirm=False)
    key 'dismiss' action [Hide("main_menu_timer"), Show("main_menu_timer")]

label start:
    "..."
    show screen main_menu_timer
    "... ..."
    "?!"
https://www.renpy.org/doc/html/screens.html#key
https://www.renpy.org/doc/html/screen_actions.html#Hide

initialzero
Regular
Posts: 27
Joined: Fri Jul 08, 2016 6:53 am
Contact:

Re: Inactivity Timer in a RenPy Game

#12 Post by initialzero »

Alex wrote: Sat Jul 24, 2021 2:29 am
initialzero wrote: Fri Jul 23, 2021 7:28 pm ...
Try it like

Code: Select all

screen main_menu_timer():
    timer 180.0 action MainMenu(confirm=False)
    key 'dismiss' action [Hide("main_menu_timer"), Show("main_menu_timer")]

label start:
    "..."
    show screen main_menu_timer
    "... ..."
    "?!"
https://www.renpy.org/doc/html/screens.html#key
https://www.renpy.org/doc/html/screen_actions.html#Hide
Thank you!

But it doesnt run smoothly if I add the screen main_menu_timer in the label section. I can't play further after I add the screen main_menu_timer.

is it possible to use the function rollback?

User avatar
Alex
Lemma-Class Veteran
Posts: 3090
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Inactivity Timer in a RenPy Game

#13 Post by Alex »

initialzero wrote: Sat Jul 24, 2021 5:29 am ...But it doesnt run smoothly if I add the screen main_menu_timer in the label section. I can't play further after I add the screen main_menu_timer....
Oops, lack of testing, sorry. Try

Code: Select all

screen main_menu_timer():
    timer 180.0 action MainMenu(confirm=False)
    key 'dismiss' action [Hide("main_menu_timer"), Show("main_menu_timer"), Return()]
    # or
    # key 'mousedown_1' action [Hide("main_menu_timer"), Show("main_menu_timer")]

initialzero
Regular
Posts: 27
Joined: Fri Jul 08, 2016 6:53 am
Contact:

Re: Inactivity Timer in a RenPy Game

#14 Post by initialzero »

Unfortunately nothing happens while playing the game.
I put the code before label start.

Is there a better solution to solve it?

User avatar
PyTom
Ren'Py Creator
Posts: 16088
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Inactivity Timer in a RenPy Game

#15 Post by PyTom »

Here's my go at it, based on Alex's version.

Code: Select all

screen activity_timer():

    default warning = False

    timer 10.0 action SetScreenVariable("warning", True)
    timer 15.0 action MainMenu(confirm=False)

    if warning:
        text "Timeout in 5 seconds!"


label reset_timer:
    hide screen activity_timer
    show screen activity_timer
    return


label start:

    scene bg washington

    call reset_timer

    "First choice."

    call reset_timer

    "Hard mode - two choices, one timer."

    "And choice 2."

    call reset_timer

    "Part 3."

    return
In this version, calls to reset_timer reset the timer.

Note there's an accessibility issue here. While it might make sense to use this to reset a game in a demo setting, trying to increase game challenge with timers like this hurts people who might not speak the language the game is written in. (As well is people with cognitive disabilities, people with screaming children, etc.)
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

Post Reply

Who is online

Users browsing this forum: No registered users