I have been trying to make receiving file paths really easy, with this method in an autoload:

func file_dialog_path(extentions:PackedStringArray = [], dialog_type:FileDialog.FileMode = FileDialog.FileMode.FILE_MODE_OPEN_FILE) -> String:
	var new_dialog:FileDialog = FileDialog.new()
	new_dialog.min_size = Vector2i(400, 400)
	new_dialog.access = FileDialog.ACCESS_FILESYSTEM
	new_dialog.file_mode = dialog_type
	for i in extentions:
		new_dialog.add_filter(i)
	get_tree().current_scene.add_child(new_dialog)
	new_dialog.popup_centered()
	var file_path:String = ""
	new_dialog.file_selected.connect( func receive_path(path:String):
		file_path = path
		print("file path from within the signal: ", path)
		return path)
	file_path = await new_dialog.files_selected
	print("this is the file now", file_path)
	
	#while file_path == "":
		#await get_tree().create_timer(0.1)
	return file_path

I commented out the while loop, as it froze the entire game when I do this.

I have tried this many times now, but the method always seems to get stuck somewhere. Right now, it gets stuck in the “receive path” function, where it will print the path, but not actually return. I am receiving the output from the file_dialog_path method like this:

var file:String = await file_dialog_path(["*.mml"], FileDialog.FileMode.FILE_MODE_SAVE_FILE)
print("This is the filepath for our level ", file)

Could anyone help me with this? The best idea I have right now would be to let that commented while loop run on a thread, but that seems unneccessarily difficult for such a simple problem.

  • @copycat@lemm.ee
    link
    fedilink
    9
    edit-2
    1 month ago

    file_path = await new_dialog.files_selected

    You probably want file_selected, not files. When the file mode is set to single file, then only file_selected signal is emitted, and when the file mode is set to multiple files, only files_selected is emitted. So if you use await files_selected when file mode is set to single file, then it will be stuck forever.

    • Smorty [she/her]OP
      link
      1
      edit-2
      1 month ago

      Thank you for helping me with this. It really was the file_selected signal.