Countdown Timer with Time Extension or Time Penalty Feature

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
BladeGrip
Newbie
Posts: 2
Joined: Fri Jan 12, 2024 8:55 am
Contact:

Countdown Timer with Time Extension or Time Penalty Feature

#1 Post by BladeGrip »

In the VN I'm developing I need a countdown timer for a planned gameplay purpose. For now I use this code I picked from the forum:

Code: Select all

screen countdown(timer_range, timer_jump):
    zorder 9
    default end_time = time.time() + timer_range
    default current_time = time.time()
    timer 0.05 repeat True action If(
        current_time < end_time,
        true=SetScreenVariable('current_time', time.time()),
        false=[Hide('countdown'), Jump(timer_jump)]
    )
    # We have default dissolve in and dissolve out transform
    bar value (end_time-current_time)*100 range timer_range*100 xalign 0.5 yalign 0.1 xmaximum 300 at notify_appear
The post I picked the code from:
viewtopic.php?p=440383#p440383

I just have to show and hide the screen whenever it's relevant, and so far it works well.
But I'd also like to be able to extend or cut the timer while it's counting down, for the purpose of giving the player character a time extension or penalty depending on the course of action they take.

I figure that I just need to externally modify the end_time screen variable, but I couldn't find how to do it from a label.
The thread itself is over 7 years old, so perhaps there's an easier and better solution today?

jeffster
Veteran
Posts: 409
Joined: Wed Feb 03, 2021 9:55 pm
Contact:

Re: Countdown Timer with Time Extension or Time Penalty Feature

#2 Post by jeffster »

BladeGrip wrote: Sat Mar 09, 2024 4:53 pm In the VN I'm developing I need a countdown timer for a planned gameplay purpose. For now I use this code I picked from the forum:

Code: Select all

screen countdown(timer_range, timer_jump):
    zorder 9
    default end_time = time.time() + timer_range
    default current_time = time.time()
    timer 0.05 repeat True action If(
        current_time < end_time,
        true=SetScreenVariable('current_time', time.time()),
        false=[Hide('countdown'), Jump(timer_jump)]
    )
    # We have default dissolve in and dissolve out transform
    bar value (end_time-current_time)*100 range timer_range*100 xalign 0.5 yalign 0.1 xmaximum 300 at notify_appear
The post I picked the code from:
viewtopic.php?p=440383#p440383

I just have to show and hide the screen whenever it's relevant, and so far it works well.
But I'd also like to be able to extend or cut the timer while it's counting down, for the purpose of giving the player character a time extension or penalty depending on the course of action they take.

I figure that I just need to externally modify the end_time screen variable, but I couldn't find how to do it from a label.
The thread itself is over 7 years old, so perhaps there's an easier and better solution today?
You can "restart" this screen with changed timer_range (and other parameters if necessary).

For example, if you do

Code: Select all

    show screen countdown(time_left, lost)
    #...
then at the point you need to add or subtract some time, you change time_left and do the same "show screen" statement again. I think it will hide the previous screen with that name and show it again, with new parameters.

BladeGrip
Newbie
Posts: 2
Joined: Fri Jan 12, 2024 8:55 am
Contact:

Re: Countdown Timer with Time Extension or Time Penalty Feature

#3 Post by BladeGrip »

Alright, I found a different solution which works well as far as I can see.

First, I declare this in init.

Code: Select all

init python:
    def timer_mod(time_mod):
        global time_remaining
        time_remaining += time_mod
The time_remaining variable is declared somewhere else as default.

Code: Select all

default time_remaining = 40
Then I use this screen to display the countdown timer whenever relevant:

Code: Select all

screen countdown:
    zorder 7
    $ minutes = str(int(time_remaining // 60))
    $ seconds = str(int(time_remaining % 60))
    $ micro_seconds = str(int( (time_remaining % 1) * 10 ))
    if time_remaining <= 5:
        $ display_color = "#FF0000"
    else:
        $ display_color = "#FFFFFF"
    timer 0.1:
        repeat True
        action If(
                time_remaining > 0,
                true=SetVariable("time_remaining", time_remaining - 0.1),
                false=[Hide('countdown'), Jump("timeout")])

    frame:
        add Solid ("#000000")
        xsize 158
        ysize 48
        xalign 0.5
        yalign 0.05
        text "[minutes]:[seconds]:[micro_seconds]" color display_color size 40 yalign 0.5 xpos 0
When I want the countdown timer to start, I determine the amount of time and then show the screen:

Code: Select all

label timedcorridortest:
    ##Other label stuffs
    $time_remaining = 40
    "Get to the exit before time runs out."
    show screen countdown
    ##Other label stuff
When the player character gets a time extension or penalty, I can call the function I previously defined. Positive number for time extension, negative for time penalty.

Code: Select all

label timedcorridortest3:
    ##Other label stuffs
    menu:
        "Time extension":
            $ timer_mod(15)
            
        "Time penalty":
            $ timer_mod(-10)
            
    ##Other label stuffs
The only issue I have now is the fact that this system is prone to a save-load exploit. Saving the game at any point of the timer (for example 5 seconds left) and then loading the save data will set the timer back to the initial value (40 seconds in this example). I'd like to have this issue tackled, but for now I think this will do just fine.

For reference, this is the reddit thread I found the solution from:
https://www.reddit.com/r/RenPy/comments ... ning_time/

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Semrush [Bot]