Page 1 of 1

[Solved] Cycling Through a List of Images in a Folder for Main Menu

Posted: Mon May 17, 2021 3:08 pm
by quinnsea
Hey! I'm finally getting back into working on my game, and I figured that it might be an interesting idea to make it so the background image in my main menu cycles through different backgrounds in the game. I'm struggling a bit with accessing the folder and putting the list in my game. Here's a couple of methods that I tried:

Code: Select all

init python:
    import os
    path = 'images/bgs'
        
    mmBackground = os.listdir(path)

    imageIndex = random.randint(0,len(mmBackground)-1)
    gui.main_menu_background = mmBackground[imageIndex]
    
screen main_menu():
The error here is:

Code: Select all

WindowsError: [Error 3] The system cannot find the path specified: u'images/bgs\\*.*'
So I figured that I just should've used a back slash instead of a forwards slash, but the new error only left me more confused.

Code: Select all

WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect: u'images\x08gs\\*.*'
...So, I tried something else.

Code: Select all

define mmBackgrounds = renpy.list_files('images/bgs')
define imageIndex = renpy.random.randint(0,len(mmBackgrounds)-1)
define gui.main_menu_background = mmBackgrounds[imageIndex]
    
screen main_menu():
Which was a little bit closer, I did actually get to the main menu this time instead of the code aborting right away. However, it seems like it's just listing all the files in the game and not specifically the ones in that directory as I will occasionally get the error:

Code: Select all

Exception: Could not load image u'00compat.rpy': error ('Unsupported image format',)
Not always that specific file, but I am trying to get files only in 'images/bgs'. This game is going to be released in multiple chapters that have to be installed, and I want to make sure new backgrounds still have the potential to load so I'd rather not manually input the whole list of images if I can avoid it.

Any help with whichever method would be appreciated, thank you!

Re: Cycling Through a List of Images in a Folder for Main Menu

Posted: Mon May 17, 2021 3:17 pm
by Syrale
Not sure if it really is that simple, but instead of "images/bgs", you could try "images/bgs/**" or "images/bgs/*.*"?

Re: Cycling Through a List of Images in a Folder for Main Menu

Posted: Mon May 17, 2021 3:27 pm
by quinnsea
Syrale wrote: Mon May 17, 2021 3:17 pm Not sure if it really is that simple, but instead of "images/bgs", you could try "images/bgs/**" or "images/bgs/*.*"?
Aah, if only! ;_; I tried all of those, and it still gives me the error where it can't find the path.

Re: Cycling Through a List of Images in a Folder for Main Menu

Posted: Mon May 17, 2021 3:58 pm
by Ocelot
renpy.list_files() returns a list of all accessible game files, as you noticed. From there you can just filter those you do not need:
mm_backgrounds = list(filter( lambda s: s.startswith('images/bgs/'), renpy.list_files() ))

Re: Cycling Through a List of Images in a Folder for Main Menu

Posted: Mon May 17, 2021 4:03 pm
by quinnsea
Ocelot wrote: Mon May 17, 2021 3:58 pm renpy.list_files() returns a list of all accessible game files, as you noticed. From there you can just filter those you do not need:
mm_backgrounds = list(filter( lambda s: s.startswith('images/bgs/'), renpy.list_files() ))
Oh! I didn't even think about using startswith()! Thanks so much, works perfectly!