RevertableObject, RevertableDict and etc documentation

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
rivvil
Regular
Posts: 26
Joined: Fri Feb 20, 2015 3:05 pm
Location: Russia
Contact:

RevertableObject, RevertableDict and etc documentation

#1 Post by rivvil » Fri Feb 20, 2015 4:34 pm

Hello. I'm a newbie Ren'Py programmer and just discovered that my Python classes should inherit renpy.store.object to be able to rollback properly. Then I searched Ren'Py sources and found RevertableObject, RevertableDict and other similar classes.
So, is there any documentation about these classes? Or maybe some guru can explain when and how they should be used? What public aliases do they have?
For instance, I assume that RevertableDict should be used instead of standard Python dict to enable rollback functionality for this kind of objects. Am I right or not?

User avatar
PyTom
Ren'Py Creator
Posts: 15893
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: RevertableObject, RevertableDict and etc documentation

#2 Post by PyTom » Sat Feb 21, 2015 12:58 am

The answer is yes, but Ren'Py does this for you automatically, when you're in a Ren'Py (.rpy, rather than .py) context. For example, in .rpy code,

Code: Select all

{ "foo" : "bar", "bar" : "baz", "baz" : "foo" }
creates a RevertableDict, rather than a dict. Similarly, in the Ren'Py context, object is bound to RevertableObject. So unless you plan on writing python code in a .py file, there's no need to use the names in renpy.python directly. If you are using .py files, you should inherit from (and create) renpy.store.object, renpy.store.list, renpy.store.dict, and renpy.store.set.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
"Silly and fun things are important." - Elon Musk
Software > Drama • https://www.patreon.com/renpytom

rivvil
Regular
Posts: 26
Joined: Fri Feb 20, 2015 3:05 pm
Location: Russia
Contact:

Re: RevertableObject, RevertableDict and etc documentation

#3 Post by rivvil » Sat Feb 21, 2015 1:39 am

PyTom, thank you for the answer.
I was about to refactor major portion of my code in a rollback sake but these implicit bindings saved my day :) It's a very nice feature I think.

User avatar
caryoscelus
Newbie
Posts: 19
Joined: Wed Sep 10, 2014 4:46 pm
IRC Nick: caryoscelus
Github: caryoscelus
Location: Earth
Contact:

Re: RevertableObject, RevertableDict and etc documentation

#4 Post by caryoscelus » Wed May 20, 2015 7:04 am

So basically everything in minstore.py should be imported in .py files (that would add 'sorted' and 'range' to the list)? And does this also mean that using something beyond those (even from standard library, e.g. collections) may break rollback or saving?

For now i'm using following code for Ren'Py/pure python compatibility. (This still requires to manually write "class A(object):" and literals like {} will always use builtin python..)

Code: Select all

try:
    from renpy.store import object, list, dict, set, range, sorted
    HAS_RENPY = True
except ImportError:
    HAS_RENPY = False

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: RevertableObject, RevertableDict and etc documentation

#5 Post by trooper6 » Wed May 20, 2015 9:07 am

About inheriting from object, should it be A:

Code: Select all

class Item(object)
or

Code: Select all

class Item(renpy.store.object)
what's the difference between the two? Is there a difference?
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
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: RevertableObject, RevertableDict and etc documentation

#6 Post by xela » Wed May 20, 2015 9:53 am

If you are in .py file, object will be referenced to the original Python's object. Ren'Pys object (which I believe facilitates rollback and allows setting attributes directly) can be found at renpy.store.object (assuming you've imported it to the module).

In .rpy file, there is absolutely no difference between the two. Original Python's object is referenced at _object on global namespace.

*Edit: About an year ago a friend wrote some python code for my game and it failed to load after putting it into .rpy file because Ren'Py was using older sets that lacked one or two methods. That was the only time code written for Python 2.7 failed to load in Ren'Py at face value, maybe that is now fixed. There is virtually no difference that I am aware of otherwise... other than that you can do:

Code: Select all

obj = object()
obj.attr = "Meow"
with Ren'Pys object but not with pythons one (should throw an error) and the rollback thing. I usually use original stuff since my game doesn't require rollback.
Like what we're doing? Support us at:
Image

User avatar
caryoscelus
Newbie
Posts: 19
Joined: Wed Sep 10, 2014 4:46 pm
IRC Nick: caryoscelus
Github: caryoscelus
Location: Earth
Contact:

Re: RevertableObject, RevertableDict and etc documentation

#7 Post by caryoscelus » Wed May 20, 2015 11:18 am

trooper6 wrote:About inheriting from object, should it be A:

Code: Select all

class Item(object)
or

Code: Select all

class Item(renpy.store.object)
what's the difference between the two? Is there a difference?
Well, renpy.store.object is only available when the code is run in Ren'Py. In .rpy it's available by default and object is pointing to it as well. In .py it should be imported manually, so i'm using the code above to use the same code with and without Ren'Py. So there isn't much meaning in writing Item(renpy.store.object) unless you want to have both Ren'py and simple objects.

User avatar
PyTom
Ren'Py Creator
Posts: 15893
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: RevertableObject, RevertableDict and etc documentation

#8 Post by PyTom » Thu May 21, 2015 1:06 am

Rollback won't work right with code in .py files.

When compling .rpy files, Ren'Py rewrites the python code in a number of ways. For example, the code.

Code: Select all

a = [ 1, 2, 3, 4 ]
becomes

Code: Select all

a = _revertable_list([1, 2, 3, 4])
(I'm going from memory, and the actual transformation takes place on the python AST.)

You can create immutable objects in python code, but if you want rollback to work right, put your code in an init python block. There's only so much magic to go around.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
"Silly and fun things are important." - Elon Musk
Software > Drama • https://www.patreon.com/renpytom

User avatar
caryoscelus
Newbie
Posts: 19
Joined: Wed Sep 10, 2014 4:46 pm
IRC Nick: caryoscelus
Github: caryoscelus
Location: Earth
Contact:

Re: RevertableObject, RevertableDict and etc documentation

#9 Post by caryoscelus » Sat May 23, 2015 5:10 am

Rollback won't work right with code in .py files.
Hmm, maybe i wasn't quite clear. I meant not using objects created in .py directly, but creating objects in .rpy from classes in .py. Rollback does seem to work as long as all classes are subclasses of renpy.store.* classes.

Unfortunately trick with multiple inheriting (renpy.store.object and some "plain python" class) doesn't work, so any library classes would fail to rollback.

User avatar
PyTom
Ren'Py Creator
Posts: 15893
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: RevertableObject, RevertableDict and etc documentation

#10 Post by PyTom » Sat May 23, 2015 7:56 am

I'm pretty clear what I mean. The rollback system has a lot of code transformations in it, and these are required to make it work. You can't expect it to work for arbitrary Python code, at least not without reading and understanding renpy.python.WrapNode.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
"Silly and fun things are important." - Elon Musk
Software > Drama • https://www.patreon.com/renpytom

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot]