[SOLVED]Problem With Save/Load

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
Lucyper
Newbie
Posts: 9
Joined: Sat Jun 17, 2023 8:07 pm
Projects: The Mad Scientist
Deviantart: Lucyper4u
itch: https://lucyp-r.itch
Contact:

[SOLVED]Problem With Save/Load

#1 Post by Lucyper »

Renpy doesn't save data after closing the game and loading a save, I'm trying to build a Clothing System based in viewtopic.php?t=25579#p313665 and viewtopic.php?t=30643.

That renpy cannot save objects I already know about, but all possible solutions are not working and I have no idea how to make the code below work properly. :evil:

Basically it's a code with some pre-defined elements for me to have a basis for how I'm going to build a clothing system:

renpy.storage.object which in theory was supposed to save the data, is not saving. (at least I imagine that's it :? )

Although while the game is open, if I load the 3 items, save, go to the main menu, and load the save, the 3 items will be there, but when I close the game, open it again and load the save, the items are gone - first problem

I read about Rollback can cause problems like this in addition to being duplicated every time the label start is read again, using the renpy.block_rollback() command solves this problem but the others do not. - second "problem"

The last problem is in relation to the duplication of items every time the label that inserts them is read, I thought of a temporary solution that they are only added when a variable X is false
(I also thought about writing a function inside the classes but it would be additional work since I didn't solve the previous problems)

Code: Select all

init -2 python:
    import renpy.store as store
    import renpy.exports as renpy

    class Player(renpy.store.object):
        def __init__(self, name):
            self.name=name
            self.inv=[]

        def gain(self,Item):
            self.inv.append([Item])
            return
    
    class Item(renpy.store.object):
            def __init__(self, name, image):
                self.name=name
                self.image=image
init -1 python:
    
    box_1 = Item("box","box.png") 
    triangle_1= Item("triangle","triangle.png")
    circle_1 = Item("circle","circle.png")

init :
    $ eil = Player("Eil")
    define n = Character(eil.name)
    define n = "Narrador"
    define e = "Eileen"

label start:
    
    scene bg room
    
    show eileen happy

    n "Eileen inventory has: Box, Triangle and Circle"
    $ eil.gain(box_1)
    $ eil.gain(triangle_1)
    $ eil.gain(circle_1)
    $ renpy.block_rollback()
    jump inv
    return
label inv:
    call screen inventario
screen inventario:
    zorder 1
    vbox:
        hbox:
            textbutton "{color=FFFFFF}Close:{/color}":
                action [Hide ("inventario"), Call ("start")]
            for item in eil.inv:
                $ pic = item[0].image
                $ hover = im.Sepia(pic)
                imagebutton:
                    idle Composite((100,100), (0,0), pic)
                    hover Composite((100,100), (0,0), pic)
                    action NullAction()
        
I really don't know what to do... so any help I will be eternally grateful. :)

ps :English is not my native language. :cry:
Last edited by Lucyper on Sun Jun 18, 2023 12:46 pm, edited 1 time in total.

User avatar
_ticlock_
Miko-Class Veteran
Posts: 910
Joined: Mon Oct 26, 2020 5:41 pm
Contact:

Re: Problem With Save/Load

#2 Post by _ticlock_ »

Lucyper wrote: Sat Jun 17, 2023 8:39 pm

Code: Select all

init :
    $ eil = Player("Eil")
        
I really don't know what to do... so any help I will be eternally grateful. :)
In your case, eil is treated as a constant. You need to use default statement for variables.

Check documentation for default statements and define statements.

Code: Select all

define n = Character("Eil")
default eil = Player(n.name)
PS: define statement are performed at init phase, default statement performed at the start of the game. Thus, you can't use eil in the define statement. But you can use n.name for eil

Lucyper
Newbie
Posts: 9
Joined: Sat Jun 17, 2023 8:07 pm
Projects: The Mad Scientist
Deviantart: Lucyper4u
itch: https://lucyp-r.itch
Contact:

Re: Problem With Save/Load

#3 Post by Lucyper »

_ticlock_ wrote: Sat Jun 17, 2023 11:26 pm
Lucyper wrote: Sat Jun 17, 2023 8:39 pm

Code: Select all

init :
    $ eil = Player("Eil")
        
I really don't know what to do... so any help I will be eternally grateful. :)
In your case, eil is treated as a constant. You need to use default statement for variables.

Check documentation for default statements and define statements.

Code: Select all

define n = Character("Eil")
default eil = Player(n.name)
PS: define statement are performed at init phase, default statement performed at the start of the game. Thus, you can't use eil in the define statement. But you can use n.name for eil
First I would like to thank you for the answer, calmly analyzing the other codes and making some changes I managed to solve the problem of not keeping the data and the Rollback adding more and more objects to the list:

The new code working properly:

Code: Select all

init -2 python:
    import renpy.store as store
    import renpy.exports as renpy

    class Player(renpy.store.object):
        def __init__(self, name):
            self.name=name
            self.inv=[]
    
        def gain(self,Item):
            self.inv.append([Item])
            return

    eil = Player("Eil")

    class Item(renpy.store.object):
            def __init__(self, name, image):
                self.name=name
                self.image=image
    
init :
    define n = Character(eil.name)
    define n = "Narrador"
    define e = "Eileen"

label start:
    
    scene bg room
    
    show eileen happy
    python:
        eil = Player("Eil")
        box = Item("box",image="box.png") 
        triangle = Item("triangle",image="triangle.png")
        circle = Item("circle",image="circle.png")
        eil.gain(box)
        eil.gain(triangle)
        eil.gain(circle)

    n "Eileen inventory has: Box, Triangle and Circle"
    
    jump inv
    return
label inv:
    call screen inventario
screen inventario:
    zorder 1
    vbox:
        hbox:
            textbutton "{color=FFFFFF}Close:{/color}":
                action [Hide ("inventario"), Call ("start")]
            for item in eil.inv:
                $ pic = item[0].image
                $ hover = im.Sepia(pic)
                imagebutton:
                    idle Composite((100,100), (0,0), pic)
                    hover Composite((100,100), (0,0), pic)
                    action NullAction()
But now in my game, I get the error, TypeError: 'Char' object is not iterable :?

jeffster
Veteran
Posts: 447
Joined: Wed Feb 03, 2021 9:55 pm
Contact:

Re: Problem With Save/Load

#4 Post by jeffster »

Lucyper wrote: Sun Jun 18, 2023 10:14 am

Code: Select all

init :
    define n = Character(eil.name)
    define n = "Narrador"
Don't put define in init block.
Don't define the same name twice.
Reporting errors, show the traceback message (where exactly the exception happened, with which instruction & variable).
If the problem is solved, please edit the original post and add [SOLVED] to the title. 8)

Lucyper
Newbie
Posts: 9
Joined: Sat Jun 17, 2023 8:07 pm
Projects: The Mad Scientist
Deviantart: Lucyper4u
itch: https://lucyp-r.itch
Contact:

Re: Problem With Save/Load

#5 Post by Lucyper »

jeffster wrote: Sun Jun 18, 2023 10:50 am
Lucyper wrote: Sun Jun 18, 2023 10:14 am

Code: Select all

init :
    define n = Character(eil.name)
    define n = "Narrador"
Don't put define in init block.
Don't define the same name twice.
Reporting errors, show the traceback message (where exactly the exception happened, with which instruction & variable).
So, if I run the above code, no error occurs, rollback does not multiply the items, pressing the Close button that returns to the start label also does not multiply the items, and the saves keep the data

And about my game, I forgot to specify the self.inv in Imagebutton

but, what happens if I use define in Init block?

User avatar
_ticlock_
Miko-Class Veteran
Posts: 910
Joined: Mon Oct 26, 2020 5:41 pm
Contact:

Re: Problem With Save/Load

#6 Post by _ticlock_ »

Lucyper wrote: Sun Jun 18, 2023 10:14 am
Let me suggest the following:

Code: Select all

init -2 python:
    class Player():
        def __init__(self, name):
            self.name=name
            self.inv=[]
    
        def gain(self,Item):
            self.inv.append(Item) #Why not add just item

    class Item():
        def __init__(self, name, image):
            self.name=name
            self.image=image
    
define n = Character("Eil")
default eil = Player(n.name)
default box = Item("box",image="box.png") 
default triangle = Item("triangle",image="triangle.png")
default circle = Item("circle",image="circle.png")

label start:
    python:
        eil.gain(box)
        eil.gain(triangle)
        eil.gain(circle)

    n "Eileen inventory has: Box, Triangle and Circle"
    
    call inv
    n "You closed the inventory”
    return # End of game
    
label inv:
    call screen inventario
    return
    
screen inventario:
    zorder 1
    vbox:
        hbox:
            textbutton "{color=FFFFFF}Close:{/color}":
                action Return()
            for item in eil.inv:
                imagebutton:
                    idle item.image
                    hover Transform(item.image, matrixcolor= SepiaMatrix())
                    action NullAction()
PS: You don’t need to specify renpy.store.object if you are in rpy file.

jeffster
Veteran
Posts: 447
Joined: Wed Feb 03, 2021 9:55 pm
Contact:

Re: Problem With Save/Load

#7 Post by jeffster »

Lucyper wrote: Sun Jun 18, 2023 11:20 am but, what happens if I use define in Init block?
Nobody knows.

That's the problem.

When you do things a wrong way, sometimes there can be no visible effect. And then one day... BANG!
If the problem is solved, please edit the original post and add [SOLVED] to the title. 8)

Lucyper
Newbie
Posts: 9
Joined: Sat Jun 17, 2023 8:07 pm
Projects: The Mad Scientist
Deviantart: Lucyper4u
itch: https://lucyp-r.itch
Contact:

Re: Problem With Save/Load

#8 Post by Lucyper »

jeffster wrote: Sun Jun 18, 2023 7:38 pm
Lucyper wrote: Sun Jun 18, 2023 11:20 am but, what happens if I use define in Init block?
Nobody knows.

That's the problem.

When you do things a wrong way, sometimes there can be no visible effect. And then one day... BANG!
Thanks for the advise, I already changed the code and put everything out from init block, I was following the code from someone to build one myself, at least I finally understand how to build a inv system :D

Lucyper
Newbie
Posts: 9
Joined: Sat Jun 17, 2023 8:07 pm
Projects: The Mad Scientist
Deviantart: Lucyper4u
itch: https://lucyp-r.itch
Contact:

Re: Problem With Save/Load

#9 Post by Lucyper »

_ticlock_ wrote: Sun Jun 18, 2023 4:03 pm
Lucyper wrote: Sun Jun 18, 2023 10:14 am
Let me suggest the following:

Code: Select all

init -2 python:
    class Player():
        def __init__(self, name):
            self.name=name
            self.inv=[]
    
        def gain(self,Item):
            self.inv.append(Item) #Why not add just item

    class Item():
        def __init__(self, name, image):
            self.name=name
            self.image=image
    
define n = Character("Eil")
default eil = Player(n.name)
default box = Item("box",image="box.png") 
default triangle = Item("triangle",image="triangle.png")
default circle = Item("circle",image="circle.png")

label start:
    python:
        eil.gain(box)
        eil.gain(triangle)
        eil.gain(circle)

    n "Eileen inventory has: Box, Triangle and Circle"
    
    call inv
    n "You closed the inventory”
    return # End of game
    
label inv:
    call screen inventario
    return
    
screen inventario:
    zorder 1
    vbox:
        hbox:
            textbutton "{color=FFFFFF}Close:{/color}":
                action Return()
            for item in eil.inv:
                imagebutton:
                    idle item.image
                    hover Transform(item.image, matrixcolor= SepiaMatrix())
                    action NullAction()
PS: You don’t need to specify renpy.store.object if you are in rpy file.
OKAY and thx for the answer!

jeffster
Veteran
Posts: 447
Joined: Wed Feb 03, 2021 9:55 pm
Contact:

Re: Problem With Save/Load

#10 Post by jeffster »

Lucyper wrote: Sun Jun 18, 2023 9:01 pm Thanks for the advise, I already changed the code and put everything out from init block, I was following the code from someone to build one myself, at least I finally understand how to build a inv system :D
Superb! Be especially wary when documentation says "then behavior is undefined". It can mean that everything works until some software update or some combination of conditions happen... When it suddenly breaks big time with a little chance to figure out where to look for errors.
Ren'Py only seems to be a simple piece of software. In fact it integrates CPython caching, Python and CPython modules, SDL, HLSL, Cubism, pygame and whatnot...
If the problem is solved, please edit the original post and add [SOLVED] to the title. 8)

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2420
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Problem With Save/Load

#11 Post by Ocelot »

Lucyper wrote: Sun Jun 18, 2023 11:20 am but, what happens if I use define in Init block?
Absolutely nothing. Define imlicitely introduces init block for itself. In modern RenPy there is no need for init block: all entities you might want to use it for are already executed at init time.
< < insert Rick Cook quote here > >

jeffster
Veteran
Posts: 447
Joined: Wed Feb 03, 2021 9:55 pm
Contact:

Re: Problem With Save/Load

#12 Post by jeffster »

Ocelot wrote: Mon Jun 19, 2023 12:50 am
Lucyper wrote: Sun Jun 18, 2023 11:20 am but, what happens if I use define in Init block?
Absolutely nothing. Define imlicitely introduces init block for itself. In modern RenPy there is no need for init block: all entities you might want to use it for are already executed at init time.
Maybe you are right in this case, but developing new versions is always based on some assumptions about how that software will be used (especially when talking about extendable programmed engines). If a user doesn't conform with those assumptions, or even if they just fail to work with some previous parts of code (creating so called "regressions"), then we don't know what would happen. It would only depend on particular realization of future versions. So if it works today doesn't mean it will tomorrow. Even if it will, it's better to program in reliable, clean ways.
If the problem is solved, please edit the original post and add [SOLVED] to the title. 8)

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot], Majestic-12 [Bot]