[SOLVED] PY class definition in RPY and rollback

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
BukaP
Newbie
Posts: 3
Joined: Sun Aug 01, 2021 1:26 pm
Contact:

[SOLVED] PY class definition in RPY and rollback

#1 Post by BukaP »

Hello!

I added python class "Test2" in "Test2.py" and defined it through "default" in "script.rpy". If I use rollback "Test2.nValue" behaves strange. In first position ("rollback 3") for "Test2" it equal 10 not 0. If I use "addValue" with rollback ("rollback 1") call this function change "Test2.nValue". Please tell me where is my mistake?

Thank you very in advance!

script.rpy

Code: Select all

init python:
    import Test2

    class Test():
        def __init__(self, nValue = 0):
            self.nValue = nValue

        def setValue(self, nValue):
            self.nValue = nValue

        def getValue(self):
            return self.nValue

        def addValue(self, nValue):
            self.nValue += nValue

default test = Test()
default test2 = Test2.Test2()

label start:
    $ sText = u"Test.getValue = {0}\nTest2.getValue = {1}".format(test.getValue(), test2.getValue())
    "[sText]"

    menu:
        "setValue(10)":
            $ test.setValue(10)
            $ test2.setValue(10)

        "setValue(20)":
            $ test.setValue(20)
            $ test2.setValue(20)

        "addValue(1)":
            $ test.addValue(1)
            $ test2.addValue(1)

        "next":
            jump start
    jump start

Test2.py

Code: Select all

class Test2():
    def __init__(self):
        self.nValue = 0

    def setValue(self, nValue):
        self.nValue = nValue

    def getValue(self):
        return self.nValue

    def addValue(self, nValue):
        self.nValue += nValue
Example with setValue.
RenpyTestWork1.png
Example with addValue.
RenpyTestWork2.png
Last edited by BukaP on Sun Aug 01, 2021 3:47 pm, edited 1 time in total.

User avatar
hell_oh_world
Miko-Class Veteran
Posts: 777
Joined: Fri Jul 12, 2019 5:21 am
Contact:

Re: PY class definition in RPY and rollback

#2 Post by hell_oh_world »

https://www.renpy.org/doc/html/python.h ... d-packages
Warning

Python defined in .rpy files is transformed to allow rollback to work. Python imported from .py files is not. As a result, objects created in Python will not work with rollback, and probably should not be changed after creation.

BukaP
Newbie
Posts: 3
Joined: Sun Aug 01, 2021 1:26 pm
Contact:

Re: PY class definition in RPY and rollback

#3 Post by BukaP »

Thank you for the answer. If I'll disable rollback "define config.rollback enabled = False" can I use changeable python's classes wich defined in the *.py files? Will this cause others problems? As I tested, "new game", "save" and "load" work normally.
Maybe I'm wrong. Have I no reason to use "* .py" files instead of "*.rpy" files?

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

Re: PY class definition in RPY and rollback

#4 Post by Ocelot »

BukaP wrote: Sun Aug 01, 2021 3:26 pm Thank you for the answer. If I'll disable rollback "define config.rollback enabled = False" can I use changeable python's classes wich defined in the *.py files? Will this cause others problems? As I tested, "new game", "save" and "load" work normally.
Maybe I'm wrong. Have I no reason to use "* .py" files instead of "*.rpy" files?
Loading uses rollback internally to return to the beginning of current statement, which might cause problems if player decides to save at wrong moment.

Generally you do not need to use .py files for game-related code. You can just can write it in separate rpy file and have it autoload. Alternatively: embrace functional programming and write immutable classes. External .py files are fine when you do not change instances of classes defined there.
< < insert Rick Cook quote here > >

BukaP
Newbie
Posts: 3
Joined: Sun Aug 01, 2021 1:26 pm
Contact:

Re: PY class definition in RPY and rollback

#5 Post by BukaP »

Thank you very much. I understood.

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Ocelot