Help preloading hundreds of sprites

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By miss_bisque

I’m creating a “fruit ninja”-like game where I need to preload around 200 sprites to use during my game. I have 6 folders of 34 sprites each. The idea is that there will be 6 levels to the game, and each image folder has the sprites needed for each level.

how do I efficiently preload hundreds of sprites into my game without having to hard code every single image into a variable such as var bowl = preload("res://Sprites/Objects/Folder1/bowl.png")? The catch is that each image needs to have a unique name that I can point to in my other scripts.

Here’s my code that I’m using for a few test images. I have a list of preloaded images that I then put into an array and use to make different target and distractor objects that are used as sprite textures in the main part of my game. This script is autoloaded in my game:

extends Node

var list = range(0,10)
var sample =[]
var distractor_object = []
var target_objects = []


var bowl = preload("res://Sprites/Objects/bowl.png")
var bread = preload("res://Sprites/Objects/bread.png")
var cheese_grater = preload("res://Sprites/Objects/cheese_grater.png")
var clock= preload("res://Sprites/Objects/clock.png")
var cup = preload("res://Sprites/Objects/cup.png")
var pot = preload("res://Sprites/Objects/pot.png")
var straws = preload("res://Sprites/Objects/straws.png")
var tissue_paper = preload("res://Sprites/Objects/tissue_paper.png")
var toilet_paper = preload("res://Sprites/Objects/toilet_paper.png")
var tooth_paste = preload("res://Sprites/Objects/tooth_paste.png")

var tex_ref_array = [bowl, bread, cheese_grater, clock, cup, pot, straws, tissue_paper,
 toilet_paper, tooth_paste]

func _ready():
	randomize()
	for i in range(4):
		var x = randi()%list.size()
		sample.append(list[x])
		list.remove(x)
	print("Array is " + str(sample))
	target_objects = sample.slice(0,2)
	print("Target objects are " + str(target_objects))
	distractor_object = sample[3]
	print("Distractor object is " + str(distractor_object))

So I need to preload way more images than just these 10, and be able to use them in my game.

Any suggestions?

thanks it definitely gets me part of the way there… I think what i’m unsure about is how to have specific names for the files in the array.

so in the link you posted above to a similar question, how would I access my array and then rename the files to store and name them, like i’ve done above?

does that make sense? I guess what I’m trying to say is that I can’t just have an array pics = res://Sprites/Objects/straws.png, res://Sprites/Objects/tissue_paper.png etc. I need to be able to rename them and point to those names in my game.

also, in the link you posted https://forum.godotengine.org/62238/how-to-preload-folders-worth-of-images-and-put-them-into-array I tried the code

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()

but when i print(pics), the array is empty.

miss_bisque | 2020-12-15 04:10

I don’t have any problems with the code posted. If you used that code as is then you’d want to change the path variable: var path = "res://path/to/folder"

var path = "res://Sprites/Objects/"

avencherus | 2020-12-15 06:38

:bust_in_silhouette: Reply From: Bimi124

Just loop through the level`s folder (see https://forum.godotengine.org/5175/how-to-get-all-the-files-inside-a-folder) Then you can preload them in a loop and add them to your list.