Page 1 of 1

Why doesn't the game save state and return to the main screen?

Posted: Sat Oct 22, 2022 10:44 pm
by span4ev
Since I'm trying to do poker (for practice), I don't have a 'label' with dialogs. I use a lot of 'screens' that call up the following screens.

I don't need labels, I just need screens. I only use label start, since it is fundamental.

But I have a problem: the game doesn't save its state. Every time I go back to the screen 'start screen'.
I don't understand, what does saving state depend on? After loading a save I go back to the screen 'start screen'.

I understand that it's pretty easy to use lots of labels that jump to the next label. But how do I properly use only screens?

I wrote a simple code sample that might serve as an example of roughly what I use

Code: Select all

init:
    $ blinds = 0
    $ players = 0
    $ strn = ''

    $ val_1         = 0
    define val_2    = 0
    default val_3   = 0

init:
    style frame:
        background None


init python:

    def create_table(i, x):
        global blinds
        blinds = i

        renpy.hide_screen('choice_table')
        renpy.show_screen('choice_players')

        change_var(x)

    def create_players(i, x):
        global players
        players = i

        renpy.hide_screen('choice_players')
        renpy.show_screen('table')

        change_var(x)

    def start_game(i, x):
        global strn
        strn = i

        renpy.hide_screen('table')
        renpy.show_screen('new_game')

        change_var(x)

    def change_var(x):
        global val_1, val_2, val_3
        val_1 = x
        val_2 = x
        val_3 = x


screen choice_table:
    add '#111'
    frame:
        vbox:
            text 'SCREEN 1'

            for i in [10, 20, 50, 100]:
                textbutton str(i) action Function(create_table, i, 1)

screen choice_players:
    add '#222'
    frame:
        vbox:
            text 'SCREEN 2'
            for i in [1, 3, 5, 7]:
                textbutton str(i) action Function(create_players, i, 2)

screen table:
    add '#333'
    frame:
        vbox:
            text 'SCREEN 3'
            for i in ['a', 'b', 'c', 'd']:
                textbutton i action Function(start_game, i, 3)

screen new_game:
    add '#444'
    text 'start new game'


screen table_info:
    zorder 999
    frame align(0, .95):
        vbox:
            text 'SCREEN 0'
            text 'blinds '  + str(blinds)
            text 'players ' + str(players)
            text 'strn '    + strn
            text 'val_1 '   + str(val_1)
            text 'val_2 '   + str(val_2)
            text 'val_3 '   + str(val_3)

label start:

    $ Game_running = True
    while Game_running:

        jump game

label game:
    show screen table_info
    call screen choice_table 
What do I need to make the save work?

Re: Why doesn't the game save state and return to the main screen?

Posted: Sun Oct 23, 2022 4:42 am
by Alex
span4ev wrote:
Sat Oct 22, 2022 10:44 pm
...What do I need to make the save work?
Check this page about Ren'Py saving system - https://www.renpy.org/doc/html/save_load_rollback.html

You could try 2 options:
- create a game loop inside a label, so every turn/move will run some statement.
Kind of sample - viewtopic.php?f=51&t=18047&p=235921#p235919
(this is preferable way of doing turn based game),

- use renpy.retain_after_load() before showing your main screen or use this function as 'on show' acion for the screen
https://www.renpy.org/doc/html/save_loa ... after-load
https://www.renpy.org/doc/html/screens.html#on
https://www.renpy.org/doc/html/screen_a ... l#Function
(this is less preferable way, 'cause
'Screens must not cause side effects that are visible from outside the screen. Ren'Py will run a screen multiple times, as it deems necessary. It runs a screen as part of the image prediction process, before the screen is first shown. As a result, if running a screen has side effects, those side effects may occur at unpredictable times.')

Re: Why doesn't the game save state and return to the main screen?

Posted: Sun Oct 23, 2022 5:45 pm
by span4ev
Alex wrote:
Sun Oct 23, 2022 4:42 am
You could try 2 options:
- create a game loop inside a label, so every turn/move will run some statement.
Hi. Thanks for the reply. I tried using a loop inside the label and changing the variable with each step (tried 3 options), but it doesn't work. I added this example code to my question.

Re: Why doesn't the game save state and return to the main screen?

Posted: Sun Oct 23, 2022 10:43 pm
by _ticlock_
span4ev wrote:
Sun Oct 23, 2022 5:45 pm
Hi. Thanks for the reply. I tried using a loop inside the label and changing the variable with each step (tried 3 options), but it doesn't work. I added this example code to my question.
Based on your code, I would suggest doing the following changes:
action Function -> Return(<with some useful data>)
the function you can perform directly in the label
instead of using renpy.hide_screen and renpy.show_screen inside function use just call screen inside the label with action Return()

Approximate changes to your code.

Code: Select all

default blinds = 0
default players = 0
default strn = ''

default val_1         = 0
default val_2    = 0 # Use define for constants, not for variables.
default val_3   = 0

Code: Select all

init python:

    def create_table(i, x):
        global blinds
        blinds = i

        change_var(x)

    def create_players(i, x):
        global players
        players = i

        change_var(x)

    def start_game(i, x):
        global strn
        strn = i

        change_var(x)

    def change_var(x):
        global val_1, val_2, val_3
        val_1 = x
        val_2 = x
        val_3 = x

Code: Select all

screen choice_table:
    add '#111'
    frame:
        vbox:
            text 'SCREEN 1'

            for i in [10, 20, 50, 100]:
                textbutton str(i) action Return([i, 1])

screen choice_players:
    add '#222'
    frame:
        vbox:
            text 'SCREEN 2'
            for i in [1, 3, 5, 7]:
                textbutton str(i) action Return([i, 2])

screen table:
    add '#333'
    frame:
        vbox:
            text 'SCREEN 3'
            for i in ['a', 'b', 'c', 'd']:
                textbutton i action Return([i, 3])

screen new_game:
    add '#444'
    text 'start new game'

Code: Select all

label start:

    $ Game_running = True

label game_loop:
    show screen table_info
    call screen choice_table
    # _return variable stores what you return with action Return()
    $ create_table(*_return)
    
    call screen choice_players
    $ create_players(*_return)
    call screen table
    
    $start_game(*_return)
    call screen new_game
    # I am not sure why you need screen new_game here. I either don't understand the logic, or it is simplified version of your code.
    
    if Game_running:
        jump game_loop
    
    #else
    jump game_results
Note that variable _return stores the value from action Return

Please, consider my changes as an example how you can change your code to achieve the desirable effect. I am not sure what your code is supposed to do, I just approximately changed it as I tried to understand its logic.

game_loop should consist of repeatable lines. and use if statements to either jump back to the game_loop or jump to game_result and some options to start new game.

Re: Why doesn't the game save state and return to the main screen?

Posted: Mon Oct 24, 2022 12:19 am
by span4ev
_ticlock_ wrote:
Sun Oct 23, 2022 10:43 pm
.
Thank you very much for your detailed response. Your changes really work.
But in practice for my master file I could not apply it. I am too stupid for that. I can't take any more of your time, you have given me enough information already. Maybe in the future I can figure it out and apply your approach. Or try to use different lable and try to call screens from there. It's a shame that Renpy has trouble with saving. I guess it should be much simpler than this version
I am not sure why you need screen new_game here. I either don't understand the logic, or it is simplified version of your code.
Yes yes, this is just an example I wrote quickly as an example because the original code is too big. There are classes, dictionaries, lists, functions, styles. I tried to show how screens refer to each other.

Code: Select all

# Use define for constants, not for variables.
All these val_1 variables I declared just to check which variant might work if I change any of these variables.

Thank you very much for your help!

Re: Why doesn't the game save state and return to the main screen?

Posted: Mon Oct 24, 2022 2:43 pm
by _ticlock_
span4ev wrote:
Mon Oct 24, 2022 12:19 am
But in practice for my master file I could not apply it.
span4ev wrote:
Mon Oct 24, 2022 12:19 am
There are classes, dictionaries, lists, functions, styles.
If you managed to write all the necessary logic for the game using python, I don't think it will be hard for you to write it using Renpy label flow. Maybe look at some Renpy minigame examples (that uses screen language) for more ideas on how to make it more simple.
span4ev wrote:
Mon Oct 24, 2022 12:19 am
It's a shame that Renpy has trouble with saving.
I know it is frustrating that you can't just save what you have. However, many factors contribute to this. For example:
  • Ren'Py is a multiplatform engine.
  • Ren'Py uses the Python pickle system to save game state. Some objects can't be saved, like Render objects that Renpy uses for all displayables.
  • Ren'Py target VN. In VN, it is typical that with new chapter (update) of the game, some parts of the previous chapter (version) are changed. Sometimes new routes can be added, etc. In many cases, Ren'Py old save files can be still used in new version, even though some parts were changed. If the Ren'Py would save just the state of the game old save would not work or it would requite much more work for developers to update the old save files.

Re: Why doesn't the game save state and return to the main screen?

Posted: Tue Oct 25, 2022 4:07 pm
by span4ev
_ticlock_ wrote:
Mon Oct 24, 2022 2:43 pm
span4ev wrote:
Mon Oct 24, 2022 12:19 am
But in practice for my master file I could not apply it.
span4ev wrote:
Mon Oct 24, 2022 12:19 am
There are classes, dictionaries, lists, functions, styles.
If you managed to write all the necessary logic for the game using python, I don't think it will be hard for you to write it using Renpy label flow. Maybe look at some Renpy minigame examples (that uses screen language) for more ideas on how to make it more simple.
span4ev wrote:
Mon Oct 24, 2022 12:19 am
It's a shame that Renpy has trouble with saving.
I know it is frustrating that you can't just save what you have. However, many factors contribute to this. For example:
  • Ren'Py is a multiplatform engine.
  • Ren'Py uses the Python pickle system to save game state. Some objects can't be saved, like Render objects that Renpy uses for all displayables.
  • Ren'Py target VN. In VN, it is typical that with new chapter (update) of the game, some parts of the previous chapter (version) are changed. Sometimes new routes can be added, etc. In many cases, Ren'Py old save files can be still used in new version, even though some parts were changed. If the Ren'Py would save just the state of the game old save would not work or it would requite much more work for developers to update the old save files.
Sorry for the delay in replying. I didn't get a notification of a new reply.
I haven't written all the logic yet, because I have encountered the above problem and am trying to solve problems and problems as they arise. The thing is, I know python better than Renpy and I have difficulties with Renpy. I often get confused about its nuances. Yesterday I spent half the night trying to solve an obvious problem with DragGroup... xD
It never occurred to me to look at the mini games, thanks for the tip. Instead I was looking at the code of other games in the DEMO stage to see how they are saved, but they all use "label" there. Haven't found an example where only "screen" is used yet. Maybe I'll find something and figure out how to implement saving or I'll use label.
Thanks