Renaming label Dicts and undefined crash

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
orian34
Newbie
Posts: 14
Joined: Tue Aug 24, 2021 3:42 am
Github: orian34
itch: orian34
Discord: orian34
Contact:

Renaming label Dicts and undefined crash

#1 Post by orian34 »

I've been trying to set up a system that displays the music and scene names, and figured out that the best way would be to use a dict (found some answers by searching through the forums) and managed to make it work.
Now there's still some issues, namely that it will crash if a track or label is not added to the dicts, since it's undefined to the dict. I tried searching but I'm not sure how I could make it simply ignore the outcome if the dict doesn't have the value presented.
I had to manually add a variable into the labels to have the dict even accept working, label_callback acted weirdly.

Code: Select all

screen display_list():
	if read_current_alt > 0:
            text Labels[read_current_alt] xpos 0.06 ypos 0.84
        else:
            text "No Scene" xpos 0.06 ypos 0.84
        $ playing = renpy.music.get_playing("music")
        if playing is not None:
            $ playing = Playlist[playing]
            text playing xpos 0.06 ypos 0.9
        else:
            text "No Music Playing" xpos 0.06 ypos 0.9

default Labels = {
                1: "Start",
                2: "Opening credits",
                3: "Locked Door",
                4: "Breaking Down",
                5: "No Lie"
                }

default Playlist = {audio.family_01: "The Beginning",
                    audio.turnaround: "Awakening",
                    audio.goodbye_01: "Day By Day",
                    audio.goodbye_02: "No More Demons"
                    }
                    
label game:
	$ read_current_alt = 1
        call S1
        $ read_current_alt = 2
        call S2
        $ read_current_alt = 3
        call S3
        return
Where S1-3 are the individual scenes, each with their own label. Now, I know I'm not very smart, so there might be a better way to do it, but I couldn't prevent it crashing from trying to parse undefined labels/musics, and trying to use the label_callback name value straight up didn't work in a dict.

I would greatly appreciate any help/advice regarding the situation. It currently works, but it's unstable and dirty makeshift.

User avatar
emz911
Regular
Posts: 103
Joined: Fri Jun 23, 2017 2:23 pm
Contact:

Re: Renaming label Dicts and undefined crash

#2 Post by emz911 »

If I understood the question correctly, you can probably try replacing the condition with checking if the value exists in the dict, "if label_callback in Labels:"

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

Re: Renaming label Dicts and undefined crash

#3 Post by Ocelot »

You can also use default values for dict access:

Code: Select all

text Labels.get(read_current_alt, "Unknown Label") xpos 0.06 ypos 0.84
# . . .
$ playing = Playlist.get(playing, "Unknown Track")
< < insert Rick Cook quote here > >

User avatar
orian34
Newbie
Posts: 14
Joined: Tue Aug 24, 2021 3:42 am
Github: orian34
itch: orian34
Discord: orian34
Contact:

Re: Renaming label Dicts and undefined crash

#4 Post by orian34 »

Ocelot wrote: Wed Mar 23, 2022 5:35 pm You can also use default values for dict access:

Code: Select all

text Labels.get(read_current_alt, "Unknown Label") xpos 0.06 ypos 0.84
# . . .
$ playing = Playlist.get(playing, "Unknown Track")
Thanks a lot!
I didn't know it was possible, guess I missed that part...

I also figured out why callback wasn't working: it's because I had to filter out menu labels. After doing so it works fine and I could remove the read_current_alt completely!

now looks like (for people wondering how to do it too)

Code: Select all

screen display_list():
	if current_scene is not None:
            text Labels.get(current_scene, "Unknown Scene") xpos 0.06 ypos 0.84
        else:
            text "No Scene" xpos 0.06 ypos 0.84
        $ playing = renpy.music.get_playing("music")
        if playing is not None:
            text Playlist.get(playing, "Unknown Track") xpos 0.06 ypos 0.9
        else:
            text "No Music Playing" xpos 0.06 ypos 0.9
            
init python:
    def label_callback(name, abnormal):
        if name.startswith("_") or "textbox" in name: return
        store.current_scene = name

    config.label_callback = label_callback

default Labels = {
                S1: "Start",
                S2: "Opening credits",
                S3: "Locked Door",
                S4: "Breaking Down",
                S5: "No Lie"
                }

default Playlist = {audio.family_01: "The Beginning",
                    audio.turnaround: "Awakening",
                    audio.goodbye_01: "Day By Day",
                    audio.goodbye_02: "No More Demons"
                    }
                    
label game:
        call S1
        call S2
        call S3
        call S4
        call S5
        return

Post Reply

Who is online

Users browsing this forum: Google [Bot]