Page 1 of 2

Inactivity Timer in a RenPy Game

Posted: Sat Oct 01, 2016 3:06 pm
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!

Re: Inactivity Timer in a RenPy Game

Posted: Sat Oct 01, 2016 4:35 pm
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.

Re: Inactivity Timer in a RenPy Game

Posted: Sat Oct 01, 2016 5:23 pm
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.

Re: Inactivity Timer in a RenPy Game

Posted: Sat Oct 01, 2016 5:24 pm
by Evildumdum
Lemme get out my note pad. I might use this myself.

Re: Inactivity Timer in a RenPy Game

Posted: Fri Apr 20, 2018 1:44 pm
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?

Re: Inactivity Timer in a RenPy Game

Posted: Fri Apr 20, 2018 1:52 pm
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.

Re: Inactivity Timer in a RenPy Game

Posted: Sun Apr 22, 2018 12:33 pm
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?

Re: Inactivity Timer in a RenPy Game

Posted: Sun Apr 22, 2018 1:09 pm
by kivik
Fair enough. Have you read the documentation I linked? Do you need further help?

Re: Inactivity Timer in a RenPy Game

Posted: Sun Apr 22, 2018 6:58 pm
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

Re: Inactivity Timer in a RenPy Game

Posted: Fri Jul 23, 2021 7:28 pm
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)

Re: Inactivity Timer in a RenPy Game

Posted: Sat Jul 24, 2021 2:29 am
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

Re: Inactivity Timer in a RenPy Game

Posted: Sat Jul 24, 2021 5:29 am
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?

Re: Inactivity Timer in a RenPy Game

Posted: Sat Jul 24, 2021 7:04 am
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")]

Re: Inactivity Timer in a RenPy Game

Posted: Sat Jul 24, 2021 2:20 pm
by initialzero
Unfortunately nothing happens while playing the game.
I put the code before label start.

Is there a better solution to solve it?

Re: Inactivity Timer in a RenPy Game

Posted: Sat Jul 24, 2021 3:11 pm
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.)