Get button reference when pressed

Godot Version

4.2.1

Question

I have different buttons. When I pressed the buttons I want to get the Button Node (self). The pressed signal has no parameters. How can I do this in Godot 4.2.1?
I tried to overwrite the signal, but it is not working:

	#button.emit_signal("pressed", button)
	#button.connect("pressed", myFunction)

func myFunction(node: Node):
	print("HEYHEYHEY ", node)

two ways to do this:

in the example below, i pass the node itself as an arg

extends Node2D
func _ready():
	var v_button : Button = find_child("Button2")
	v_button.pressed.connect(_button_pressed.bind(v_button, "Hello, world"))
func _button_pressed(a_button : Button, a_message : String):
	print("button name: ", a_button.name, " message:", a_message)

above code was swiped from here → Godot: how to connect button signal via script - Stack Overflow

and if you didn’t know, you can add a script to button and overload _pressed()

extends Button

func _pressed():
	print(name)
1 Like

Thank you. I think the second solution is not working for me, because I have to do something else with the variable and not just print it. It seems I cannot access the variable from the other script with this.

But the first one works for me!

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