Loading a bunch of resource files at export?

Godot Version

4.5

Question

I have a lot of similar resource files that I want to load in an Array of Resource at startup.
Reading the forum or reddit, I came to this weird solution (removing “remap” extensions on “tres” files…):

	var _resources: Array[Resource]
	var _dir := DirAccess.open("res://resources/enemies/")
	for _file: String in _dir.get_files():
		print(_file)
		
		if (_file.get_extension() == "remap"):
			_file = _file.replace(".remap", "")
			
		if (_file.get_extension() == "tres"):
			var _resource := ResourceLoader.load(_dir.get_current_dir() + "/" + _file)
			_resources.push_back(_resource)

But I wonder if this is the right way to do it, because it smells like a poor workaround…
There should be a better solution when you have an undefined number of resources, or no ?

In my case, I have resources describing enemies, and I want to be able to pick up at random in this list of enemies, that could grow as soon as I invent new ones…

oh, nevermind, I just found the solution that was not very far :

	var _resources: Array[Resource]
	for _file: String in ResourceLoader.list_directory("res://resources/enemies/"):
		if (_file.get_extension() == "tres"):
			print(_file)
			var _resource := ResourceLoader.load("res://resources/enemies/" + _file)
			_resources.push_back(_resource)

this function does the job and was added in 4.4, that’s probably why I did not found the solution on the web…

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