is there an easy way of keeping the class attrebutes after reload?

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
Kia
Miko-Class Veteran
Posts: 887
Joined: Fri Aug 01, 2014 7:49 am
Deviantart: KiaAzad
Skype: KiaAzad
Contact:

is there an easy way of keeping the class attrebutes after reload?

#1 Post by Kia » Fri Sep 27, 2019 5:07 am

I'm revamping one of my old codes for gallery and before writing some hack to store locked/unlocked state in persistent variables, I want to ask if there's a more elegant way of retaining class attributes after restarts?
here's my gallery images class:

Code: Select all

init python:
    class gallpic:
        def __init__(self, name, img, locked = True, icon = None, info = "", align = (0.5, 1.0)):
            self.name = name
            self.img = img
            self.icon = icon
            self.info = info
            self.locked = locked
            self.align = align
and after defining the images:

Code: Select all

define gall_bg = gallpic(
    name = "1- Background",
    img = "demo/bg.jpg",
    locked = False,
    icon = "gall/bg.jpg",
    info = "A simple background.",
    align = (0.5, 1.0)
    )
they are stored in a list to loop through but it seems that they are re-defined every time the game is launched, I've tried defining them in a label, as default, inheriting from _object lass and check for instance name and only define them if it doesn't exist. but nothing seems to work. am I missing something obvious again?

drKlauz
Regular
Posts: 168
Joined: Mon Oct 12, 2015 3:04 pm
Contact:

Re: is there an easy way of keeping the class attrebutes after reload?

#2 Post by drKlauz » Fri Sep 27, 2019 11:08 am

Try inheriting your classes from this or similar BaseClass:

Code: Select all

init -1000 python:
  class BaseClass(renpy.python.RevertableObject):
    def __getstate__(self):
      return vars(self).copy()
    def __setstate__(self,new_dict):
      self.__dict__.update(new_dict)
I really don't know why it isn't default base class in RenPy...

User avatar
Kia
Miko-Class Veteran
Posts: 887
Joined: Fri Aug 01, 2014 7:49 am
Deviantart: KiaAzad
Skype: KiaAzad
Contact:

Re: is there an easy way of keeping the class attrebutes after reload?

#3 Post by Kia » Sat Sep 28, 2019 3:21 am

drKlauz wrote:
Fri Sep 27, 2019 11:08 am
Try inheriting your classes from this or similar BaseClass:
I tried it and got the same result as before, did I miss a step somewhere?

User avatar
Remix
Eileen-Class Veteran
Posts: 1338
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: is there an easy way of keeping the class attrebutes after reload?

#4 Post by Remix » Sat Sep 28, 2019 5:37 am

You should use default rather than define

default var = value
# set a default which is only ran if the variable does not exist in the store

define var = value
# set an immutable value (a value we do not expect to change) which is ran at each game init. As it is not expected to change, Ren'Py does not automatically add it to the store for saving (after all, why store it if it never changes?)
Mad Scientist Todo List:
  • Finish Improved Event Handler
  • Implement DragonBones animation as a Creator Defined Container
  • Develop Cartoon Speech Bubble dialogue
  • Finish Bitmask collision and rebound vector system
  • Develop time based building mechanic
  • Others
  • *Find a superb artist and actually write a game*

drKlauz
Regular
Posts: 168
Joined: Mon Oct 12, 2015 3:04 pm
Contact:

Re: is there an easy way of keeping the class attrebutes after reload?

#5 Post by drKlauz » Sat Sep 28, 2019 6:34 am

Your problem is this gall_bg not saving/loading properly?
- inherit gallpic class from BaseClass (class GallPic(BaseClass):...)
- use default or create gall_bg after start (default gall_bg=GallPic(name=...) )
If it still doesn't work, then problem might be somewhere else. In this case create minimal project what will show error, just script.rpy with all needed code.

User avatar
Kia
Miko-Class Veteran
Posts: 887
Joined: Fri Aug 01, 2014 7:49 am
Deviantart: KiaAzad
Skype: KiaAzad
Contact:

Re: is there an easy way of keeping the class attrebutes after reload?

#6 Post by Kia » Sat Sep 28, 2019 10:59 am

the problem with default is: these variables keep their value after the start label, once the game returns to the main menu, their value will be reset to their default value. which is not exactly useful for a gallery.
I've attached the whole gallery, it will look a bit out of alignment without the rest of 0gui but it should function enough for what we want to test.
Attachments
gall.zip
(2.7 MiB) Downloaded 2 times

User avatar
trooper6
Lemma-Class Veteran
Posts: 3631
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: is there an easy way of keeping the class attrebutes after reload?

#7 Post by trooper6 » Sat Sep 28, 2019 11:07 am

If you want the data to remain between games, then it needs to be a persistent variable.
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

User avatar
Kia
Miko-Class Veteran
Posts: 887
Joined: Fri Aug 01, 2014 7:49 am
Deviantart: KiaAzad
Skype: KiaAzad
Contact:

Re: is there an easy way of keeping the class attrebutes after reload?

#8 Post by Kia » Sat Sep 28, 2019 11:48 am

trooper6 wrote:
Sat Sep 28, 2019 11:07 am
If you want the data to remain between games, then it needs to be a persistent variable.
I can think of few hacks involving persistent variables, but I'm hoping to find a cleaner solution.

User avatar
rayminator
Veteran
Posts: 306
Joined: Fri Feb 09, 2018 12:05 am
Projects: Redoing games to fix spelling mistakes other companys that don't care
Location: Canada
Contact:

Re: is there an easy way of keeping the class attrebutes after reload?

#9 Post by rayminator » Sat Sep 28, 2019 2:08 pm

have you tried using this as well

Code: Select all

$ renpy.retain_after_load()

drKlauz
Regular
Posts: 168
Joined: Mon Oct 12, 2015 3:04 pm
Contact:

Re: is there an easy way of keeping the class attrebutes after reload?

#10 Post by drKlauz » Sat Sep 28, 2019 2:24 pm

Ok, i looked source file and re-read thread.
You have to separate state of gallery entry from information how does it look.
Locked attribute get reset when game is over, and rightly so as whole game get restarted.
I would replace locked attribute with locked property what will read entry state from persistents and when manually changed will update persistent.

Code: Select all

  def __init__(self,name,img,**kwargs):
    self.name=name
    self.img=img
    self.icon=kwargs.get('icon',None)
    self.info=kwargs.get('info',None)
    self.align=kwargs.get('align',(0.5,1.0))
    if not kwargs.get('locked',True):
      self.locked=False
  @property
  def locked(self):
    return not renpy.seen_image(self.img)
  @locked.setter
  def locked(self,locked):
    name=self.img
    if not isinstance(name,tuple):
      name=tuple(name.split())
    if locked:
      renpy.game.persistent._seen_images.pop(name,None)
    else:
      renpy.game.persistent._seen_images[name]=True
If you want to check if label was visited, use renpy.seen_label and renpy.game.persistent._seen_ever.

Something like. (Code not tested, but should give idea what i mean) With this approach you could remove BaseClass as gallpic class will not participate in save/load/rollback.
What is 'unclean' with using persistents btw?

P.S.: Unless i really misunderstood your intentions.

User avatar
Kia
Miko-Class Veteran
Posts: 887
Joined: Fri Aug 01, 2014 7:49 am
Deviantart: KiaAzad
Skype: KiaAzad
Contact:

Re: is there an easy way of keeping the class attrebutes after reload?

#11 Post by Kia » Sat Sep 28, 2019 4:24 pm

drKlauz wrote:
Sat Sep 28, 2019 2:24 pm
What is 'unclean' with using persistents btw?
using persistent variables is the old way I handled this, however, I prefer a simpler solution that gives me the flexibility to not only change the locked property, the other properties as well.
it would be great to have the option to change an images name or description depending on the events in the game, maybe I want to add CG variations later on and the option to add new variations directly to an object would become very handy in that case.

drKlauz
Regular
Posts: 168
Joined: Mon Oct 12, 2015 3:04 pm
Contact:

Re: is there an easy way of keeping the class attrebutes after reload?

#12 Post by drKlauz » Sat Sep 28, 2019 7:49 pm

Turn name/info into properties as well, i guess. Inside property check locked/unlocked persistent variables and show wanted content. It is really game-specific thing, so can't give generic advice.

User avatar
Kia
Miko-Class Veteran
Posts: 887
Joined: Fri Aug 01, 2014 7:49 am
Deviantart: KiaAzad
Skype: KiaAzad
Contact:

Re: is there an easy way of keeping the class attrebutes after reload?

#13 Post by Kia » Sun Sep 29, 2019 12:58 am

drKlauz wrote:
Sat Sep 28, 2019 7:49 pm
Turn name/info into properties as well, i guess. Inside property check locked/unlocked persistent variables and show wanted content. It is really game-specific thing, so can't give generic advice.
that would be more persistent variables that I want to deal with, the whole point of using classes for something like gallery is ease of use, otherwise I can simply put everything in a persistent list and call it a day.
I wouldn't want to define every image as persistent variable either, nobody wants to type persistent a thousand times in one game just to get the gallery going.

I want my code to be easily understandable for the uses, something like:

Code: Select all

    if g.lst[g.current].locked:
        vbox:
            text "⚷" size 200 color "#d00"
            text "Locked"
    else:
        add g.lst[g.current].img align g.lst[g.current].align
    frame:
        align(.5,0.0)
        text "{} - {}".format(g.lst[g.current].name, g.lst[g.current].info)
makes much more sense than:

Code: Select all

    if persistent.gallery_1[g.current].[0]:
        vbox:
            text "⚷" size 200 color "#d00"
            text "Locked"
    else:
        add persistent.gallery_1[g.current].[1] align persistent.gallery_1[g.current].[2]
    frame:
        align(.5,0.0)
        text "{} - {}".format(persistent.gallery_1[g.current].[3], persistent.gallery_1[g.current].[4])
finding an image's index in this list just to change something of it is another can of worms I don't want subject people to.

User avatar
PyTom
Ren'Py Creator
Posts: 15444
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: is there an easy way of keeping the class attrebutes after reload?

#14 Post by PyTom » Sun Sep 29, 2019 1:38 am

The one thing you could do would be to stick your entire gallery class into the persistent data. You can do that if you define the class itself in a python early block. Note that you'll have to be very careful about adding things to the instance of the class, since the instance loaded from the persistent data won't have such newly added attributes.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
"Silly and fun things are important." - Elon Musk
Software > Drama • https://www.patreon.com/renpytom

User avatar
Kia
Miko-Class Veteran
Posts: 887
Joined: Fri Aug 01, 2014 7:49 am
Deviantart: KiaAzad
Skype: KiaAzad
Contact:

Re: is there an easy way of keeping the class attrebutes after reload?

#15 Post by Kia » Sun Sep 29, 2019 2:06 am

PyTom wrote:
Sun Sep 29, 2019 1:38 am
The one thing you could do would be to stick your entire gallery class into the persistent data.
it worth to try, but I might need an example of how to, since I'm not that familiar with inner workings of renpy

Post Reply

Who is online

Users browsing this forum: Ace94, Bing [Bot]