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

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
quinnsea
Regular
Posts: 51
Joined: Wed Apr 26, 2017 5:04 pm
Completed: Birch Sapling
Projects: Aspen, Baleen
Organization: Koimarina Games
IRC Nick: quinnsea
Soundcloud: nishiikigoi
itch: koimarina
Location: Missouri
Contact:

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

#1 Post by quinnsea » Mon May 17, 2021 3:08 pm

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!
Last edited by quinnsea on Mon May 17, 2021 4:12 pm, edited 1 time in total.
ImageImage

quinn / 23 / game design student
icon by church

User avatar
Syrale
Regular
Posts: 58
Joined: Sun Oct 25, 2015 10:28 am
Completed: Robot Daycare, Deep Sea Valentine, Locke(d)
Projects: Artificial Selection, rei_carnation
itch: kigyo
Contact:

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

#2 Post by Syrale » 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/*.*"?

User avatar
quinnsea
Regular
Posts: 51
Joined: Wed Apr 26, 2017 5:04 pm
Completed: Birch Sapling
Projects: Aspen, Baleen
Organization: Koimarina Games
IRC Nick: quinnsea
Soundcloud: nishiikigoi
itch: koimarina
Location: Missouri
Contact:

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

#3 Post by quinnsea » Mon May 17, 2021 3:27 pm

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.
ImageImage

quinn / 23 / game design student
icon by church

User avatar
Ocelot
Eileen-Class Veteran
Posts: 1086
Joined: Tue Aug 23, 2016 10:35 am
Skype: miinipaa
Contact:

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

#4 Post by Ocelot » 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() ))
< < insert Rick Cook quote here > >

User avatar
quinnsea
Regular
Posts: 51
Joined: Wed Apr 26, 2017 5:04 pm
Completed: Birch Sapling
Projects: Aspen, Baleen
Organization: Koimarina Games
IRC Nick: quinnsea
Soundcloud: nishiikigoi
itch: koimarina
Location: Missouri
Contact:

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

#5 Post by quinnsea » Mon May 17, 2021 4:03 pm

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!
ImageImage

quinn / 23 / game design student
icon by church

Post Reply

Who is online

Users browsing this forum: No registered users