Page 1 of 1

[SOLVED] PY class definition in RPY and rollback

Posted: Sun Aug 01, 2021 2:14 pm
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

Re: PY class definition in RPY and rollback

Posted: Sun Aug 01, 2021 2:49 pm
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.

Re: PY class definition in RPY and rollback

Posted: Sun Aug 01, 2021 3:26 pm
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?

Re: PY class definition in RPY and rollback

Posted: Sun Aug 01, 2021 3:37 pm
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.

Re: PY class definition in RPY and rollback

Posted: Sun Aug 01, 2021 3:44 pm
by BukaP
Thank you very much. I understood.