Resources not export after export (with var dir = DirAccess.open(path))

Godot Version

Godot 4.4

Question

For contex, i have some classes with methods that works like this:

func load_enemy_data():
	enemy_templates.clear()
	_load_enemy_data_recursive("res://balance/enemy/")
func _load_enemy_data_recursive(path: String):
	var dir = DirAccess.open(path)
	if not dir:
		push_error("EnemyGod: cannot open directory: " + path)
		return
	dir.list_dir_begin()
	var file_name = dir.get_next()
	while file_name != "":
		var full_path = path.path_join(file_name)
		if dir.current_is_dir():
			if file_name != "." and file_name != "..":
				_load_enemy_data_recursive(full_path) 
		elif file_name.ends_with(".tres"):
			var res: EnemyData = load(full_path) as EnemyData
			if res:
				if res.id in enemy_templates:
					push_error("Duplicate enemy ID %d in '%s'" % [res.id, full_path])
				else:
					enemy_templates[res.id] = res
		file_name = dir.get_next()
	dir.list_dir_end()

The idea is that I store different resources as datapacks, and based on this data I restore behavior/instant enemies/create spells via factories. In dev mode it works, but when exporting a project (for Win) it doesn’t. I tried adding filters up to doing res://* - and in the console Godot seems to be able to find the folders themselves because I don’t get errors - however the method and similar ones return an empty list. Enemies and spells also don’t load when expected. Enemies and spells also do not load in those cases when expected and where in dev build they appeared without problems.

func _ready():
	load_enemy_data()
	GDConsole.create_command(_debug_enemy)
func _debug_enemy():
	print("spells in data: ", enemy_templates)

Here the program will say that the resource was not found.

func create_enemy_by_id(id: int, is_real_enemy := true) -> Enemy:
	var e = ENEMY.instantiate() as Enemy
	var template = enemy_templates.get(id)
	if template == null:
		push_warning("No EnemyData found for id %s" % id)
		return null

DirAccess doesn’t account for how the resources are transformed on export, maybe you should use ResourceLoader.list_directory instead.

1 Like

I’ll use list_directory() and that works for me, thanks

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