How to get all tscn files from a folder and put them into an array as, preload("file")

Godot Version

4.3

Question

So i have an arry of preloaded scenes, but i dont want to add to it manually instead i would like to get all the tscn files from a folder and put them in the array as preloaded scenes. Can i do this?

for preload you have to use predefined strings, so its not allowed to create these strings through code.

If you want to use load() its definitly possible

2 Likes

load() would work for my purpose
how would i do it?

Use DirAccess, here’s a snippet for going through a directory

func dir_contents(path):
	var scene_loads = []	

	var dir = DirAccess.open(path)
	if dir:
		dir.list_dir_begin()
		var file_name = dir.get_next()
		while file_name != "":
			if dir.current_is_dir():
				print("Found directory: " + file_name)
			else:
				if file_name.get_extension() == "tscn":
					var full_path = path.path_join(file_name)
					scene_loads.append(load(full_path))
			file_name = dir.get_next()
	else:
		print("An error occurred when trying to access the path.")

	return scene_loads
3 Likes

Tysm this worked

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