Unexpected behavior when trying to execute code before showing file dialog

Godot Version

4.2.2

Question

I am encountering issues when trying to execute a line of code directly before opening a read only file dialog.

I simplified my code to highlight this issue:

func _ready():
	button.pressed.connect(_open_dialog)

func _open_dialog():
	print("test")
	dialog.visible = true

When I press the button the file dialog opens and waits for a file selection before “test” is printed in the debug console. I would have expected test to be printed first and then the dialog appear. What I’m actually trying to achieve is to make a node visible before the dialog appears, but I need to get this working first.

Is this the expected behavior of the file dialog? If so how would i go about executing code before the dialog appears?

Can you connect to a different function and have that function first show your node, and then call _open_dialog?

edit: nevermind, I thought _open_diaog was an override. Maybe a very short timer would work?

edit 2: actually this is probably better - Window, which this inherits from, has a signal called about_to_popup() Window — Godot Engine (stable) documentation in English

Thanks for the suggestion, I changed my code to:

	print("hello")
	await get_tree().create_timer(0.1).timeout
	dialog.visible = true

I’m going to look into your second suggestion now it seems very promising.

1 Like

No worries. I kinda jumped the gun a little there!

Hmmm. I tried:

button.pressed.connect(func(): dialog.popup())
dialog.about_to_popup.connect(func(): print("test"))

looking through the debugger it seems like the about_to_popup signal is never emitted which is odd but I might be misunderstanding how it works.

I did a quick test as so:

class_name DialogTest
extends Node

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	var dialog = $FileDialog;
	dialog.about_to_popup.connect(func(): print("test1"));
	dialog.visibility_changed.connect(func(): print("test2"));
	dialog.popup();
	print("test3");

Seemed to work fine, console printed test1 and test2.
Though it did also print test3 right away - dialog.popup() is asynchronous. It makes sense that it’s designed that way but I’m now very confused about what happened in your original code sample. You said it didn’t print “test” until after you closed the file dialog?