How to use FileDialog add_button and custom_action

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

I’m trying to have a dialog that either opens an existing directory or creates a specially named directory for my data repository. To do this, I’m using a FileDialog with directory settings and then adding another button for “Create New”. Unfortunately, the custom_action signal is not called when the button is pressed. Here is my reduced example.

extends Control

onready var open = $OpenDialog

func _ready():
	open.mode = FileDialog.MODE_OPEN_DIR
	open.window_title = "Open or Create a Repository"

func _on_OpenDialog_ready():
	$OpenDialog.add_button("Create New", true)

func _on_OpenDialog_custom_action(action):
	print("Create new at "+open.current_path)

func _on_OpenDialog_dir_selected(dir):
	print("Selected "+dir)
func _on_OpenDialog_confirmed():

This is attached to the Startup scene and the signals attached to the functions above. I can open an existing directory with no problems but the Create New button is non-responsive. I’m very very new to Godot so probably just missing something.

:bust_in_silhouette: Reply From: jgodfrey

I think your problem is here:

$OpenDialog.add_button("Create New", true)

You need to pass a 3rd argument - the action name. For example:

$OpenDialog.add_button("Create New", true, "create_new")

This will be passed into the callback so you can differentiate multiple custom actions in the same function.

I will add that the 3rd argument doesn’t appear to be required, but it seems to not work without it (?) - at least in my limited testing…

jgodfrey | 2022-12-24 02:21

Thanks. You are correct. Adding that did make it work. Looks like a bug to me because, as you say, that parameter shows as not being required (having a default value).

AmusedBored | 2022-12-24 04:40