I want to make a global 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
0xrgb
Newbie
Posts: 3
Joined: Thu Sep 03, 2015 6:36 am
Deviantart: 0xrgb
Github: 0xrgb
Soundcloud: 0xrgb
Contact:

I want to make a global timer.

#1 Post by 0xrgb »

Hello. I'm Korean renpy user, 0xrgb.

I want to make a global timer that records "play time".
It should start when renpy starts, end when renpy ends.
And It must not reset "play time" when renpy restarts. (So it should use persistent values)

I know how to make a simple timer(counter), using DynamicDisplayable or timer in screen.rpy.
So I was trying to make "play time" timer.
But I failed. My version of timer was so buggy, complicated and didn't work properly.

Is there a easy way to make "playtime" timer?

User avatar
PyTom
Ren'Py Creator
Posts: 16096
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: I want to make a global timer.

#2 Post by PyTom »

Here's one way to do it, using config.periodic_callbacks to update a count stored in persistent.

Code: Select all

init python:
    import time

    # Create an object to store the last time playtime was updated.
    # Since we create it in init, it won't be saved or participate
    # in rollback.
    playtime = object()
    playtime.last_update = time.time()

    # Store the total playtime in persistent.total_playtime.
    if persistent.total_playtime is None:
        persistent.total_playtime = 0

    # This is called periodically (several times a second) to update
    # persistent.total_playtime.
    def playtime_callback():
        now = time.time()
        delta = now - playtime.last_update
        persistent.total_playtime += delta
        playtime.last_update = now

    config.periodic_callbacks.append(playtime_callback)

    def dynamic_playtime(st, at):
        return Text("{:.0f}".format(persistent.total_playtime)), 1.0

screen playtime:
    add DynamicDisplayable(dynamic_playtime)

label start:
    show screen playtime

    "..."

    return
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
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: I want to make a global timer.

#3 Post by trooper6 »

PyTom, I looked up config.periodic_callback in the documentation says:
config.periodic_callback = None
If not None, this should be a function. The function is called, with no arguments, at around 20hz.
What does "at around 20hz" mean?
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

User avatar
PyTom
Ren'Py Creator
Posts: 16096
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: I want to make a global timer.

#4 Post by PyTom »

hz is Hertz, which is a standard unit meaning 1/s. So something that is called at 20hz is called 20 times per second.
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
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: I want to make a global timer.

#5 Post by trooper6 »

PyTom wrote:hz is Hertz, which is a standard unit meaning 1/s. So something that is called at 20hz is called 20 times per second.
I know of Hertz as unit of measurement...but my context for it has always been music rather than computer science (A = 440 and all that)...so I was having trouble figuring out how to translate arguments about where A should be to renpy functionality.

Thank you so much!
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

Post Reply

Who is online

Users browsing this forum: Google [Bot]