Can't use "_focus_entered()" in Button Script

Godot Version

4.3

Question

Hello, so I’m struggling to understand why I can’t seem to get “_focus_entered()” to work on my script. Basically, when I enter a button’s focus, I want my script to activate a line of code, however attempting to write any code under the signal function “_focus_entered()” will result in nothing occurring.

A strange oddity I also noticed is that if I were to type “_pres”, the editor would give an auto-complete suggestion of the “_pressed()” signal function, meanwhile doing any variation of “_focus_entered()” or “_focus_exited()” will give no results as if the function doesn’t exist.

Am I doing something wrong here / missing something important? I’ve tried variations of the name as well, like “focus_entered()”, “_on_focus_entered()”, etc. but none have seemingly worked. Any help would greatly be appreciated!

For reference, the script shown below is meant to be attached to each individual button. These buttons are then placed 4 in a row in an HBoxContainer.

My expected outcome is for “b” to be printed to the console every time I move the focus between the buttons, but nothing is ever actually being printed. (The “_pressed()” signal function however does correctly print “a” when the button is pressed)

extends Button

func _pressed():
	print("a")

func _focus_entered():
	print("b")

If you look at the docs for BaseButton, you will notice, that there is a function called _pressed() mentioned, which does, what you expect on button presses.

In order to get notified, when the focus is entered, you will either need to listen to the respective notifications like this:

func _notification(what):
	if what == NOTIFICATION_FOCUS_ENTER:
		print ("%s: focus enter" % [get_path()])
	if what == NOTIFICATION_FOCUS_EXIT:
		print ("%s: focus exit" % [get_path()])

Or alternatively you could listen to the focus_entered/focus_exited signal like this:

func _ready() -> void:
	focus_entered.connect(_on_focus)

func _on_focus() -> void:
	print ("enter")

Thank you for the help, both of these methods have worked for me!

I suppose I was getting confused with the “_pressed()” function being usable within the script by default and getting activated when the “pressed()” signal is fired. I figured that all signals worked as such, and thus assumed that a “_focus_entered()” function would already exist and be activated upon the “focus_entered()” signal being fired. I was quite stumped when there wasn’t lol.

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