Showing game stats on file slots?

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.
Message
Author
Adrian_DVL
Regular
Posts: 141
Joined: Fri Mar 15, 2019 8:46 am
Completed: Clockwork Poison
Projects: Melodic Dates, Clockwork Poison: Salvation
Contact:

Showing game stats on file slots?

#1 Post by Adrian_DVL »

Hi again!

I want to change the file_slots screens. I've already made a lot of changes, but I'd like to know if this is possible in Renpy: I want to display some stats (variables and class objects), like MC's name, money, etc of each saved game in its corresponding slot. Can I do that without doing things like setting those variables as persistent or something like that? I'm asking because, since the screen file_slots is accessed outside a saved game (mainly), I'm not sure if I can use normal game variables on it.

EDIT: I was thinking about creating a function that gives the variable save_name a string with changes of lines with \n and changes of font size and even interpolating some images in the text. The problem with that is that I'd like some part of the text to be aligned to the left, others to the right and others to the center, and I don't see how to do this in one string.

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

Re: Showing game stats on file slots?

#2 Post by Imperf3kt »

Yes, I believe that's possible.
I'm not home at the moment, so I'm posting to remind myself to reply later.
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

rayminator
Miko-Class Veteran
Posts: 793
Joined: Fri Feb 09, 2018 12:05 am
Location: Canada
Contact:

Re: Showing game stats on file slots?

#3 Post by rayminator »

yes you can

here is a example but not using the save slots

if you have variables line this

Code: Select all

$ love = 0
how to show up in a sceens or anywhere else is to go like this but you need to know where to put it this how far I can help you... you have to put it somewhere in the screen file_slots(title):

Code: Select all

 [love] 

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

Re: Showing game stats on file slots?

#4 Post by Imperf3kt »

rayminator wrote: Fri May 01, 2020 7:07 pm yes you can

here is a example but not using the save slots

if you have variables line this

Code: Select all

$ love = 0
how to show up in a sceens or anywhere else is to go like this but you need to know where to put it this how far I can help you... you have to put it somewhere in the screen file_slots(title):

Code: Select all

 [love] 
Its not quite that simple, as the stats must be save independant.
There's a way, I think, but it's not something I can recall from memory and must check my project files for an example.
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
MaydohMaydoh
Regular
Posts: 165
Joined: Mon Jul 09, 2018 5:49 am
Projects: Fuwa Fuwa Panic
Tumblr: maydohmaydoh
Location: The Satellite of Love
Contact:

Re: Showing game stats on file slots?

#5 Post by MaydohMaydoh »

https://www.renpy.org/doc/html/save_loa ... -save_name
You could probably use this somehow

Adrian_DVL
Regular
Posts: 141
Joined: Fri Mar 15, 2019 8:46 am
Completed: Clockwork Poison
Projects: Melodic Dates, Clockwork Poison: Salvation
Contact:

Re: Showing game stats on file slots?

#6 Post by Adrian_DVL »

Imperf3kt wrote: Fri May 01, 2020 7:21 pm Its not quite that simple, as the stats must be save independant.
There's a way, I think, but it's not something I can recall from memory and must check my project files for an example.
As you said, when I simply do text "[variable]" inside the button, the variable for the last saved slot repeats itself for all the slots, so every slot has the same information. I'm looking for a way to be independent for each saved game. If you say it's possible, though, I'd appreciate a lot to know how!
MaydohMaydoh wrote: Sat May 02, 2020 12:37 am https://www.renpy.org/doc/html/save_loa ... -save_name
You could probably use this somehow
That's precisely what I tried to do, and it's indeed doable that way. My problem here is that "save_name" takes only one string and, as I said in my original post, I'd like to have some parts of the text aligned to the left, others to the center and others to the right. I've made a concept on Photoshop (it's not a screenshot from the game) and I'd like it to look something like this:
Image
As you can see, I have squares where each variable goes, so in my case, MC's name and job would be aligned to the left, the money and the other currency to the right, girls met, true lovers and songs made to the center, etc. If I make a function that gives save_name a string with all this info, each variable wouldn't fit in each square, because the name or the job or other variables can take different widths.

User avatar
MaydohMaydoh
Regular
Posts: 165
Joined: Mon Jul 09, 2018 5:49 am
Projects: Fuwa Fuwa Panic
Tumblr: maydohmaydoh
Location: The Satellite of Love
Contact:

Re: Showing game stats on file slots?

#7 Post by MaydohMaydoh »

Using the JSON will allow you to save things as list or whatever you want.
Something like:

Code: Select all

name = "Myaido"
money = 500
love = 0

def save_data(d):
    d["save_data"] = [ name, money, love ]
config.save_json_callbacks.append(save_data)
Then retrieve it using

Code: Select all

save_data = FileJson(slot, "save_data")

User avatar
Alex
Lemma-Class Veteran
Posts: 3090
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Showing game stats on file slots?

#8 Post by Alex »

Adrian_DVL wrote: Sat May 02, 2020 9:01 am ...My problem here is that "save_name" takes only one string and, as I said in my original post, I'd like to have some parts of the text aligned to the left, others to the center and others to the right. ...
You could try to make such a system:
- make a screen to dynamicaly set 'save_name' variable

Code: Select all

screen test_save_scr():
    tag menu
    $ store.save_name = var_1+"|"+var_2 # all the variables you want to use, separated with "|"
    timer 0.01 action ShowMenu("save") repeat False
- use this screen when player right click

Code: Select all

$ _game_menu_screen = "test_save_scr"
- modify the 'file_slots' screen to show all the stuff properly (below is just a sample)

Code: Select all

            ## 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"
                        $ some_data = FileSaveName(slot).split("|") # the 'save_name' split to parts
                        if len(some_data) < 2: # if slot is empty there will be only 1 part
                            text "{}".format(some_data[0]):
                                style "slot_name_text"
                        else:
                            hbox:
                                xsize 250
                                text "{}".format(some_data[0]) xalign 0.0
                                text "{}".format(some_data[1]) xalign 1.0

                        key "save_delete" action FileDelete(slot)
Try it with

Code: Select all

label start:
    $ _game_menu_screen = "test_save_scr"

    $ var_1 = "bob"
    $ var_2 = "hp: 100"
    "test save/load"
    "..."
    $ var_1 = "sam"
    $ var_2 = "hp: 20"
    "test save/load 2"
    "?!"

Adrian_DVL
Regular
Posts: 141
Joined: Fri Mar 15, 2019 8:46 am
Completed: Clockwork Poison
Projects: Melodic Dates, Clockwork Poison: Salvation
Contact:

Re: Showing game stats on file slots?

#9 Post by Adrian_DVL »

Alex wrote: Sat May 02, 2020 11:45 am
Adrian_DVL wrote: Sat May 02, 2020 9:01 am ...My problem here is that "save_name" takes only one string and, as I said in my original post, I'd like to have some parts of the text aligned to the left, others to the center and others to the right. ...
You could try to make such a system:
I honestly don't know how you have a solution for everything, but you did help me quite a few times. This works so well! I now have to fix a few things to make all the variables and elements to fit and that would be it. Thank you so much!
MaydohMaydoh wrote: Sat May 02, 2020 11:07 am Using the JSON will allow you to save things as list or whatever you want.
I've also though about using the JSON but I was not quite sure about how to use it. I messed a bit with it and it's pretty doable like that, too. Thank you!

Adrian_DVL
Regular
Posts: 141
Joined: Fri Mar 15, 2019 8:46 am
Completed: Clockwork Poison
Projects: Melodic Dates, Clockwork Poison: Salvation
Contact:

Re: Showing game stats on file slots?

#10 Post by Adrian_DVL »

Alex wrote: Sat May 02, 2020 11:45 am
Adrian_DVL wrote: Sat May 02, 2020 9:01 am ...My problem here is that "save_name" takes only one string and, as I said in my original post, I'd like to have some parts of the text aligned to the left, others to the center and others to the right. ...
You could try to make such a system:
Okay, I positioned all the elements how I wanted to, but a couple things:
First of all, when I try to load a game from the main menu, all the variables are showed as by default, as if Renpy didn't recognize them as stored variables. How do I fix this? I presume that making the variables persistent won't help, as it'd overrate the variables values for the rest of the slots.

Second: even if I fix the above, some of the variables I want to display in the file slot are gotten by a function inside the game. I mean, for instance for the time spent in the game I've made a function that gets the session game and adds it to the persistent.runtime, so I execute these functions before the player click the save button. Shall I expect those variables to be stored the same as the other ones and be displayed properly in the file slot?

User avatar
Alex
Lemma-Class Veteran
Posts: 3090
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Showing game stats on file slots?

#11 Post by Alex »

Adrian_DVL wrote: Sat May 02, 2020 1:51 pm ...
Hm, in my sample 'save_name' variable is used as it usually used. So, as long as your 'save_name' variable is stored in save file it should show up in load screen properly.
Check if you set it properly in "test_save_scr" screen (in my sample).

Also, check the "file_slots" screen if you use the value of 'save_name' variable corresponded to the slot

Code: Select all

$ some_data = FileSaveName(slot).split("|")

Adrian_DVL
Regular
Posts: 141
Joined: Fri Mar 15, 2019 8:46 am
Completed: Clockwork Poison
Projects: Melodic Dates, Clockwork Poison: Salvation
Contact:

Re: Showing game stats on file slots?

#12 Post by Adrian_DVL »

Alex wrote: Sat May 02, 2020 2:43 pm
Adrian_DVL wrote: Sat May 02, 2020 1:51 pm ...
Hm, in my sample 'save_name' variable is used as it usually used. So, as long as your 'save_name' variable is stored in save file it should show up in load screen properly.
Check if you set it properly in "test_save_scr" screen (in my sample).

Also, check the "file_slots" screen if you use the value of 'save_name' variable corresponded to the slot

Code: Select all

$ some_data = FileSaveName(slot).split("|")
I'm really trying to find the issue but I don't know. I tried your code in a test project and it worked perfectly fine, but that's not the case in my project. I even left your comments on the code, changing just the name of variables and that's it:

Code: Select all

screen test_save_scr():
    tag menu
    $ store.save_name = var_1+"|"+var_2+"|"+var_3+"|"+var_4+"|"+var_5+"|"+var_6+"|"+var_7 # all the variables you want to use, separated with "|"
    timer 0.01 action ShowMenu("save") repeat False

Code: Select all

grid 1 5:
                style_prefix "slot"

                xalign 0.65
                ypos -34

                spacing 5

                for i in range(5):
                    $ slot = i + 1

                    button:
                        action FileAction(slot)

                        has vbox

                        $ some_data = FileSaveName(slot).split("|") # the 'save_name' split to parts
                        if len(some_data) < 7: # if slot is empty there will be only 1 part
                            text "{}".format(some_data[0]):
                                style "slot_name_text"
                        else:
                            vbox:
                                xsize 500
                                ysize 100
                                hbox:
                                    text "{}".format(some_data[0]) xpos 6 xalign 0.0 ypos 10
                                    text "{}".format(some_data[1]) xpos 160 xalign 1.0 ypos 10
                                    text "{}".format(some_data[2]) xpos 220 xalign 1.0 ypos 10
                                hbox:
                                    text "{}".format(some_data[3]) xpos 6 xalign 0.0 ypos 76
                                    text "{}".format(some_data[4]) xpos 126 xalign 1.0 ypos 76
                                    text "{}".format(some_data[5]) xpos 184 xalign 1.0 ypos 76
                                    text "{}".format(some_data[6]) xpos 240 xalign 1.0 ypos 76
                                hbox:
                                    text FileTime(slot, format=_("{#file_time}%A, %B %d %Y, %H:%M"), empty=_("empty slot")):

                        key "save_delete" action FileDelete(slot)
Then, I only allow player to save in the game main hub, so instead of setting _game_menu_screen to the screen, I do this:

Code: Select all

label room:
        $ metnum = glist.metnum()
        $ maxnum = glist.maxnum()
        $ calc_total_run()
        $ minutes, seconds = divmod(int(persistent.runtime), 60)
        $ hours, minutes = divmod(minutes, 60)
        $ jobname()
        $ var_1 = "{size=16}[name], [job!l]{/size}"
        $ var_2 = "{size=16}[money]${/size}"
        $ var_3 = "{size=16}[clef]cl.{/size}"
        $ var_4 = "{size=12}[hours] h and [minutes] m in town{/size}"
        $ var_5 = "{size=12}[metnum] girls met{/size}"
        $ var_6 = "{size=12}[maxnum] true lovers{/size}"
        $ var_7 = "{size=12}[songs] songs made"
        ...
This is the label where the player gets a screen with a button to save the game. I set the action of that button like this:

Code: Select all

action [Play("sound", "audio/hi.mp3"), ShowMenu('test_save_scr')]
Maybe I'm spoiling something doing this, I don't know. I even tried setting $ _game_menu_screen = "test_save_scr" and saving the game via right click, just in case, but that doesn't change anything. Like I said, your code on a brand new test project works perfect.

User avatar
Alex
Lemma-Class Veteran
Posts: 3090
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Showing game stats on file slots?

#13 Post by Alex »

Adrian_DVL wrote: Sat May 02, 2020 3:19 pm ...
Looks like you ought to move all the sizing and other stuff to the 'save_slot' screen.

In 'test_save_scr' then you can use your own variables, like

Code: Select all

$ store.save_name = name+", "+job+"|"+money+"|"+clef+"|"+hours+" h and "+minutes+" m in town"+"|"+metnum+"|"+maxnum+"|"+songs

Code: Select all

                                hbox:
                                    text "{size=16}{}{/size}".format(some_data[0]) xpos 6 xalign 0.0 ypos 10
                                    text "{size=16}{}${/size}".format(some_data[1]) xpos 160 xalign 1.0 ypos 10
                                    text "{size=16}{}cl.{/size}".format(some_data[2]) xpos 220 xalign 1.0 ypos 10
                                hbox:
                                    text "{size=12}{}{/size}".format(some_data[3]) xpos 6 xalign 0.0 ypos 76
                                    text "{size=12}{} girls met{/size}".format(some_data[4]) xpos 126 xalign 1.0 ypos 76
                                    text "{size=12}{} true lovers{/size}".format(some_data[5]) xpos 184 xalign 1.0 ypos 76
                                    text "{size=12}{} songs made{/size}".format(some_data[6]) xpos 240 xalign 1.0 ypos 76

Adrian_DVL
Regular
Posts: 141
Joined: Fri Mar 15, 2019 8:46 am
Completed: Clockwork Poison
Projects: Melodic Dates, Clockwork Poison: Salvation
Contact:

Re: Showing game stats on file slots?

#14 Post by Adrian_DVL »

Alex wrote: Sat May 02, 2020 3:45 pm
Adrian_DVL wrote: Sat May 02, 2020 3:19 pm ...
Looks like you ought to move all the sizing and other stuff to the 'save_slot' screen.

In 'test_save_scr' then you can use your own variables, like

Code: Select all

$ store.save_name = name+", "+job+"|"+money+"|"+clef+"|"+hours+" h and "+minutes+" m in town"+"|"+metnum+"|"+maxnum+"|"+songs

Code: Select all

                                hbox:
                                    text "{size=16}{}{/size}".format(some_data[0]) xpos 6 xalign 0.0 ypos 10
                                    text "{size=16}{}${/size}".format(some_data[1]) xpos 160 xalign 1.0 ypos 10
                                    text "{size=16}{}cl.{/size}".format(some_data[2]) xpos 220 xalign 1.0 ypos 10
                                hbox:
                                    text "{size=12}{}{/size}".format(some_data[3]) xpos 6 xalign 0.0 ypos 76
                                    text "{size=12}{} girls met{/size}".format(some_data[4]) xpos 126 xalign 1.0 ypos 76
                                    text "{size=12}{} true lovers{/size}".format(some_data[5]) xpos 184 xalign 1.0 ypos 76
                                    text "{size=12}{} songs made{/size}".format(some_data[6]) xpos 240 xalign 1.0 ypos 76
That doesn't work either due to two reasons: the first one is that I got a Coercing to Unicode, int found error, probably because I'm using an int in save_name (not like last time, when it was a string with an int inside) and the second reason is that I get a Key Error {size=16}, probably because python is detecting the size tag as the variable instead of the next {}. Any other idea?

EDIT: I checked your previous code again (the one with var_1, var_2, etc) in a test project, but defaulting a variable money to 200 and then changing it in the game before saving the game. When saving the game, the new money value is displayed in the slot, but when I try to load the game from the main menu, the default value is displayed, just like it happens in my main project...

Adrian_DVL
Regular
Posts: 141
Joined: Fri Mar 15, 2019 8:46 am
Completed: Clockwork Poison
Projects: Melodic Dates, Clockwork Poison: Salvation
Contact:

Re: Showing game stats on file slots?

#15 Post by Adrian_DVL »

Guys, I've tried just by giving the variable save_name a string with all the variables and even a few interpolated images, but the outcome is just about the same as with Alex's code. Seems the problem here are the variables themselves, as they won't be stored and be displayed while in the main menu.
Here's what I got in the save screen inside the game:
Image
Here's what I got in the load screen in the main menu:
Image
Clearly my problem is that I'm not able to access game variables outside the game. If I was, then I'd be able to implement Alex's code, as it's the fanciest and most functional way to do this.
It seems my best choice is to use FileJson somehow as MaydohMaydoh suggested, since I'm sure that way the variables will be accessed outside the game, but I don't know how to use it properly to display different things in a file slot. Can someone help me?

Post Reply

Who is online

Users browsing this forum: piinkpuddiin