0 votes

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.

in Engine by (60 points)

2 Answers

0 votes

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!")
by (10,483 points)
0 votes

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.

by (13,042 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.