Ren'Py How do I change a variable permanently?

Black Ram oss

Black Ram
Game Developer
May 10, 2018
582
1,548
hi, I created this type of variable:
Python:
    ## Friendship
    class Friendship():
        def __init__(self, friendship):
            self.friendship = friendship
        def changeFriendship(self, amt):
            self.friendship += amt
            if amt > 0:
                notify(increase_friendship_notify)
            else:
                notify(decrease_friendship_notify)
            if self.friendship < -100:
                self.friendship = -100
            elif self.friendship > 100:
                self.friendship = 100
but defining it like this:
Python:
define john_friendship = Friendship(50)
changing the value with:
Python:
$ john_friendship .changeFriendship(4)
saving by closing the game and loading the save, the variable returns to the pre-set value 50.
moreover, if I go backwards at the point where the value increases, the value continues to increase.
Could you help me?
 

Max Craving

Member
Game Developer
Oct 14, 2017
409
228
How are they used?
I once had a problem to keep one variable trough some functions, but this variable got reset by default. Global variables solved the problem. You can google it, I am sure there is an answer to your problem, if you ask Google correctly
 

Epadder

Programmer
Game Developer
Oct 25, 2016
568
1,061
No don't need to use global variables for any reason...

If you need to change a saved variable in python you need to prefix it with 'store', if you only need to look at the value you don't need this prefix.

The first obvious problem is using 'define', define is not meant for things that need to be saved.
Default is used for variables that you mean to change and save that result.

Read Here For more information/discussion:
https://f95zone.to/threads/newbie-guide-to-basic-variables-in-renpy.22709/

The other big problem is your design philosophy in general here, is sort of... strange.

You probably have no reason to make a class for each stat you want your characters to have, instead you should make a generic class for your characters that hold all the stats they have and instead write the methods(functions) that alter that stat and manage it from going under/over the limit.

Also to make sure your class participates in Ren'py's rollback should inherit from renpy.python.RevertableObject.

Like:
Python:
class CharacterStats(renpy.python.RevertableObject):
I'd also recommend to read this guide:
https://f95zone.to/threads/how-to-variables-and-save-compatibility.16011/
 

KiaAzad

Member
Feb 27, 2019
276
207
in case you want to keep variables through multiple playthroughs, look into persistent variables.