dicts and RevertableLists

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
pucedragonlord
Regular
Posts: 159
Joined: Wed May 09, 2012 2:49 am
Projects: The Diviner
Organization: Two Crowns Entertainment
Location: Now: Charlottesville, VA
Contact:

dicts and RevertableLists

#1 Post by pucedragonlord »

To avoid defining unnecessary Object types, I'm using a dict to store some data in my current project, but when I declare the empty ones in an init python block, apparently they're being declared as "revertableList" types!

Code: Select all

init python:
    dict1 = {}
    dict2 = {}

    dict1['testcase'] = 0
    dict1.has_key('testcase')
The above code will return an error, because revertableLists don't have "has_key," apparently. I tried it with dict.get(), too, but got the same error. Is there something special I need to do to declare these as dictionaries? Or if it would be better for RenPy to leave them as revertableLists, how do I use them?
The more you know

Asceai
Eileen-Class Veteran
Posts: 1258
Joined: Fri Sep 21, 2007 7:13 am
Projects: a battle engine
Contact:

Re: dicts and RevertableLists

#2 Post by Asceai »

This code works fine for me. Are you sure it's exactly what you used?
RevertableList does not contain has_key but that's because it's an implementation of list. {} creates a RevertableDict which does have has_key.
Are you using the latest ver of ren'py?

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: dicts and RevertableLists

#3 Post by xela »

Not likely unless you've messed with the parser...

Try something like:

dict1 = dict()

or

dict1 = _dict()

but your error is kinda absurd.
Like what we're doing? Support us at:
Image

pucedragonlord
Regular
Posts: 159
Joined: Wed May 09, 2012 2:49 am
Projects: The Diviner
Organization: Two Crowns Entertainment
Location: Now: Charlottesville, VA
Contact:

Re: dicts and RevertableLists

#4 Post by pucedragonlord »

It is absurd, hence my need to ask.
I get the same error even using dict() or _dict()

The exact code throwing the error is...

Code: Select all

init python:
    import xml.etree.ElementTree as ET
    import math
    import renpy.store as store
    import renpy.exports as renpy
    
    ...

    earnedProgress = _dict()

    def achievement(type, id, steps = 0): # it takes a string type, an int id, and an optional int steps
        
        if "honor" in type.lower() or type.lower() == "moh" or type.lower() == "mofh": #if it's an honor...
            ...
        else: #if it's a Mark of Progress, do the same thing, but replace earnedHonors with earnedProgress
            #first get the achievement data, so we can use it
            findPath = "MoP[@id='" + str(id) + "']"
            print findPath #for debugging
            achInfo = Achievements.find(findPath)
            
            if earnedProgress.has_key(str(id)): #if we've seen this achievement before.. <<<<<THIS IS THE ERROR LINE
                if earnedProgress[str(id)] != 0: #and we don't already have it...
                    earnedProgress[str(id)] += steps #increment it!
                    
                    #now see if we've earned it or not
                    if earnedProgress[str(id)] >= int(archInfo.find("steps").text):
                        print "progress get!"
                        earnedProgress[str(id)] = 0 #we got it, so set it to 0 since we have it now
                        doActions(achInfo) #also run its actions
                        
                else: #if we do have it already, do nothing (expect debug code)
                    print "we already have MoP " + str(id) + "."
                    
            else: #we've never seen this one before. Add it to the dict and run it's actions
                earnedProgress[ str(id) ] = steps
                
                if earnedProgress[ str(id) ] == 0:
                    print "progress get!"
                    doActions(achInfo)
The ElementTree stuff is an XML parser, and doesn't have anything called a revertableList of it's own, so that's not it. Those renpy exports are to allow custom class objects to be saved properly.

I'm using version 6.16.5.525

I'm thoroughly confused.
The more you know

Asceai
Eileen-Class Veteran
Posts: 1258
Joined: Fri Sep 21, 2007 7:13 am
Projects: a battle engine
Contact:

Re: dicts and RevertableLists

#5 Post by Asceai »

We can't do anything with the code you posted, since we can't run it and importing xml.etree.ElementTree doesn't make the dict stop working. (I also coded temporary replacements for the missing class and function called so I could run it and it had no problems, so the problem isn't in this code in the first place)

Simplify and remove things until

a = dict()
a.has_key('x')

works.
The last thing you removed is the culprit.

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: dicts and RevertableLists

#6 Post by xela »

Asceai is right, what you've posted is not responsible.
pucedragonlord wrote:

Code: Select all

init python:
    import renpy.store as store
    import renpy.exports as renpy
Those renpy exports are to allow custom class objects to be saved properly.

I'm thoroughly confused.
They are already imported... you have no idea how redicolous it is to import renpy into renpy, but I suppose it doesn't do anything weird either. Basically it's the same as putting:

Code: Select all

# ABRAKADABRA HOOOAH!
in every file and telling yourself that it brings you luck.

=================================================
My rambling aside:

dict() and {} are about the same but allow different syntax and {} is faster on creation with a large body of information.

_dict() is the original python dictionary.

ReverableList is a RenPy list ([] or list()) that facilitates Rollbacks. _list() is the original python list.

In your code, you reassign your variable to a list somewhere. Anything else that could cause such an error would require incredible effort and know-how on how to break stuff :)

Anywhere in python code, simply try this two lines:

Code: Select all

earnedProgress={}
raise Exception, type(earnedProgress)
and it'll say something like renpy.python.ReverableDict in the error report. Then you'll know that fault lies in your code somewhere. If you absolutely cannot find it, attach your code files and we'll help but first simply try to do an ALL SEARCH in your text editor for earnedProgress. You have something like earnedProgress=[], earnedProgress=list(), earnedProgress = earnedProgress.keys() or earnedProgress.values() somewhere or earnedProgress=<something that returns a list>. Or something along the same lines.


PS:

Just for the record:

Code: Select all

if str(id) in earnedProgress:
is better Python than:

Code: Select all

if earnedProgress.has_key(str(id)):
but there is no chance that this will fix your error :(
Like what we're doing? Support us at:
Image

Asceai
Eileen-Class Veteran
Posts: 1258
Joined: Fri Sep 21, 2007 7:13 am
Projects: a battle engine
Contact:

Re: dicts and RevertableLists

#7 Post by Asceai »

xela wrote:PS:

Just for the record:

Code: Select all

if str(id) in earnedProgress:
is better Python than:

Code: Select all

if earnedProgress.has_key(str(id)):
but there is no chance that this will fix your error :(
Actually, 'in' works for lists. It's probably not very efficient, but it works. So this might actually fix it - more likely, it'll stop the error from appearing and cause problems elsewhere because earnedProgress still isn't a dict.

pucedragonlord
Regular
Posts: 159
Joined: Wed May 09, 2012 2:49 am
Projects: The Diviner
Organization: Two Crowns Entertainment
Location: Now: Charlottesville, VA
Contact:

Re: dicts and RevertableLists

#8 Post by pucedragonlord »

xela wrote:
Just for the record:

Code: Select all

if str(id) in earnedProgress:
is better Python than:

Code: Select all

if earnedProgress.has_key(str(id)):
but there is no chance that this will fix your error :(
That's good to know, actually. I'll remember that.

I'd always wondered about the import renpy.exports as renpy thing. I forget if it was in the wiki or I picked it up in the forums, but somehow that line showed up when I was first learning how to make custom objects work in RenPy and I've always included it since, just to be safe.

I'll run through it a few more times. There has to be something simple in there that's causing the problem.
The more you know

Asceai
Eileen-Class Veteran
Posts: 1258
Joined: Fri Sep 21, 2007 7:13 am
Projects: a battle engine
Contact:

Re: dicts and RevertableLists

#9 Post by Asceai »

For laughs I tried

Code: Select all

from renpy.python import RevertableList as dict
This causes things to break much, much earlier than any code I have working with dicts. So that's not it, I guess =P

User avatar
PyTom
Ren'Py Creator
Posts: 16096
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: dicts and RevertableLists

#10 Post by PyTom »

When you open the console (shift+O) and enter in "dict", what does it display?
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

pucedragonlord
Regular
Posts: 159
Joined: Wed May 09, 2012 2:49 am
Projects: The Diviner
Organization: Two Crowns Entertainment
Location: Now: Charlottesville, VA
Contact:

Re: dicts and RevertableLists

#11 Post by pucedragonlord »

"renpy.Python.RevertableDict"

The weirdest part is that I use dicts elsewhere in the code and they don't seem to have exploded, yet. At this point I'm probably just going to re-write the whole function, since it is rather unwieldy as it is now. Still have no idea where this error is coming from, though. I checked the type after declaring the variable and it came out as a dict, but trying to call "print" or "raise" within the function spat out an error saying "object not callable" pointing to the debug line.

This has been a strange string of errors, that's for sure.
The more you know

Post Reply

Who is online

Users browsing this forum: Google [Bot]