[SOLVED] Clearing a class?

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
gummii_exe
Newbie
Posts: 13
Joined: Fri Jun 10, 2022 11:42 am
Contact:

[SOLVED] Clearing a class?

#1 Post by gummii_exe »

So in my code, I have a character list class that essentially states which characters have been met. However, this list seems to be persistent, without any persistent value- So I'm wondering if there is a way to either define a method to clear the class upon starting the game fresh, or some way to simply remove that persistence.
This is my code for the class:

Code: Select all

class CharaList(store.object):
        def __init__(self):
            self.chara_list = []

        def metChara(self, chara):
            self.chara_list.append(chara)

        def removeChara(self, chara):
            self.chara_list.remove(chara)

        def hasChara(self, chara):
            if chara in self.chara_list:
                return True
            else:
                return False

define metCharacters = CharaList()
It relies on a seperate class I have defined for Characters, which is as follows:

Code: Select all

class Chara(store.object):
        def __init__(self, name, pronouns, description, meet = False, affection = 0):
         #imageName = "",
            self.name = name
            self.pronouns = pronouns
            self.description = description
            #self.imageName = "characters/"+ imageName + ".png"
            self.meet = meet
            self.affection = affection
If any other code is needed to help don't hesitate to let me know! Thank you!
Last edited by gummii_exe on Sun Jun 12, 2022 11:46 am, edited 2 times in total.

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

Re: Clearing a class?

#2 Post by Ocelot »

No special code nessesary, RenPy handles it just fine as long as variables are defined properly.

Code: Select all

define metCharacters = CharaList()
This is example of variable that would not be reset to its original value, because there is not need: after all, you promised that it would not change anyway.
Documentation wrote: Variables that are defined using the define statement are treated as constant, are not saved or loaded, and should not be changed. (Ren'Py does not enforce this, but will produce undefined behavior when this is not the case.)
You want to declare mutable variables using default:

Code: Select all

default metCharacters = CharaList()
< < insert Rick Cook quote here > >

gummii_exe
Newbie
Posts: 13
Joined: Fri Jun 10, 2022 11:42 am
Contact:

Re: Clearing a class?

#3 Post by gummii_exe »

I have never seen it explained that way but that clears it up so well- thank you! I was wondering what the difference between default and define was- I probably should have looked into that in the first place lol. Thank you!!

Post Reply

Who is online

Users browsing this forum: Ocelot