Total playtime in quit menu

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
JNK_FOX
Newbie
Posts: 12
Joined: Tue Nov 03, 2020 11:08 am
Contact:

Total playtime in quit menu

#1 Post by JNK_FOX »

Hi guys, i would like to add in my quit menu a text that show total playtime(of all the saves). I've tried something like this but it didn't work:

---- playtime.rpy -------

init -20 python:
class PlayTimer:
def __str__(self):
delta = datetime.timedelta(seconds=renpy.get_game_runtime())
mins = delta.seconds // 60
return '{}d, {}h, {}m'.format(delta.days, mins / 60, mins % 60)

playtime = PlayTimer()


---- screen.rpy -----

screen confirm(message, yes_action, no_action):

## Ensure other screens do not get input while this screen is displayed.
modal True

zorder 200

style_prefix "confirm"
add "gui/overlay/confirm.jpg"
text "{=style_prove}FINAL WARNING!{/style_prove}" xpos 0.34 ypos 0.2
vbox:
fixed:
if message == gui.QUIT:
text _('You\'ve spent [playtime] ...') xpos 0.2 ypos 0.2

User avatar
RicharDann
Veteran
Posts: 286
Joined: Thu Aug 31, 2017 11:47 am
Contact:

Re: Total playtime in quit menu

#2 Post by RicharDann »

It will be more helpful if you post your code between [code] and [/code] tags, this way it keeps identation and we can quickly copy/paste it to test for problems.
What error are you getting or what is not working as expected? I don't see you importing the datetime module so that might be the cause.

Code: Select all

init -20 python:
    import datetime
If the problem persists also try str.format() interpolation in your screen:

Code: Select all

if message == gui.QUIT:
    text _("You've spent {} ...".format(playtime)) xpos 0.2 ypos 0.2
The most important step is always the next one.

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3807
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: Total playtime in quit menu

#3 Post by Imperf3kt »

Renpy uses datetime elsewhere for its own internal functions, so it is already imported and there should be no need to import it again.

I don't see anything obvious, but I haven't had a good look.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

User avatar
RicharDann
Veteran
Posts: 286
Joined: Thu Aug 31, 2017 11:47 am
Contact:

Re: Total playtime in quit menu

#4 Post by RicharDann »

I just tested it, it does throw a NameError: global name 'datetime' is not defined. Importing datetime in an init python block in any rpy file does seem to fix that particular issue.
The most important step is always the next one.

JNK_FOX
Newbie
Posts: 12
Joined: Tue Nov 03, 2020 11:08 am
Contact:

Re: Total playtime in quit menu

#5 Post by JNK_FOX »

i dont get any error with my code but it doesnt print any total time either.. it print just the word "[playtime]"

Code: Select all

init -20 python:
    class PlayTimer:
        def __str__(self):
            delta = datetime.timedelta(seconds=renpy.get_game_runtime())
            mins = delta.seconds // 60
            return '{}d, {}h, {}m'.format(delta.days, mins / 60, mins % 60)

    playtime = PlayTimer()


screen confirm(message, yes_action, no_action):

    ## Ensure other screens do not get input while this screen is displayed.
    modal True

    zorder 200

    style_prefix "confirm"
    add "gui/overlay/confirm.jpg"
    text "{=style_prove}FINAL WARNING!{/style_prove}" xpos 0.34 ypos 0.2
    vbox:
        fixed:
                if message == gui.QUIT:
                    text _('You\'ve spent [playtime]...') xpos 0.2 ypos 0.2

User avatar
Vladya
Regular
Posts: 34
Joined: Sun Sep 20, 2020 3:16 am
Github: NyashniyVladya
Contact:

Re: Total playtime in quit menu

#6 Post by Vladya »

JNK_FOX wrote: Tue Nov 17, 2020 5:22 am i dont get any error with my code but it doesnt print any total time either.. it print just the word "[playtime]"
It works exactly as it should. You will not see the error. In new versions of Ren'Py format errors are suppressed (see the "substitute" function in the "renpy-sdk/renpy/substitutions" file). Import the module and everything will work.

UPD: Interesting detail. I looked at the files in the prerelease version. It turns out it was not done on purpose. In Ren'Py 7.4.0, the "raise" operator was added to the function. It means this behavior only in versions 7.1.2 to 7.3.5.

JNK_FOX
Newbie
Posts: 12
Joined: Tue Nov 03, 2020 11:08 am
Contact:

Re: Total playtime in quit menu

#7 Post by JNK_FOX »

Vladya wrote: Tue Nov 17, 2020 6:54 am
JNK_FOX wrote: Tue Nov 17, 2020 5:22 am i dont get any error with my code but it doesnt print any total time either.. it print just the word "[playtime]"
It works exactly as it should. You will not see the error. In new versions of Ren'Py format errors are suppressed (see the "substitute" function in the "renpy-sdk/renpy/substitutions" file). Import the module and everything will work.

UPD: Interesting detail. I looked at the files in the prerelease version. It turns out it was not done on purpose. In Ren'Py 7.4.0, the "raise" operator was added to the function. It means this behavior only in versions 7.1.2 to 7.3.5.
Sorry but i dont get it..which module should i import? substitution.py file?

User avatar
Vladya
Regular
Posts: 34
Joined: Sun Sep 20, 2020 3:16 am
Github: NyashniyVladya
Contact:

Re: Total playtime in quit menu

#8 Post by Vladya »

JNK_FOX wrote: Tue Nov 17, 2020 7:27 am
Vladya wrote: Tue Nov 17, 2020 6:54 am
JNK_FOX wrote: Tue Nov 17, 2020 5:22 am i dont get any error with my code but it doesnt print any total time either.. it print just the word "[playtime]"
It works exactly as it should. You will not see the error. In new versions of Ren'Py format errors are suppressed (see the "substitute" function in the "renpy-sdk/renpy/substitutions" file). Import the module and everything will work.

UPD: Interesting detail. I looked at the files in the prerelease version. It turns out it was not done on purpose. In Ren'Py 7.4.0, the "raise" operator was added to the function. It means this behavior only in versions 7.1.2 to 7.3.5.
Sorry but i dont get it..which module should i import? substitution.py file?
No, no. When I mentioned "substitutions.py" file, I just pointed out where you can see the implementation of the formatting function. You have to import the "datetime" module, as RicharDann has already written above.

JNK_FOX
Newbie
Posts: 12
Joined: Tue Nov 03, 2020 11:08 am
Contact:

Re: Total playtime in quit menu

#9 Post by JNK_FOX »

ok, now we got a little step forward..it print the time but its stuck at 0... sorry for being boring but I'm a beginner

User avatar
Vladya
Regular
Posts: 34
Joined: Sun Sep 20, 2020 3:16 am
Github: NyashniyVladya
Contact:

Re: Total playtime in quit menu

#10 Post by Vladya »

JNK_FOX wrote: Tue Nov 17, 2020 7:50 am ok, now we got a little step forward..it print the time but its stuck at 0... sorry for being boring but I'm a beginner
Because the "screen" is not redrawn. As one option, add timer with the function "renpy.restart_interaction".

Code: Select all

timer .1 repeat True action Function(renpy.restart_interaction)
upd.: BTW, I recommend to use UDD for such purposes. This is very handy and allows you to customize everything exactly as you need it.
https://www.renpy.org/doc/html/udd.html

User avatar
gas
Miko-Class Veteran
Posts: 842
Joined: Mon Jan 26, 2009 7:21 pm
Contact:

Re: Total playtime in quit menu

#11 Post by gas »

You want to do a very stretchy thing. renpy.get_game_runtime give you the time you've spent THIS SESSION.

That's mean you need to do the following stuff:

1. Create a new persistent at start with value 0, if not None. This will register the wholesome game playtime of any session. You'll show this to the players.

2. On start, reset the game_runtime. Set a "clock" variable to 0.

3. You need a function to call in the QUIT screen.
This function must:
3.1 update "clock" by renpy.get_runtime
3.2 add the "clock" to the persistent
3.3 return the persistent into time formula as a string. Hint: you don't need at all any time module, use divmod() and is easier. Or strftime() method.

4. In your QUIT screen, assign the return of the previous FUNCTION (not a class, a function!) to a variable, and quote it in the text (so, quote a variable, not a function!).

And this will work, as I did once with with success.
If you want to debate on a reply I gave to your posts, please QUOTE ME or i'll not be notified about. << now red so probably you'll see it.

10 ? "RENPY"
20 GOTO 10

RUN

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3807
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: Total playtime in quit menu

#12 Post by Imperf3kt »

There is a way to save playtime (per save) using JSON data, perhaps you could leverage that?

PyTom explains how here:
viewtopic.php?t=24001#p298546
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

User avatar
Vladya
Regular
Posts: 34
Joined: Sun Sep 20, 2020 3:16 am
Github: NyashniyVladya
Contact:

Re: Total playtime in quit menu

#13 Post by Vladya »

Made something similar to what you need.
viewtopic.php?t=60644

Post Reply

Who is online

Users browsing this forum: Amazon [Bot]