Search Container disappears before the clickable buttons "click"

Godot Version

4.2.1 Stable version

Question

I have a search container (Vbox container) with buttons that are hidden initially.
I have a LineEdit that when selected (has focus) will make the search container visible with the clickable buttons (the LineEdit has code that displays what buttons are visible/not as typing text, functions as a search bar).
My problem is that when I attempt to click the buttons the search container disappears because the LineEdit no longer has focus on it. This makes the clickable buttons unclickable. I was thinking of trying await commands or making it while the search container is active and mouse inside vbox container it would stop the container from disappearing, but at a loss at how to code that. All efforts that round end with it disappearing anyways.

example code


func is_search_bar_selected():
	if search_bar.has_focus():
		search_container.visible=true
		if not cleared_text:
			search_bar.text=""
			cleared_text=true
	else:
		var list = search_menu_list.get_children()
		var selected_button=null
		for button in list:
			if button.has_focus():
				selected_button=button
				break
		if selected_button:
			await(is_mouse_inside_search_container())
			print(selected_button)
#problem is specifiically with search_container.visible=false
# (disappearing before buttons get clicked)
		search_container.visible=false
		search_bar.text=menu_state
		cleared_text=false

Waiting briefly after the LineEdit loses focus and then checking if the Button has the focus now should work. Minimal example:

func _on_line_edit_focus_entered() -> void:
	print("LineEdit focused")
	$Button.show()

func _on_line_edit_focus_exited() -> void:
	print("LineEdit unfocused")
	await get_tree().process_frame
	if not $Button.has_focus():
		$Button.hide()

func _on_button_pressed() -> void:
	print("Button pressed")

Not sure if one frame is guaranteed to be enough to process the focus change on all machines, but you could also wait longer by using await get_tree().create_timer(<timeout>).

thanks for the Idea, I ended up splitting my code into smaller bite sized pieces and it works now!


func is_search_bar_selected():
	search_container.visible=true
	if not cleared_text:
		search_bar.text=""
		cleared_text=true

func is_search_bar_deselected():
	await button_press()
	search_container.visible=false
	search_bar.text=menu_state
	cleared_text=false

func button_press():
	var buttons=search_menu_list.get_children()
	for button in buttons:
		if button.has_method("_on_search_button_pressed"):
			button.call("_on_search_button_pressed")
			print("button is pressed")
		else:
			print("button missing function")

Thank you for the help!

1 Like

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