Skip to content

Instantly share code, notes, and snippets.

@WolfgangSenff
Last active January 14, 2023 01:33
Show Gist options
  • Save WolfgangSenff/f6ccf8dd0a640c312461e80394a09a62 to your computer and use it in GitHub Desktop.
Save WolfgangSenff/f6ccf8dd0a640c312461e80394a09a62 to your computer and use it in GitHub Desktop.
extends Resource
class_name FolderListResource
export (String) onready var FolderPath setget set_folder_path
export (String) var ResourceCategory
var all_resources = []
func set_folder_path(value):
FolderPath = value
load_all_resources()
func load_all_resources() -> void:
var folder = Directory.new()
if FolderPath and folder.open(FolderPath) == OK:
folder.list_dir_begin()
var file_name = folder.get_next()
while file_name != "":
if not folder.current_is_dir() and file_name.find(".tres") != -1:
all_resources.push_back(load(FolderPath.plus_file(file_name)))
file_name = folder.get_next()
folder.list_dir_end()
else:
print("An error occurred when trying to access the folder.")
func find_index_of(resource : Resource) -> int:
return all_resources.find(resource)
The 4.0 version of this is:
extends Resource
class_name FolderListResource
@export
@onready var FolderPath : String:
set(value):
FolderPath = value
load_all_resources()
@export
var ResourceCategory : String
var all_resources = []
func load_all_resources() -> void:
if FolderPath:
var folder = DirAccess.open(FolderPath)
folder.list_dir_begin()
var file_name = folder.get_next()
while file_name != "":
if not folder.current_is_dir() and file_name.find(".gd") == -1:
all_resources.push_back(load(FolderPath.path_join(file_name)))
file_name = folder.get_next()
folder.list_dir_end()
else:
print("An error occurred when trying to access the folder.")
func find_index_of(resource : Resource) -> int:
return all_resources.find(resource)
@WolfgangSenff
Copy link
Author

This is one of the more useful resources I've ever created. This will automatically load all resources within a given folder. These can be images, sounds, text files, .tres files - anything you want. Just be careful as they are not guaranteed to all be the same type unless you're very careful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment