Problems resetting a timer

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
User avatar
ComputerArt.Club
Veteran
Posts: 427
Joined: Mon May 22, 2017 8:12 am
Completed: Famous Fables, BoPoMoFo: Learn Chinese, Santa's workshop, Cat's Bath, Computer Art Club
Location: Taiwan
Contact:

Problems resetting a timer

#1 Post by ComputerArt.Club »

I have a timer in my game that is called multiple times, the first time it runs as it should, but the second time it starts at zero. I've tried quite a few different things and consulted a relevant thread (viewtopic.php?t=40559)
One difference is that my timer is a displayable and not a screen.

Here is the timer code:

Code: Select all

init python:

    def show_countdown(st, at):
        if st > 10:
            st = 10 # this and the next two lines were one recent failed attempt
            d = Text("{0:.0f}".format(10 - st), color="#fff", size=90) 
            return d, 1 #added but no effect
            # return Text("0"), None # earlier code from the documentation
        else:
            d = Text("{0:.0f}".format(10 - st), color="#fff", size=90)
            return d, 1

image countdown = DynamicDisplayable(show_countdown)   


Here is the timer implemented:

Code: Select all

	hide countdown
        show countdown at Position(xalign=.9, yalign=.1)
        $ renpy.pause(10.0, hard=True)
        return
I also tried adding it into a label that is called, but this did not change anything.

Any ideas?

User avatar
ComputerArt.Club
Veteran
Posts: 427
Joined: Mon May 22, 2017 8:12 am
Completed: Famous Fables, BoPoMoFo: Learn Chinese, Santa's workshop, Cat's Bath, Computer Art Club
Location: Taiwan
Contact:

Re: Problems resetting a timer

#2 Post by ComputerArt.Club »

Actually, maybe I better check my screens file again tomorrow, it would make more sense if it were part of a screen and that the show image parts aren't being used. It's been awhile since I added the timer, I think my code.
Probably the same problem though, the original init code.

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Problems resetting a timer

#3 Post by Remix »

You might be finding that st is not resetting as there is no interaction between hide and show.
Does it work with a dialogue line between them?

If it does, you might get away with $ renpy.restart_interaction() or some redraw call

If you did want a versatile screen based one, maybe:

Code: Select all


style text_timer_ok:
    size 72
    color "#FFF"
    outlines [(2, "#000", 0, 0)]

style text_timer_near:
    size 72
    color "#F22"
    outlines [(2, "#000", 0, 0)]

init python:

    def text_countdown( st, at, 
                        duration = 10.0, 
                        fail_label = 'fail_label', 
                        screen = 'text_timer',
                        ok_style = 'text_timer_ok',
                        near_style = 'text_timer_near',
                        style_swap = 5.0,
                        text_format = "{minutes:02d}:{seconds:02d}:{micro_seconds[0]}" ):
        
        remaining = duration - st

        parts_dict = {
            'minutes' : int( remaining // 60 ),
            'seconds' : int( remaining % 60 ),
            'micro_seconds' : str(int( (remaining % 1) * 10000 ))
        }

        if remaining <= 0.0:
            renpy.hide_screen(screen)
            renpy.call(fail_label)
        
        return Text( text_format.format(**parts_dict), 
                     style = ok_style if remaining > style_swap else near_style), .1

    

screen text_timer( **kwargs ):
    # @param: kwargs
    #
    # duration = 10.0, 
    # success_label = 'success_label',
    # fail_label = 'fail_label', 
    # screen = 'text_timer',
    # ok_style = 'text_timer_ok',
    # near_style = 'text_timer_near',
    # style_swap = 5.0,
    # text_format = "{minutes:02d}:{seconds:02d}:{micro_seconds[0]}"

    vbox:
        add DynamicDisplayable(text_countdown, **kwargs)

        textbutton "Found Me":
            action [ Function(renpy.hide_screen, 'text_timer'), Call(kwargs.get('success_label', 'success_label')) ]



label start:
    "start"
    call screen text_timer
    "middle"
    call screen text_timer(duration=22.0, text_format = "{minutes:02d}:{seconds:02d}" )
    "end"
    
label success_label:
    "Success"
    return

label fail_label:
    "Too slow"
    return
Frameworks & Scriptlets:

User avatar
ComputerArt.Club
Veteran
Posts: 427
Joined: Mon May 22, 2017 8:12 am
Completed: Famous Fables, BoPoMoFo: Learn Chinese, Santa's workshop, Cat's Bath, Computer Art Club
Location: Taiwan
Contact:

Re: Problems resetting a timer

#4 Post by ComputerArt.Club »

Problem solved!
THANK YOU!!!

Post Reply

Who is online

Users browsing this forum: Google [Bot]