GODOT finds dirs that don't exitst

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Uskazd

I’m trying to build a save system that requires multiple folders and files (cause I actually want to give users the ability to modify some of them).

A problem I keep running into when trying to find these save folders is that my code also finds “.” and “…” folders which don’t exist.

I tried adding a failsafe which checks if that the map has the Game.json file which contains game parameters using “file_exists”.

var world_location = str(str(saveDir) + str(file_name))
var world_file_location = str(str(world_location) + "/Game.json")
    
func _checkSaves():
	#WORKS
	if newDir.open(saveDir) == OK:
		newDir.list_dir_begin()
		var file_name = newDir.get_next()
		
		while(file_name != ""):
			
			if newDir.current_is_dir():
				var check_world_file = Directory.new()
				world_location = str(str(saveDir) + str(file_name))
				check_world_file.open(world_file_location)
				
				if check_world_file.file_exists(world_file_location):
					var dir_name = file_name
					gameSaves.push_back(dir_name)
					
				print("Found directory: " + file_name)
				
			file_name = newDir.get_next()
		
		newDir.list_dir_end()
	
	print("Folders: " + str(gameSaves))

The output gives the following result:

Found directory: .
Found directory: ..
Found directory: Alpha 1
Folders: []

While it should be:

Found directory: .
Found directory: ..
Found directory: Alpha 1
Folders: [Alpha 1]

And excluding “if check_world_file.file_exists(world_file_location):” gives me:

Found directory: .
Found directory: ..
Found directory: Alpha 1
Folders: [., .., Alpha 1]
:bust_in_silhouette: Reply From: jgodfrey

You can eliminate the return of the . and .. folders from the list_dir_begin() call by passing the skip_navigational argument to that method as true, as documented below:

https://docs.godotengine.org/en/stable/classes/class_directory.html?highlight=list_dir_begin#class-directory-method-list-dir-begin