How to protect object variables when rollbacking ? [SOLVED]

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
Dawn
Newbie
Posts: 14
Joined: Sun Apr 30, 2017 3:49 am
Projects: Nephilim — Revelation
Contact:

How to protect object variables when rollbacking ? [SOLVED]

#1 Post by Dawn »

Rollback on my novel worked fine when I was using global variables. But I'd rather use some object. And now I encountered some difficulties when player is rollbacking...

Here is my new code:

Code: Select all

init python :
    inventory = Inventory()
label start:
    e "You have %(inventory.money)s $"  #You have 50$
    $ inventory.earn(5)
   e "You earn 5 $"
   e "You have %(inventory.money)s $"  #You have 55$
It works fine. But if players use the rollback feature, he earn 5$ for each rollback...

So his money is growing 55, 60, 65, 70$, etc.

When I was using global variables, I didn't have this kind of problem.

Here is the inventory class source code:

Code: Select all

    class Inventory(store.object):
    ##############################
        def __init__(self, money = 50):
            self.money = money
            self.items = []
            
        ## Player earn money
        def earn(self, money): # Player earn money
            self.money += money
What have I to code to remove money when rollbacking?

PS : I read this topic, so I tried to add the below code, but it changed nothing:

Code: Select all

    import renpy.store as store
    import renpy.exports as renpy # we need this so Ren'Py properly handles rollback with classes
Last edited by Dawn on Sat Jun 17, 2017 5:23 am, edited 1 time in total.

User avatar
Dawn
Newbie
Posts: 14
Joined: Sun Apr 30, 2017 3:49 am
Projects: Nephilim — Revelation
Contact:

Re: How to protect object variables when rollbacking ?

#2 Post by Dawn »

After a full rebuild, I found the solution!
inventory have to be declared after the "label start" and not in a "init python"

Code: Select all

label start:
    $ inventory = Inventory()
    e "You have %(inventory.money)s $"  #You have 50$
    $ inventory.earn(5)
    e "You earn 5 $"
    e "You have %(inventory.money)s $"  #You have 55$

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

Re: How to protect object variables when rollbacking ?

#3 Post by trooper6 »

Actually, current best practices, especially for saving and rollback is to declare the objects outside of an init block, before the start label, using default. (Also, you should use renpy interpolation).

So:

Code: Select all

default inventory = Inventory()

label start:
    e "You have [intentory.money]$"  #You have 50$
    $ inventory.earn(5)
    e "You earn 5 $"
    e ""You have [intentory.money]$"  #You have 55$
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
Dawn
Newbie
Posts: 14
Joined: Sun Apr 30, 2017 3:49 am
Projects: Nephilim — Revelation
Contact:

Re: How to protect object variables when rollbacking ?

#4 Post by Dawn »

Thank you very much! With interpolation you saved me a lot of time. Without your help, I will have problem with translation! Thanks for these two tips!

edit : I do a mistake. I tried your solution with define (instead of default). It doesn't work ! When I rollback money is not removed from inventory instance. But it works with default !

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot]