0
\$\begingroup\$

How we can get the button reference on the event function that is connected? My connect signals never works

extends PanelContainer

onready var hbox_container = $ScrollContainer/HBoxContainer
var btn 

func _ready():
    createbutton()

func createbutton():
    btn = Button.new()
    btn.set_name("button 1")
    btn.text = "button 1"
    hbox_container.add_child(btn)
    btn.connect("toggled", self, "button_toggled", [btn])

func button_toggled(toggled, target):
    print("which button = ", target.get_name())
    if toggled == true:
        print("Button ist pressed")
    else: 
        print("Button is released")
\$\endgroup\$
1
\$\begingroup\$

You need to set the toggle_mode

extends PanelContainer

var btn:Button

func _ready():
    createbutton()

func createbutton():
    btn = Button.new()
    btn.set_name("button 1")
    btn.text = "button 1"

    btn.toggle_mode = true

    add_child(btn)
    btn.connect("toggled", self, "button_toggled", [btn])

func button_toggled(toggled, target):
    print("which button = ", target.get_name())
    if toggled == true:
        print("Button ist pressed")
    else: 
        print("Button is released")

I removed onready var hbox_container = $ScrollContainer/HBoxContainer for brevity

It is best to type variable to get autocomplete to work like var btn:Button done above.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.