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?