how do I use built in signals through code and not the node gui

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

I’d like to use the built in signals but I’m creating the node that I want to use with the TextureRect.new() in code.

I know i can make a custom signal. That’s not what I want to do though.
Is there a way to use the built in signals on mouse entered and exited through just code?
I guess I need to know how to turn the signal on and how to write a function for the signal.

:bust_in_silhouette: Reply From: njamster

Check the documentation:

var new_rect = TextureRect.new()
new_rect.texture = load("<ImagePath>")
new_rect.connect("mouse_entered", self, "on_TextureRect_entered")
new_rect.connect("mouse_exited", self, "on_TextureRect_exited")
add_child(new_rect)

func on_TextureRect_entered():
	print("TextureRect entered!")

func on_TextureRect_exited():
	print("TextureRect exited!")
:bust_in_silhouette: Reply From: jgodfrey

Sure, you can do that in code. Here’s an example, though you’ll likely need to season it to taste based on your code strucuture.

func _ready():
	$TextureRect.connect("mouse_entered", self, "_on_mouse_entered")
	$TextureRect.connect("mouse_exited", self, "_on_mouse_exited")

func _on_mouse_entered():
	print("Mouse entered")

func _on_mouse_exited():
	print("Mouse exited")

Here, I’m referencing a TextureRect instance that I created in the editor, but it doesn’t matter. Just replace my $TextureRect with your code-created reference.