Achievement code : KeyError

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
haruray
Newbie
Posts: 2
Joined: Sun Jul 01, 2018 1:25 am
Contact:

Achievement code : KeyError

#1 Post by haruray » Sun Jul 01, 2018 2:06 am

i just read the achievement code in this topic : viewtopic.php?t=39890
and then, i used this code

Code: Select all

transform achievement_transform:
    on show:
        xalign .98 
        yalign -.3 
        linear 0.4 xalign .98 yalign .02
    on hide:
        linear 0.4 xalign 1.9 yalign .02

screen scr_achievement_get(title, a_text, icon, trans=achievement_transform):
    timer 2.4 action Hide("scr_achievement_get")
    window:
        at trans
        background "#333333cc"
        xalign .98
        yalign .02
        xysize (450, 100)
        hbox:
            vbox:
                spacing 10
                image icon
            vbox:
                xoffset 10
                xsize 330
                text title:
                    size 28
                    id title
                text a_text:
                    size 22
                    id a_text

screen scr_achievement_update(title, a_text, icon, cur_prog, max_prog, trans=achievement_transform):
    timer 2.4 action Hide("scr_achievement_update")
    window:
        at trans
        background "#333333cc"
        xalign .98
        yalign .02
        xysize (450, 100)

        #

        hbox:
            vbox:
                spacing 10
                image icon
                text "{0}/{1}".format(cur_prog, max_prog):
                    xcenter 0.5 
                    ycenter 0.2
            vbox:
                xoffset 10
                xsize 330
                text title:
                    size 28
                    id title
                text a_text:
                    size 22
                    id a_text


                



init python:
    def get_achievement(ach_id, trans=achievement_transform):
        ach = persistent.achievements_dict[ach_id]
        achievement.grant(ach_id)
        renpy.show_screen(_screen_name='scr_achievement_get', title=ach['title'],
                          a_text=ach['text'], icon=ach['icon'], trans=trans)

    def update_achievement(ach_id, to_add=1, trans=achievement_transform):
        persistent.achievements_dict[ach_id]["cur_prog"] += to_add
        ach = persistent.achievements_dict[ach_id]

        achievement.progress(ach_id, to_add)
        if ach['cur_prog'] > ach['max_prog']:
            persistent.achievements_dict[ach_id]["cur_prog"] = ach['max_prog']
            ach = persistent.achievements_dict[ach_id]

        renpy.show_screen(_screen_name='scr_achievement_update', title=ach['title'], a_text=ach['text'],
                          icon=ach['icon'], cur_prog=ach['cur_prog'], max_prog=ach['max_prog'], trans=trans)





    # Define your achievements here
    if not persistent.achievements_dict:
        persistent.achievements_dict = {"name": {"type": 0, # One time achievent
                                                             "title": "Un coeur tendre", # Also neame for steam
                                                             "text": "Relation romantique avec Azur débloquée !", # description
                                                             "icon": "images/Logo Achievement/Coeur.png" # 96x96 image
                                                             },
                                        "name2": {"type": 1, # Progress achievent
                                                             "title": "Le mystère du Chateau",
                                                             "text": "Qui es-ce qui est à l'origine de ça ??",
                                                             "icon": "images/Logo Achievement/Coeur.png",
                                                             "cur_prog": 0, # current progress 
                                                             "max_prog": 33 # maximal progress
                                                             }
                                        }
                                        

        for i, a in persistent.achievements_dict.items():
            if a['type'] == 0:
                achievement.register(i, steam=a['title'])
            if a['type'] == 1:
                achievement.register(i, steam=a['title'], stat_max=a['max_prog'])
in this block, :

Code: Select all

if not persistent.achievements_dict:
        persistent.achievements_dict = {"name": {"type": 0, # One time achievent
                                                             "title": "Un coeur tendre", # Also neame for steam
                                                             "text": "Relation romantique avec Azur débloquée !", # description
                                                             "icon": "images/Logo Achievement/Coeur.png" # 96x96 image
                                                             },
                                        "name2": {"type": 1, # Progress achievent
                                                             "title": "Le mystère du Chateau",
                                                             "text": "Qui es-ce qui est à l'origine de ça ??",
                                                             "icon": "images/Logo Achievement/Coeur.png",
                                                             "cur_prog": 0, # current progress 
                                                             "max_prog": 33 # maximal progress
                                                             }
                                        }
i tried to change the "name" into "name3", so it looked like this

Code: Select all

    if not persistent.achievements_dict:
        persistent.achievements_dict = {"name3": {"type": 0, # One time achievent
                                                             "title": "Un coeur tendre", # Also neame for steam
                                                             "text": "Relation romantique avec Azur débloquée !", # description
                                                             "icon": "images/Logo Achievement/Coeur.png" # 96x96 image
                                                             },
                                        "name2": {"type": 1, # Progress achievent
                                                             "title": "Le mystère du Chateau",
                                                             "text": "Qui es-ce qui est à l'origine de ça ??",
                                                             "icon": "images/Logo Achievement/Coeur.png",
                                                             "cur_prog": 0, # current progress 
                                                             "max_prog": 33 # maximal progress
                                                             }
                                        }
when i run it, i got an error :
I'm sorry, but an uncaught exception occurred.

While running game code:
File "game/script.rpy", line 138, in script
$ get_achievement("name3", trans=achievement_transform) ######## in the script, there wasn't " ", i add it.
File "game/script.rpy", line 138, in <module>
$ get_achievement("name3", trans=achievement_transform) ######## in the script, there wasn't " ", i add it.
File "game/script.rpy", line 82, in get_achievement
ach = persistent.achievements_dict[ach_id]
KeyError: u'name3'

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

Full traceback:
File "game/script.rpy", line 138, in script
$ get_achievement("name3", trans=achievement_transform) ######## in the script, there wasn't " ", i add it.
File "E:\Renpy\renpy-6.99.13-sdk\renpy\ast.py", line 862, in execute
renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
File "E:\Renpy\renpy-6.99.13-sdk\renpy\python.py", line 1888, in py_exec_bytecode
exec bytecode in globals, locals
File "game/script.rpy", line 138, in <module>
$ get_achievement("name3", trans=achievement_transform) ######## in the script, there wasn't " ", i add it.
File "game/script.rpy", line 82, in get_achievement
ach = persistent.achievements_dict[ach_id]
KeyError: u'name3'

BUT, if i leave the "name" and "name2" alone, i can use it and i got no error.
if i add a new achievement with different name, i got an error.
so...any suggestion? it will be helpful. Thanks

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: Achievement code : KeyError

#2 Post by kivik » Mon Jul 02, 2018 11:56 am

Try deleting persistent data and run the code again.

Your code checks if the entire persistent dictionary exists or not: and if it does, even without name3, it's not touched - so if the dictionary was already created (if you ran the code before modifying it before) - then the persistent data doesn't get updated.

Deleting the persistent data will cause the initialising code to get executed and should fix your issue.

haruray
Newbie
Posts: 2
Joined: Sun Jul 01, 2018 1:25 am
Contact:

Re: Achievement code : KeyError

#3 Post by haruray » Tue Jul 03, 2018 2:28 am

kivik wrote:
Mon Jul 02, 2018 11:56 am
Try deleting persistent data and run the code again.

Your code checks if the entire persistent dictionary exists or not: and if it does, even without name3, it's not touched - so if the dictionary was already created (if you ran the code before modifying it before) - then the persistent data doesn't get updated.

Deleting the persistent data will cause the initialising code to get executed and should fix your issue.
Umm, im sorry, but can you give the example? I can't understand anything because im new :D
by the way, thanks for replying!

Post Reply

Who is online

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