+1 vote

I am making a starter project which should show a random image on the screen when I enter a certain area. However, the folder has a large amount of images in. How can I go about selecting a random image from this folder, resize it (somewhat) and then display on the screen?

My current plan is to preload all of the images in the folder and store them in an array, which I can then access using a random number when choosing which image to display to the screen (using the image as a texture on a large sprite).

Is there an easier/simpler way to do this? Like iterating over the folder and inserting all the images into an array, there are too many for me to type in each pre-loaded image manually.

Thanks.

in Engine by (52 points)

Sounds like a perfectly fine solution to me. What's bothering you about it?

Depending on the amount (and size) of images, you might of course be better off selecting loading it at runtime (instead of preloading all of them in advance - which seems a bit wasteful, if all you need is one image).

Loading at runtime might be what I end up doing, sounds like a good practice I should get into doing. But really the question was regarding how to do it, although I was open to suggestions if there was a better way I didn't know of (such as your suggestion to load at runtime, thanks for that).

1 Answer

+2 votes
Best answer

You can get all files from a directory and store them like this.

var path = "path/to/folder"
var dir = Directory.new()
dir.open(path)
dir.list_dir_begin()
while true:
    var file_name = dir.get_next()
    if file_name == "":
        #break the while loop when get_next() returns ""
        break
    elif !file_name.begins_with("."):
        #get_next() returns a string so this can be used to load the images into an array.
        your_image_array.append(load(path + file_name))
dir.list_dir_end()
by (3,223 points)
selected by

Thank you very very much, going to try implementing this now.

Alright so after implementing, I thought i'd post the altered code.

Your code was pretty much perfect, although I forgot to add a "/" to the appended path+filename, this probably won't be a problem for most people.

Also, I found that I was importing two objects per image in the file, a jpg and a .import. The .import returns nulls and so need to be left out when appending the image to the pics list. Should say this once again is probably an issue on my end rather than anything you put my way. Either way here's the finished product for other people:

extends Sprite
var pics = []
func _ready():
    var path = "res://path/to/folder"
    var dir = Directory.new()
    dir.open(path)
    dir.list_dir_begin()
    while true:
        var file_name = dir.get_next()
        if file_name == "":
            #break the while loop when get_next() returns ""
            break
        elif !file_name.begins_with(".") and !file_name.ends_with(".import"):
            #if !file_name.ends_with(".import"):
            pics.append(load(path + "/" + file_name))
    dir.list_dir_end()

Thanks for helping me out.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.