How to make defaultdict work with rollback [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
GigaChonker9000
Newbie
Posts: 2
Joined: Tue May 05, 2020 10:59 pm
Contact:

How to make defaultdict work with rollback [SOLVED]

#1 Post by GigaChonker9000 »

Here's a short game script that shows my problem:

Code: Select all

init python:
    from collections import defaultdict

default test_dict = defaultdict(int)

label start:
    "Test A"
    $ test_dict["key"] += 1
    $ num = test_dict["key"]
    "Current value: [num]"
    "Test B"
Between the lines "Test A" and "Test B", I want to increment the value of "key" by 1. However, when I rollback from "Test B" to "Test A", instead of reverting the value back to 0, it increments it again -- now the game is at "Test A" but the value of "key" is 2. You can do this repeatedly to make the value as large as you want.

This issue doesn't happen if I use a regular dictionary (replacing defaultdict(int) with {"key" : 0}); the current value stays at 1, as it should.

I'm not an expert with programming, but I assume it has to do with the fact that the value of the key isn't explicitly defined at the point we're trying to rollback to, it just uses the default value of 0. Is there any good way to work around this? If necessary I can switch to a regular dictionary and set all of the keys and values explicitly at the beginning, but a defaultdict would be a lot cleaner if possible.
Last edited by GigaChonker9000 on Wed May 06, 2020 12:27 am, 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: How to make defaultdict work with rollback

#2 Post by hell_oh_world »

GigaChonker9000 wrote: Tue May 05, 2020 11:35 pm Here's a short game script that shows my problem:

Code: Select all

init python:
    from collections import defaultdict

default test_dict = defaultdict(int)

label start:
    "Test A"
    $ test_dict["key"] += 1
    $ num = test_dict["key"]
    "Current value: [num]"
    "Test B"
Between the lines "Test A" and "Test B", I want to increment the value of "key" by 1. However, when I rollback from "Test B" to "Test A", instead of reverting the value back to 0, it increments it again -- now the game is at "Test A" but the value of "key" is 2. You can do this repeatedly to make the value as large as you want.

This issue doesn't happen if I use a regular dictionary (replacing defaultdict(int) with {"key" : 0}); the current value stays at 1, as it should.

I'm not an expert with programming, but I assume it has to do with the fact that the value of the key isn't explicitly defined at the point we're trying to rollback to, it just uses the default value of 0. Is there any good way to work around this? If necessary I can switch to a regular dictionary and set all of the keys and values explicitly at the beginning, but a defaultdict would be a lot cleaner if possible.
Renpy dicts are a custom class called RevertableDict. I guess you could just inherit in that class.

Code: Select all

init python:
    from collections import defaultdict
    
    class defdict(defaultdict, renpy.python.RevertableDict):
        pass
        
default test_dict = defdict(int)

label start:
    "Test A"
    $ test_dict["key"] += 1
    $ num = test_dict["key"]
    "Current value: [num]"
    "Test B"

GigaChonker9000
Newbie
Posts: 2
Joined: Tue May 05, 2020 10:59 pm
Contact:

Re: How to make defaultdict work with rollback

#3 Post by GigaChonker9000 »

That works, thanks!

henvu50
Veteran
Posts: 337
Joined: Wed Aug 22, 2018 1:22 am
Contact:

Re: How to make defaultdict work with rollback

#4 Post by henvu50 »

I tried to do this with OrderedDict, but it doesn't work with rollback, but the method suggested above does indeed work with defaultdict.

Anyone know if it's possible to get OrderedDict to work with Rollback/RevertableDict? If not, it's not a big deal.

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot]