How to add total time played to save/load screen?

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
littanana
Regular
Posts: 48
Joined: Mon Sep 05, 2016 2:15 pm
Tumblr: girlgaymer
Contact:

How to add total time played to save/load screen?

#1 Post by littanana »

I found this thread, but I couldn't manage to get it to work on the load screen. I only got it to work with the toggle button during the actual game, like the code first was meant for.

viewtopic.php?f=8&t=32753

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: How to add total time played to save/load screen?

#2 Post by PyTom »

You want to use a save json callback to store the time played.

Code: Select all

init python:
     def save_playtime(d):
         d["playtime"] = renpy.get_game_runtime()
You can then get that back using FileJson():

Code: Select all

screen whatever():
    $ playtime = FileJson(slot, "playtime", empty=0, missing=0)
[code]

And do whatever you want with it.
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
littanana
Regular
Posts: 48
Joined: Mon Sep 05, 2016 2:15 pm
Tumblr: girlgaymer
Contact:

Re: How to add total time played to save/load screen?

#3 Post by littanana »

PyTom wrote:You want to use a save json callback to store the time played.

Code: Select all

init python:
     def save_playtime(d):
         d["playtime"] = renpy.get_game_runtime()
You can then get that back using FileJson():

Code: Select all

screen whatever():
    $ playtime = FileJson(slot, "playtime", empty=0, missing=0)
[code]

And do whatever you want with it.[/quote]
I'm sorry but I have no idea what to do with the code you gave. In which .rpy file and where do I need to put them for them to show on the save/load screen?

I'm a total beginner.

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

Re: How to add total time played to save/load screen?

#4 Post by gas »

Follow me!

At first, let's create a function that store in a JSON text what you want.
JSON is a kinda etiquette of free text you apply to a slot. While obviously you cannot read the game content before loading it, the trick is saving some data in that label and read it (think of a post-it applied to a closed folder).

Then add this function to the json callbacks, when things are saved.
Callbacks are "things to do when something happen", in that case when the game is saved. So it does automaticly for you.

Code: Select all

init python:
     def save_playtime(d):
         d["playtime"] = renpy.get_game_runtime()
     config.save_json_callbacks = [save_playtime] ### PYTOM??? Why you missed that?
This piece of code can be put wherever you want (usually I create a script called extre.rpy where i put strange stuff like this). You can put it at the end or begin of any script, 'cause the INIT statement have things happen before the game actually begin. Ren'py will crawl thru all of your scripts, searching for all those init stuff, and do them despite where they are (yes, it's enough smart to do that).
Of course you cannot put stuff like in the middle of a label (while is legal to put inbetween labels, but is a very bad practice!)

The second one is more tricky. It goes in the slot screen, where you define what show to users.
You'll retrieve from the JSON text associated to each slot the "playtime" value you saved in.
Something like this (you'll find the lines toward the bottom):

Code: Select all

screen file_slots(title):

    default page_name_value = FilePageNameInputValue()

    use game_menu(title):

        fixed:

            ## This ensures the input will get the enter event before any of the
            ## buttons do.
            order_reverse True

            # The page name, which can be edited by clicking on a button.
            button:
                style "page_label"

                key_events True
                xalign 0.5
                action page_name_value.Toggle()

                input:
                    style "page_label_text"
                    value page_name_value

            ## The grid of file slots.
            grid gui.file_slot_cols gui.file_slot_rows:
                style_prefix "slot"

                xalign 0.5
                yalign 0.5

                spacing gui.slot_spacing

                for i in range(gui.file_slot_cols * gui.file_slot_rows):

                    $ slot = i + 1

                    button:
                        action FileAction(slot)

                        has vbox

                        add FileScreenshot(slot) xalign 0.5

                        text FileTime(slot, format=_("{#file_time}%A, %B %d %Y, %H:%M"), empty=_("empty slot")):
                            style "slot_time_text"

                        text FileSaveName(slot):
                            style "slot_name_text"
                        $ playtime = FileJson(slot, "playtime", empty=0, missing=0) #### <- here
                        text "[playtime]" ### <- and this
                        key "save_delete" action FileDelete(slot)

DANGER!
The time is given in a strange seconds+ decimals format, so maybe you want to convert it in a readable form. Ask me if needed!
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
littanana
Regular
Posts: 48
Joined: Mon Sep 05, 2016 2:15 pm
Tumblr: girlgaymer
Contact:

Re: How to add total time played to save/load screen?

#5 Post by littanana »

Thank you!

I used these to try and figure it out..

viewtopic.php?f=8&t=32753
http://stackoverflow.com/questions/7750 ... nds-to-hms

Code: Select all

$ playtime = FileJson(slot, "playtime", empty=0, missing=0) #### <- here
                        ##text "[playtime]" ### <- and this
                        $ minutes, seconds = divmod(int(playtime), 60)
                        $ hours, minutes = divmod(minutes, 60)
                        text "[hours]:[minutes]:[seconds]"
I uhh, it doesn't seem to work properly though, it isn't accurate? If I open up a save and wait 10 secs or so in game and come to save again it sometimes only goes up by one or two seconds or something like that. What is wrong with the code?

Sorry the time is barely visible, I'm still in the process of changing colors and stuff.
Attachments
Screenshot 2016-09-22 21.53.45.png

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

Re: How to add total time played to save/load screen?

#6 Post by gas »

The code is correct, I've posted it only after testing it.

What's "strange" is the Ren'py inner time counter have a little delay when starting and entering/exiting from game menu. It always eat up some seconds, and I really dunno why.
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
littanana
Regular
Posts: 48
Joined: Mon Sep 05, 2016 2:15 pm
Tumblr: girlgaymer
Contact:

Re: How to add total time played to save/load screen?

#7 Post by littanana »

I meant was the part which I wrote working properly!

Well if there is some weird renpy issue going on, is it possible to set up the counter to not show seconds but instead hours and minutes, and make sure the minutes update every minute the game is open regardless of what menu/window is on?

The delay seems really massive tho.. It is bothering me a lot.

philat
Eileen-Class Veteran
Posts: 1910
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: How to add total time played to save/load screen?

#8 Post by philat »

get_game_runtime() only counts time 1) spent on the top-level context (and not in menus): https://www.renpy.org/doc/html/other.ht ... me_runtime and 2) is (seemingly) for the current session and further, wonky with rollback -- loading a save, rolling back a couple times, and saving will usually result in a 0 time counter, from what I can tell. That was the entire point of the weirdo calc_total_run method I created (in the post linked above); working around iffiness in the time calculated.

User avatar
littanana
Regular
Posts: 48
Joined: Mon Sep 05, 2016 2:15 pm
Tumblr: girlgaymer
Contact:

Re: How to add total time played to save/load screen?

#9 Post by littanana »

philat wrote:get_game_runtime() only counts time 1) spent on the top-level context (and not in menus): https://www.renpy.org/doc/html/other.ht ... me_runtime and 2) is (seemingly) for the current session and further, wonky with rollback -- loading a save, rolling back a couple times, and saving will usually result in a 0 time counter, from what I can tell. That was the entire point of the weirdo calc_total_run method I created (in the post linked above); working around iffiness in the time calculated.

So how can I get the your version of the code to show up in the save/load window below the saves?

User avatar
littanana
Regular
Posts: 48
Joined: Mon Sep 05, 2016 2:15 pm
Tumblr: girlgaymer
Contact:

Re: How to add total time played to save/load screen?

#10 Post by littanana »

Can someone give me advice on how to get it working?

philat
Eileen-Class Veteran
Posts: 1910
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: How to add total time played to save/load screen?

#11 Post by philat »

I mean... everything you need is right here in the thread...? Nonetheless, I'll bite.

Code: Select all

## anywhere
default playtime = 0

init 2 python:

    def save_playtime(d):
        renpy.store.playtime += renpy.get_game_runtime()
        renpy.clear_game_runtime()
        d["playtime"] = renpy.store.playtime

    config.save_json_callbacks = [save_playtime]


## screens.rpy

                        $ playtime = FileJson(slot, "playtime", empty=0, missing=0) 
                        $ minutes, seconds = divmod(int(playtime), 60)
                        $ hours, minutes = divmod(minutes, 60)
                        text "[hours]:[minutes]"
The screens.rpy stuff added because you asked about only showing minutes instead of seconds. This won't fix the fact that get_game_runtime doesn't count time spent in menus, but unless you're going to write your own counter, you're out of luck there.

User avatar
tim640
Regular
Posts: 48
Joined: Wed May 23, 2018 4:47 pm
Contact:

Re: How to add total time played to save/load screen?

#12 Post by tim640 »

philat wrote: Sun Sep 25, 2016 11:57 pm I mean... everything you need is right here in the thread...? Nonetheless, I'll bite.

Guys, so i've been using this code and it works pretty good! Only it shows time in "0:0:0" format. Is there any way to convert it into "00:00:00"? I've been messing with it for a while but just can't get it. Is there a way?

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: How to add total time played to save/load screen?

#13 Post by Remix »

"[hours:02d]:[minutes:02d]:[seconds:02d]"

the :02d basically says, expect the value (the part before the : ) to be a decimal number (actually an integer, not float), then zero pad that to be 2 characters long, so "7" would become "07", "0" would be "00" etc
Frameworks & Scriptlets:

User avatar
tim640
Regular
Posts: 48
Joined: Wed May 23, 2018 4:47 pm
Contact:

Re: How to add total time played to save/load screen?

#14 Post by tim640 »

Ohhhh i see. Thank you for a quick response, that works perfectly! I wouldn't be able to figure it out by myself. So theoretically with that you can go with as many digits as you want. That's super useful!

User avatar
komehara
Regular
Posts: 36
Joined: Fri Jan 31, 2020 10:08 pm
Projects: Spirit Link
Tumblr: hsandt
Deviantart: hsandt
Github: hsandt
itch: komehara
Contact:

Re: How to add total time played to save/load screen?

#15 Post by komehara »

I opened an issue so half of the work (save_playtime) is integrated into Renpy: https://github.com/renpy/renpy/issues/4873

I suspect devs will still have to do the formatting job, but that can always be improved later with some formatting helper functions.

Post Reply

Who is online

Users browsing this forum: No registered users