Connect a signal to a function via script

Hi, I want to connect a mouse_entered and mouse_exited signal to respective functions.

I have this :

extends Area2D

func _ready():
	if has_signal("mouse_entered"):
		connect("mouse_entered", _on_mouse_entered)
	if has_signal("mouse_exited"):
		connect("mouse_exited", _on_mouse_exited)


func _on_mouse_entered():
	print("plop")

func _on_mouse_exited():
	print("zblurg")

I don’t have any error, and it does go through the conditions.
The script is attached to an Area2D node with a CollisionPolygon2D child node. I changed only its position since I added this node

ChatGPT and documentation would let me think what I’ve done should work, but I guess not. Anyone ?

I usually use mouse_entered.connect(_on_mouse_entered) syntax.

This syntax would look like this for cross node signals

Parent connecting two children
Node.signal_name.connect(OtherNode.func_name)

One node connected other node signal to self.
Node.signal_name.connect(some_func)

Not surprisingly, ChatGPT is very wrong:

  1. has_signal is completely unnecessary, as Area2D is guaranteed to have the mouse_entered and mouse_exited signals.

  2. The connect syntax is wrong, even for Godot 3.x.

  3. use mouse_entered.connect(_on_mouse_entered) for Godot 4.

  4. I despise ChatGPT, so excuse my arrogant tone. :slight_smile:

I tried this way as well and sale result, nothing

Be sure your Area2D has at least one collision layer set, and the Area2D has pickable enabled.

Also, try using a simpler collision shape for testing (like a box). Concave collision shapes can cause collision failures.

2 Likes

So… I detached and deleted the script. Created a new one, didn’t rename it (we never know, i’m new to godot), connected the mouse_entered signal with itself through the Node tab and still doesn’t do anything. The Area2D has a simple rectangle collision shape as a child and it has pickable on true.

I mean, I can’t do anything simpler…

extends Area2D

func _on_mouse_entered() -> void:
	print("hello world")

Does it show the green arrow icon to the left of the function? Do you have your area positioned somewhere where your mouse actually enters and exits your collision shape?

Do you have this icon (circled in red) enabled, and is it not greyed out?


If it looks a little greyed out compared to this, just click on it.

I just tried what you said you did, and it works just fine. If you could take screenshots of your entire Godot application in 2D view, I might be able to identify something.

Yes, yes and yes

image

(I can put one screenshot at a time)

Edit: Tried to put the Area2D node at the root
Edit2: I’m currently using mouse_entered signal on panels and works fine. I must miss something about Area2D nodes

I don’t know why it doesn’t work BUT whatever I want to do, I realized I can do it with panels instead of areas. Areas is more for collision which I don’t use.
Thanks for your help, it’ll still be a mystery (to me at least) but w/e I guess it was more a bad choice of node (for what I need to do) than anything else.

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