Page 1 of 1

Renaming label Dicts and undefined crash

Posted: Wed Mar 23, 2022 12:17 pm
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.

Re: Renaming label Dicts and undefined crash

Posted: Wed Mar 23, 2022 2:55 pm
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:"

Re: Renaming label Dicts and undefined crash

Posted: Wed Mar 23, 2022 5:35 pm
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")

Re: Renaming label Dicts and undefined crash

Posted: Thu Mar 24, 2022 10:02 am
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