[SOLVED] Dict breaking after rollback or reload

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
Saturn_Aeon
Newbie
Posts: 2
Joined: Tue Aug 27, 2024 6:40 pm
Contact:

[SOLVED] Dict breaking after rollback or reload

#1 Post by Saturn_Aeon »

Heyy!! First question ever, so forgive me for any confusion! I'm having some trouble with a dict used to see which character has the most points, and making it a variable that i use as a name in a few rare instances to avoid constantly using if statements. It works fine, but whenever i rollback or try to reload my project, it gives me this error

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "renpy/common/00start.rpy", line 216, in script call
    call _load_reload_game from _call__load_reload_game_1
  File "renpy/common/00keymap.rpy", line 551, in script
    python hide:
  File "renpy/common/00keymap.rpy", line 551, in <module>
    python hide:
  File "renpy/common/00keymap.rpy", line 562, in _execute_python_hide
    renpy.load(renpy.session["_reload_slot"])
  File "game/scripts/options.rpy", line 123, in execute_default
    default preferences.text_cps = 50
  File "renpy/common/000namespaces.rpy", line 90, in set_default
    if not isinstance(persistent._preference_default, dict):
TypeError: isinstance() arg 2 must be a type or tuple of types

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "renpy/common/00start.rpy", line 216, in script call
    call _load_reload_game from _call__load_reload_game_1
  File "renpy/common/00keymap.rpy", line 551, in script
    python hide:
  File "C:\Users\REDACTED\Desktop\ㅤ\ARQ\renpy-8.2.1-sdk\renpy\ast.py", line 821, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  File "C:\Users\REDACTED\Desktop\ㅤ\ARQ\renpy-8.2.1-sdk\renpy\python.py", line 1178, in py_exec_bytecode
    exec(bytecode, globals, locals)
  File "renpy/common/00keymap.rpy", line 551, in <module>
    python hide:
  File "renpy/common/00keymap.rpy", line 562, in _execute_python_hide
    renpy.load(renpy.session["_reload_slot"])
  File "C:\Users\REDACTED\Desktop\ㅤ\ARQ\renpy-8.2.1-sdk\renpy\loadsave.py", line 829, in load
    log.unfreeze(roots, label="_after_load")
  File "C:\Users\REDACTED\Desktop\ㅤ\ARQ\renpy-8.2.1-sdk\renpy\rollback.py", line 1135, in unfreeze
    self.rollback(0, force=True, label=label, greedy=greedy, on_load=True)
  File "C:\Users\REDACTED\Desktop\ㅤ\ARQ\renpy-8.2.1-sdk\renpy\rollback.py", line 1031, in rollback
    renpy.exports.execute_default_statement(False)
  File "C:\Users\REDACTED\Desktop\ㅤ\ARQ\renpy-8.2.1-sdk\renpy\exports\statementexports.py", line 372, in execute_default_statement
    i.execute_default(start)
  File "game/scripts/options.rpy", line 123, in execute_default
    default preferences.text_cps = 50
  File "renpy/common/000namespaces.rpy", line 90, in set_default
    if not isinstance(persistent._preference_default, dict):
TypeError: isinstance() arg 2 must be a type or tuple of types
the dict in question is this (might look a lil wonky srry):

Code: Select all

label day_2_end(intra_transition=True):

    jump calc_fav

    label calc_fav:

        python:
            dict = {"char 1": 1_romance, 
                    "char 2": 2_romance, 
                    "char 3": 3_romance, 
                    "char 4": 4_romance, 
                    "char 5": 5_romance, 
                    "char 6": 6_romance}
        
            mydate = max(dict, key=dict.get)

        jump day_2end_diag

    label day_2end_diag:
could the error be caused by the

Code: Select all

mydate = max(dict, key=dict.get)
?
Last edited by Saturn_Aeon on Wed Sep 04, 2024 5:27 pm, edited 1 time in total.

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

Re: Dict breaking after rollback or reload

#2 Post by Ocelot »

< < insert Rick Cook quote here > >

User avatar
PyTom
Ren'Py Creator
Posts: 16121
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: Dict breaking after rollback or reload

#3 Post by PyTom »

Reassigning dict is a really bad idea.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

User avatar
m_from_space
Eileen-Class Veteran
Posts: 1136
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: Dict breaking after rollback or reload

#4 Post by m_from_space »

Saturn_Aeon wrote: Mon Sep 02, 2024 11:47 pm could the error be caused by the

Code: Select all

mydate = max(dict, key=dict.get)
?
The other answers are a bit unspecific, I don't know if you realize what you are doing.

But the problem is, that you are using the variable name "dict", which is a reserved Python/Renpy keyword, it's actually a function that returns a dictionary. If you overwrite "dict" by some arbitrary value, this will cause a lot of problems.

Code: Select all

# BAD!
dict = {"char 1": romance_1}

# GOOD!
mydict = {"char 1": romance_1}

# GOOD!
mydict = dict("char 1" = romance_1)
/edit
Changed variable names, see next post.
Last edited by m_from_space on Wed Sep 04, 2024 8:11 am, edited 1 time in total.

User avatar
jeffster
Miko-Class Veteran
Posts: 751
Joined: Wed Feb 03, 2021 9:55 pm
Contact:

Re: Dict breaking after rollback or reload

#5 Post by jeffster »

m_from_space wrote: Wed Sep 04, 2024 5:04 am

Code: Select all

# GOOD!
mydict = {"char 1": 1_romance}

# GOOD!
mydict = dict("char 1" = 1_romance)
By the way, Python identifiers (like variable or function names) shouldn't start with digits.

PS. Ren'Py too:
https://renpy.org/doc/html/language_bas ... statements

Therefore

Code: Select all

# BAD:
1_romance

# OK:
one_romance
p1_romance
_1_romance     # Though "_..." are reserved for Ren'Py internal use
romance_1
romance[1]
If the problem is solved, please edit the original post and add [SOLVED] to the title. 8)

User avatar
m_from_space
Eileen-Class Veteran
Posts: 1136
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: Dict breaking after rollback or reload

#6 Post by m_from_space »

jeffster wrote: Wed Sep 04, 2024 7:18 am By the way, Python identifiers (like variable or function names) shouldn't start with digits.
Good find, I wasn't even aware of this here, since it should have thrown an exception in the first place.

User avatar
Saturn_Aeon
Newbie
Posts: 2
Joined: Tue Aug 27, 2024 6:40 pm
Contact:

Re: Dict breaking after rollback or reload

#7 Post by Saturn_Aeon »

m_from_space wrote: Wed Sep 04, 2024 5:04 am
But the problem is, that you are using the variable name "dict", which is a reserved Python/Renpy keyword, it's actually a function that returns a dictionary. If you overwrite "dict" by some arbitrary value, this will cause a lot of problems.
[/quote]


This was exactly the problem i was having, i imagined it was something that should be reused (i feel a little silly now of getting this wrong lol). I did not, in fact, know what i was doing 😔

Post Reply

Who is online

Users browsing this forum: No registered users