Total playtime in quit menu
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.
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.
Total playtime in quit menu
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
---- 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
- RicharDann
- Veteran
- Posts: 286
- Joined: Thu Aug 31, 2017 11:47 am
- Contact:
Re: Total playtime in quit menu
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.
If the problem persists also try str.format() interpolation in your screen:
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
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.
- 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
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.
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
pro·gram·mer (noun) An organism capable of converting caffeine into code.
Current project: GGD Mentor
- RicharDann
- Veteran
- Posts: 286
- Joined: Thu Aug 31, 2017 11:47 am
- Contact:
Re: Total playtime in quit menu
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.
Re: Total playtime in quit menu
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
Re: Total playtime in quit menu
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.
Re: Total playtime in quit menu
Sorry but i dont get it..which module should i import? substitution.py file?Vladya wrote: ↑Tue Nov 17, 2020 6:54 amIt 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.
Re: Total playtime in quit menu
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 wrote: ↑Tue Nov 17, 2020 7:27 amSorry but i dont get it..which module should i import? substitution.py file?Vladya wrote: ↑Tue Nov 17, 2020 6:54 amIt 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.
Re: Total playtime in quit menu
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
Re: Total playtime in quit menu
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)
https://www.renpy.org/doc/html/udd.html
Re: Total playtime in quit menu
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.
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
10 ? "RENPY"
20 GOTO 10
RUN
- 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
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
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
pro·gram·mer (noun) An organism capable of converting caffeine into code.
Current project: GGD Mentor
Re: Total playtime in quit menu
Made something similar to what you need.
viewtopic.php?t=60644
viewtopic.php?t=60644
Who is online
Users browsing this forum: Amazon [Bot]