Reloading the game resets my python Classes
Posted: Wed Mar 06, 2024 12:49 pm
Hello everybody.
I am new to this forum. That is mainly because in the past I didn't encounter a problem I wasn't able to solve by myself. I am writing my first visual novel with renpy, but not the first program.
I am encountering a problem lately and I was able to figure out why this happens, but not how I get around it.
The problem is: Renpy resets my classes to initialization values when I reload the game. In most cases this is not too bad. But there is one case where it completely destroys my gameflow.
First off I started with a handful of characters and had each stat of the characters in a single variable. But as I intruduced more characters to my game it quickly got out of hand and so I decided to create a character-class named Person.
It looks like this:
I have defined the characters at first (maybe this wouldn't be totally neccessary, but did it anyway...):
And I initialized them in the start-label:
So far so good. The gameplay goes on and girl after girl get activated like:
Later in the game the girls get points like:
Their stats are shown in a stats screen. Girl after girl are shown in the stats screen when the become active and I can see their love, lust, upkeep and location in the screen.
But what bothers me is: Each time I hit Shift+R all girls loose their stats and go back to .active = False.
I know, that is because of renpy loading everything until it hits the end of the start-label and first it sets the girls to None (because of the default) and then it sets them to the init-values of the Person-class. I found out by testing if initializing them in the next label (right after the start-label) would lead me to a better solution and all the girls variables were set to None.
So my question is:
Has anyone an idea how to preserve the values inside the classes-variables while reloading?
Best regards
Lacha
I am new to this forum. That is mainly because in the past I didn't encounter a problem I wasn't able to solve by myself. I am writing my first visual novel with renpy, but not the first program.
I am encountering a problem lately and I was able to figure out why this happens, but not how I get around it.
The problem is: Renpy resets my classes to initialization values when I reload the game. In most cases this is not too bad. But there is one case where it completely destroys my gameflow.
First off I started with a handful of characters and had each stat of the characters in a single variable. But as I intruduced more characters to my game it quickly got out of hand and so I decided to create a character-class named Person.
It looks like this:
Code: Select all
init -1 python:
characters = []
class Person:
def __init__(self, varname, name, age, maxlove, maxlust, upkeep):
global characters
self.varname = varname
self.name = name
self.age = age
self.upkeep = upkeep
self.love = 0
self.lust = 0
self.maxlove = maxlove
self.maxlust = maxlust
self.active = False
self.tapsinname = name.replace("th", "t").replace("Th", "T").replace("TH","T")
self.location = "nowhere"
self.state = 1
if not self in characters:
characters.append(self)
if self.name[-1] == "s":
self.refname = self.name
else:
self.refname = name+"s"
self.ref = self
def activate(self):
self.active = True
shuffleOccupancy()
def pointsadd(self, stat, points, waypoint):
if points >= 0:
prefix = "+"
else:
prefix = ""
if waypoint in waypoints:
if waypoints[waypoint]['stat'] == "lust":
waypoints[waypoint]['char'].lust += waypoints[waypoint]['points']*(0-1)
if waypoints[waypoint]['stat'] == "love":
waypoints[waypoint]['char'].love += waypoints[waypoint]['points']*(0-1)
waypoints.pop(waypoint)
else:
waypoints[waypoint] = {"stat": stat, "points": points, "char": self.ref}
output = self.refname + " " + stat + " " + prefix + str(points)
if stat == "lust":
self.lust += points
if stat == "love":
self.love += points
renpy.transition(dissolve)
renpy.show_screen("PointsNotify", output)
renpy.pause(3.0)
renpy.transition(dissolve)
renpy.hide_screen("PointsNotify")
Code: Select all
init -1:
default cira = None
default tapsin = None
default vici = None
default elyria = None
default maeve = None
default ida = None
default miriam = None
Code: Select all
python:
if cira is None:
cira = Person("cira", "Kira", 26, 100, 100, 3)
if tapsin is None:
tapsin = Person("tapsin","Tapsin", 19, 100, 100, 2)
if vici is None:
vici = Person("vici","Vici", 28, 100, 100, 0)
if elyria is None:
elyria = Person("elyria","Elyria", 280, 100, 100, 5)
if miriam is None:
miriam = Person("miriam","Miriam",22, 100, 100, 7)
if maeve is None:
maeve = Person("maeve","Maeve",25, 100, 100, 9)
if ida is None:
ida = Person("ida","Ida",25, 100, 100, 10)
Code: Select all
$ cira.activate()
Code: Select all
$ cira.pointsadd("love", 1,"16952d1e-9dd3-4d93-9413-8c65d823b60f")
But what bothers me is: Each time I hit Shift+R all girls loose their stats and go back to .active = False.
I know, that is because of renpy loading everything until it hits the end of the start-label and first it sets the girls to None (because of the default) and then it sets them to the init-values of the Person-class. I found out by testing if initializing them in the next label (right after the start-label) would lead me to a better solution and all the girls variables were set to None.
So my question is:
Has anyone an idea how to preserve the values inside the classes-variables while reloading?
Best regards
Lacha