Best practice for loading images used in program?

Godot Version

GD-4.2

Question

Currently, I am creating an dictionary with a Key and preloading an image as the value. I was wondering if there is a better way to do this or if I can do this from a .json file? Preloading hundreds or thousands of sprites will definitely slow down the load time when starting up but I’m not sure how else to go about this.

I am using a singleton that includes other dictionaries that I use throughout the program.

This is what I’m doing now and how I use it:

var sprites = {
	"logs": preload("res://UI/Items/Item Sprites/Logs/logs.png"),
	"oak_logs": preload("res://UI/Items/Item Sprites/Logs/Oak logs.png"),
	"willow_logs": preload("res://UI/Items/Item Sprites/Logs/Willow Logs.png"),
	"teak_logs": preload("res://UI/Items/Item Sprites/Logs/Teak logs.png"),
	etc
}

func get_sprite(id):
	var sprite
	sprite = sprites[id]
	return sprite

Would it be possible to load similar resource items, like trees or mineable ores, into a single spritesheet and have the type be an offset on the sheet?

So if your sprites are 32hx32w then having a sheet of 5 trees being a 32hx160w image and then when instantiating the item, having the offsets defined as an int 0-4 enum, multiply that by the width of the sprite to obtain the sub sprite. There might be a more intelligent and intuitive way in godot to achieve this but I think you could accomplish your goal with this method. This will allow you to have one sheet with all like resources and then you only have to add an int to your enum list.

This is a grand idea. Thank you! Ill keep the thread running for a little longer incase someone else want to chime in.

You can use a ResourcePreloader node to preload a bunch of resources in a scene by drag and dropping them in the editor.

But if you don’t want to preload them then just keep the path and load() it when requesting the resource

var sprites = {
	"logs": "res://UI/Items/Item Sprites/Logs/logs.png",
	"oak_logs": "res://UI/Items/Item Sprites/Logs/Oak logs.png",
	"willow_logs": "res://UI/Items/Item Sprites/Logs/Willow Logs.png",
	"teak_logs": "res://UI/Items/Item Sprites/Logs/Teak logs.png",
	etc
}

func get_sprite(id):
	if sprites.has(id):
		return load(sprites[id])
	else:
		push_warning('Sprite < %s > not found' % id)
		
	return null

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.