[Solved] Why my game doesn't work when I save/load it ?

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
renardjap
Regular
Posts: 75
Joined: Sun Aug 05, 2018 1:08 pm
Location: France ! Cocorico
Contact:

[Solved] Why my game doesn't work when I save/load it ?

#1 Post by renardjap »

Hi everyone

In my game, the player can create a mini city like in the Travian Game (he clicks on an area and decides which building he wants build). You have an imagemap with some hotspot. When you click on them, it opens a a screen listing some buildings you can build (as image button). Click on a building and it appears on the chosen hotspot. When you click on the building, this one open a new screen listing all the informations about the building.

I wrote a code which has no issues when we start a new game, but when we save the game and reload it, nothing works... I use the => on "show" action Function(renpy.retain_after_load) in the village_screen to save the localization of each buildings. But when I reload the game, all the images of the buildings have sometimes disappeared or when we click on it, it doesn't work. How can I save and reload the game without issues ?

Code: Select all

init python:
    class Place:
        places = [] # contains all the instantiated places..
        
        def __init__(self, img, loc, panel = 0):
            self.img = img
            self.loc = loc
            self.panel = panel 
            
            self.places.append(self) # append the created place in the places class attrib...
            
    class Building:
        def __init__(self, name, img, loc = 0, timeleft = 0, time_build = 0):
            self.name = name
            self.img = img
            self.timeleft = timeleft
            self.time_build = time_build 
            Building.loc = loc 
            
        @classmethod 
        def attribution(cls, building_image):
            for place in Place.places: # reference the places then loop through it...
                if place.loc == cls.loc:
                    Place.places[Place.places.index(place)].img = building_image 

    def change_loc(attribute, coordonate):
        Building.loc = coordonate

    def open_panel(panel_value): ## Let opens the screen of each building
        for i in Place.places:
            if i.loc == panel_value:
                Place.places[Place.places.index(i)].panel = 1

        for j in Place.places: 
            if j.img != None and j.panel == 1: 
                Place.places[Place.places.index(j)].panel = 0 
                if Place.places[Place.places.index(j)].img == "images/house.png":
                    renpy.show_screen("house_screen")
                elif Place.places[Place.places.index(j)].img == "images/military_house.png":
                    renpy.show_screen("military_screen")
                elif Place.places[Place.places.index(j)].img == "images/forum.png":
                    renpy.show_screen("forum_screen")



default place01 = Place("", 1)
default place02 = Place("", 2)
default place03 = Place("", 3)

define building01 = Building("House","images/house.png")
define building02 = Building("Forum","images/forum.png")
define building03 = Building("Military house","images/military_house.png")


## All the screens 
screen village_screen: ## This screen contains the hotspots where we can build the buildings
    on "show" action Function(renpy.retain_after_load)
    zorder 3
    imagemap:
        idle "images/village_background.png"

        if place01.img == "":
            hotspot (545,250,205,198) clicked [Show("constructable_buildings_screen"), Function(change_loc, building01.loc, place01.loc)]

        else:
            imagebutton:
                focus_mask True 
                idle place01.img
                xalign 0.3
                yalign 0.20
                action  Function(open_panel, place01.loc) #When I click on imageboutton, nothing happen

        if place02.img == "":
            hotspot (529,508,216,147) clicked [Show("constructable_buildings_screen"), Function(change_loc, building01.loc, place02.loc)]

        else:
            imagebutton:
                focus_mask True
                idle place02.img
                xalign 0.3
                yalign 0.50
                action  Function(open_panel, place02.loc)
        
        if place03.img == "":
            hotspot (761,649,185,104) clicked [Show("constructable_buildings_screen"), Function(change_loc, building01.loc, place03.loc)]

        else:
            imagebutton:
                focus_mask True
                idle place03.img
                xalign 0.45
                yalign 0.60
                action  Function(open_panel, place03.loc)

screen constructable_buildings_screen: ## This screen lists all the building that the player can create. If he clicks on a building (imagebutton) this one is build on the choosen hotspot 

    zorder 4
    vbox:
        xalign 0.5
        yalign 0.5
        add "images/bg_select_screen.png"
    vbox:
        xalign 0.5
        yalign 0.5
        imagebutton: #After load, When I click on imageboutton, nothing happens. Same for all the imagebuttons of the screen
            focus_mask True
            idle building01.img
            action Function(Building.attribution, building01.img)

        imagebutton:
            focus_mask True
            idle building02.img
            action Function(Building.attribution, building02.img)

        imagebutton:
            focus_mask True
            idle building03.img
            action Function(Building.attribution, building03.img)

    hbox:
        xalign 0.5
        yalign 0.8
        textbutton "Quitter" action [Hide("constructable_buildings_screen")]

screen house_screen: #When we click on a building, the associated panel is open. But after load, when I click on the building, nothing happens... 
    zorder 4
    vbox:
        xalign 0.5
        yalign 0.5
        add "images/bg_building_menu_screen.png"
    vbox:
        xalign 0.5
        yalign 0.5
        text "You are in a [building01.name]"
        textbutton "Quit" action Hide("house_screen")

screen forum_screen: #When we click on a building, the associated panel is open. But after load, when I click on the building, nothing happens... 
    zorder 4
    vbox:
        xalign 0.5
        yalign 0.5
        add "images/bg_building_menu_screen.png"
    vbox:
        xalign 0.5
        yalign 0.5
        text "You are in a [building02.name]"
        textbutton "Quit" action Hide("forum_screen")

screen military_screen: #When we click on a building, the associated panel is open. But after load, when I click on the building, nothing happens... 
    zorder 4
    vbox:
        xalign 0.5
        yalign 0.5
        add "images/bg_building_menu_screen.png"
    vbox:
        xalign 0.5
        yalign 0.5
        text "You are in a [building03.name]"
        textbutton "Quit" action Hide("military_screen")

screen modal_screen:
    zorder 1
    add "#000"
    modal True


# The game starts here.

label start:
    
    show screen village_screen
    show screen debug_screen
    show screen modal_screen

    "You've created a new Ren'Py game."

    return

Last edited by renardjap on Wed Jul 22, 2020 4:17 am, edited 1 time in total.

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

Re: Why my game doesn't work when I save/load it ?

#2 Post by Alex »

Try to add a text line before showing your game screens and check if it will change the behaviour of your game (likely it won't).

Code: Select all

label start:
    "..."
    
    show screen village_screen
    show screen debug_screen
    show screen modal_screen
Try to redesign your code, like make a game loop in a label (not inside a python block), put all the game logic in it (like waiting for user interactions, changing variables, etc.) and use screens to just vizualize the game state. This should make game savable.

User avatar
Per K Grok
Miko-Class Veteran
Posts: 882
Joined: Fri May 18, 2018 1:02 am
Completed: the Ghost Pilot, Sea of Lost Ships, Bubbles and the Pterodactyls, Defenders of Adacan Part 1-3, the Phantom Flyer
itch: per-k-grok
Location: Sverige
Contact:

Re: Why my game doesn't work when I save/load it ?

#3 Post by Per K Grok »

renardjap wrote: Tue Jun 23, 2020 5:57 am Hi everyone

In my game, the player can create a mini city like in the Travian Game (he clicks on an area and decides which building he wants build). You have an imagemap with some hotspot. When you click on them, it opens a a screen listing some buildings you can build (as image button). Click on a building and it appears on the chosen hotspot. When you click on the building, this one open a new screen listing all the informations about the building.

I wrote a code which has no issues when we start a new game, but when we save the game and reload it, nothing works... I use the => on "show" action Function(renpy.retain_after_load) in the village_screen to save the localization of each buildings. But when I reload the game, all the images of the buildings have sometimes disappeared or when we click on it, it doesn't work. How can I save and reload the game without issues ?


----


I have made some test runs of your game.

My conclusion is that the problem is in the function open_panel. I can not really pinpoint what the problem is. I would suspect that it could have something to do with global vs local variables.

However with these changes it seems like I have bypassed the problem.

Code: Select all


    def open_panel(panel_value): ## Let opens the screen of each building
        if panel_value == "images/house.png":
            renpy.show_screen("house_screen")
        elif panel_value == "images/military_house.png":
            renpy.show_screen("military_screen")
        elif panel_value ==  "images/forum.png":
            renpy.show_screen("forum_screen")
            
            
            
--------------------------

                action  Function(open_panel, str(place01.img))
                
--------------------------------------------                
                
                action  Function(open_panel, str(place02.img))
                
--------------------------------------------
                
                action  Function(open_panel, str(place03.img))

----------------------------------------------            



User avatar
renardjap
Regular
Posts: 75
Joined: Sun Aug 05, 2018 1:08 pm
Location: France ! Cocorico
Contact:

Re: Why my game doesn't work when I save/load it ?

#4 Post by renardjap »

Alex wrote: Tue Jun 23, 2020 11:52 am Try to add a text line before showing your game screens and check if it will change the behaviour of your game (likely it won't)
I tried but nothing changed...
Alex wrote: Tue Jun 23, 2020 11:52 am
Try to redesign your code, like make a game loop in a label (not inside a python block), put all the game logic in it (like waiting for user interactions, changing variables, etc.) and use screens to just vizualize the game state. This should make game savable.
I don't understand how I can do this... Can you show me a quick example ?
Per K Grok wrote: Tue Jun 23, 2020 12:39 pm
I have made some test runs of your game.

My conclusion is that the problem is in the function open_panel. I can not really pinpoint what the problem is. I would suspect that it could have something to do with global vs local variables.

However with these changes it seems like I have bypassed the problem.

Code: Select all


    def open_panel(panel_value): ## Let opens the screen of each building
        if panel_value == "images/house.png":
            renpy.show_screen("house_screen")
        elif panel_value == "images/military_house.png":
            renpy.show_screen("military_screen")
        elif panel_value ==  "images/forum.png":
            renpy.show_screen("forum_screen")
            
            
            
--------------------------

                action  Function(open_panel, str(place01.img))
                
--------------------------------------------                
                
                action  Function(open_panel, str(place02.img))
                
--------------------------------------------
                
                action  Function(open_panel, str(place03.img))

----------------------------------------------            


I tried your new code and actually it fixes the bug "cant open the panel when I click on the building". But when I save/load the game and I click on a empty place to build a new building, I can open the "constructable_buildings_screen" which list all the buildings available but when I click on it, nothing happens.

I think the problem comes from the loops. I don't know why, when I create a new game, the loops work perfectly and I can build and open any panels without problem. But, once I saved/loaded the game, the loops don't work. May I use the renpy.store() function to save the content of the loops (How I can do that ?) Or is it a Ren'Py bug ?

edit: I did some tests and it's very strange. The loop of attribution(cls, building_img) works and "Place.places[Place.places.index(place)].img" contains the right image.

Code: Select all

        @classmethod 
        def attribution(cls, building_image): 
            global place_img_content # test variable to check the content of the loop
            for place in Place.places: # reference the places then loop through it...
                if place.loc == cls.loc:
                    Place.places[Place.places.index(place)].img = building_image
                    place_img_content = Place.places[Place.places.index(place)].img ## place_img contains the right image
                    renpy.notify("the loop works") # I can see the message, means the loop works
The problem is here, when Place.places[Place.places.index(place)].img contains the right image, it doesn't transfer to the constructor... I don't have any idea why... And it happens only when I reload the game...
Is it perhaps a bug with the default keyword ?

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

Re: Why my game doesn't work when I save/load it ?

#5 Post by Alex »

renardjap wrote: Wed Jun 24, 2020 3:09 am ... Can you show me a quick example ? ...
The idea is to make screens return some values instead of running functions. When in a loop you could evaluate those values to run some functions / show screens in separate lines of code. This should make game be savable.

mock-up sample

Code: Select all

label loop:
    show screen my_screen
    while True:
        $ res = ui.interact()

        if res == 'build_farm': #<--- check the value got from the screen
            $ change_loc(building01.loc, place02.loc)
            show screen constructable_buildings_screen
            $ renpy.pause(0.5, hard=True) # if needed
            
        elif res == ???, etc.
https://www.renpy.org/doc/html/screen_p ... i.interact

User avatar
Per K Grok
Miko-Class Veteran
Posts: 882
Joined: Fri May 18, 2018 1:02 am
Completed: the Ghost Pilot, Sea of Lost Ships, Bubbles and the Pterodactyls, Defenders of Adacan Part 1-3, the Phantom Flyer
itch: per-k-grok
Location: Sverige
Contact:

Re: Why my game doesn't work when I save/load it ?

#6 Post by Per K Grok »

renardjap wrote: Wed Jun 24, 2020 3:09 am
-----

I tried your new code and actually it fixes the bug "cant open the panel when I click on the building". But when I save/load the game and I click on a empty place to build a new building, I can open the "constructable_buildings_screen" which list all the buildings available but when I click on it, nothing happens.

I think the problem comes from the loops. I don't know why, when I create a new game, the loops work perfectly and I can build and open any panels without problem. But, once I saved/loaded the game, the loops don't work. May I use the renpy.store() function to save the content of the loops (How I can do that ?) Or is it a Ren'Py bug ?

-----

Just for the hell of it I tried to get the same function without using class, object or functions.

This is what I came up with.

Code: Select all



define building = ["house","forum","military_house"]

default place=[-1,-1,-1]

default chosenplc=1
default type=2

screen base:

    if place[0]==-1:
        button:
            area (100, 100, 100, 100)
            action SetVariable("chosenplc",0), Show("pickbuild")
    else:
        imagebutton:
            idle building[place[0]]
            pos (100,100)
            action Hide("pickbuild"), SetVariable("type", place[0]), Show("Thisis")

    if place[1]==-1:
        button:
            area (200, 200, 100, 100)
            action SetVariable("chosenplc",1), Show("pickbuild")

    else:
        imagebutton:
            idle building[place[1]]
            pos (200,200)
            action Hide("pickbuild"), SetVariable("type", place[1]), Show("Thisis")

    if place[2]==-1:
        button:
            area (300, 300, 100, 100)
            action SetVariable("chosenplc",2), Show("pickbuild")

    else:
        imagebutton:
            idle building[place[2]]
            pos (300,300)
            action Hide("pickbuild"), SetVariable("type", place[2]), Show("Thisis")
    frame:
        pos (100, 310)
        vbox:
            textbutton "Reset Area 1" action Jump("reset1")
            textbutton "Reset Area 2" action Jump("reset2")
            textbutton "Reset Area 3" action Jump("reset3")

screen pickbuild:
    frame:
        vbox:
            imagebutton:
                idle "house"
                action [SetVariable("type", 0), Hide("pickbuild"), Jump("buildz"), SetVariable("chosenplc",-1)]
            imagebutton:
                idle "forum"
                action [SetVariable("type", 1), Hide("pickbuild"), Jump("buildz"),SetVariable("chosenplc",-1)]
            imagebutton:
                idle "military_house"
                action [SetVariable("type", 2), Hide("pickbuild"), Jump("buildz"), SetVariable("chosenplc",-1)]
            textbutton "Quit" action [SetVariable("chosenplc",-1), Hide("pickbuild")]


screen Thisis():
    frame:
        xpos 110
        vbox:
            text "This is a " + building[type]
            textbutton "Quit" action  Hide("Thisis")


label start:

    scene village_background


label map:
    show screen base

    $ renpy.pause(hard=True)



label buildz:
    $ place[chosenplc]=type
    jump map

label reset1:
    $ place[0]=-1
    jump map

label reset2:
    $ place[1]=-1
    jump map

label reset3:
    $ place[2]=-1
    jump map




User avatar
renardjap
Regular
Posts: 75
Joined: Sun Aug 05, 2018 1:08 pm
Location: France ! Cocorico
Contact:

Re: Why my game doesn't work when I save/load it ?

#7 Post by renardjap »

@ Per K Grok Thamks for your code. It must have taken you a while ! My problem is that's just a small part of my code. In reality, it is larger with a lot of functions like a timer to create and upgrade the buildings, functions to destroy them and rebuild it... And I have a plenty of different buildings (25 buildings at least)... Also, I need python statement for this project. I'm very disappointed because it works pretty well but I can't save it... what a shame.
Is there no other solution ?

User avatar
Per K Grok
Miko-Class Veteran
Posts: 882
Joined: Fri May 18, 2018 1:02 am
Completed: the Ghost Pilot, Sea of Lost Ships, Bubbles and the Pterodactyls, Defenders of Adacan Part 1-3, the Phantom Flyer
itch: per-k-grok
Location: Sverige
Contact:

Re: Why my game doesn't work when I save/load it ?

#8 Post by Per K Grok »

renardjap wrote: Thu Jun 25, 2020 8:58 am @ Per K Grok Thamks for your code. It must have taken you a while ! My problem is that's just a small part of my code. In reality, it is larger with a lot of functions like a timer to create and upgrade the buildings, functions to destroy them and rebuild it... And I have a plenty of different buildings (25 buildings at least)... Also, I need python statement for this project. I'm very disappointed because it works pretty well but I can't save it... what a shame.
Is there no other solution ?
I think there are two fundamental problems you have work around. One is that Ren'py does not save objects. So if you store something in an objects attribute that will not be saved. If you close and reopen the program, it is gone.

https://www.renpy.org/doc/html/save_load_rollback.html

So you want to have stuff that are going to be saved, in a variable. If you have a lot of those you might want to have them in a list.

There are, as far as I know (which is a map with a lot of white on it), not any easy way to change an item in a list, directly in a screen command, so you would need to go to Jump or Call a label, or use a Function.

With a function you have the problem of global and local variables.


That is what it seems to me you have to work with.

nananame
Regular
Posts: 72
Joined: Fri Oct 13, 2017 1:40 pm
Contact:

Re: Why my game doesn't work when I save/load it ?

#9 Post by nananame »

I concur and think that's what Alex was getting at when he said you should redesign your code. It might be a large overhaul but you'll get it working. What you are trying to do is possible but requires a lot of careful work.

ferruginoso
Newbie
Posts: 1
Joined: Sun Jun 28, 2020 6:09 pm
Contact:

Re: Why my game doesn't work when I save/load it ?

#10 Post by ferruginoso »

Try to use retain_after_load in conjunction with the buttons actions. That's what I did with my item system and it works just fine.

User avatar
renardjap
Regular
Posts: 75
Joined: Sun Aug 05, 2018 1:08 pm
Location: France ! Cocorico
Contact:

Re: Why my game doesn't work when I save/load it ?

#11 Post by renardjap »

ferruginoso wrote: Sun Jun 28, 2020 6:18 pm Try to use retain_after_load in conjunction with the buttons actions. That's what I did with my item system and it works just fine.
I already tried but it didn't work...

User avatar
renardjap
Regular
Posts: 75
Joined: Sun Aug 05, 2018 1:08 pm
Location: France ! Cocorico
Contact:

Re: Why my game doesn't work when I save/load it ?

#12 Post by renardjap »

Hi,
Finally I discovered why it doesn't save ! I created a default building_list = [] and a default place_list = [] I declared just before the start label. I replace all the Building.building and Places.places by building_list and place_list and it's work perfectly !

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot]