'Nonexistent signal' with '.connect'

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

I’m trying to use code to determine if a “create new post” button already exists at the end of a gallery, and if not, make one. I’m finding it simple to create the button and position it and everything, but am having issues creating and connecting a signal from the button to the root Node2D in order to… you know, be able to create a new post.

Now, it’s my understanding that ‘.connect’ functions like

PARENT_NODE.connect("signal", CHILD_NODE, "signal_method"

So here’s what I’ve done (unrelated code removed for post brevity):

extends Node2D

var mainBox = get_node(".")
var gridBox = get_node("Container/PostGrid")
var newButton = Button.new()

func NewPostButton():
  if gridBox.has_node("NewButton"):
    print("can create New Post!")
  else:
    add_child(newButton, true)
    newButton.show()
    mainBox.connect("pressed", self, "buttonHandler_pressed")

func buttonHandler_pressed():
  print("button pressed!")

But when I play the scene, I get the error

In Object of type 'Node2D': Attempt to connect nonexistent signal 'pressed' to method 'Button.buttonHandler_pressed'

I’m lost, since my understanding tells me that the syntax is correct, and that since the hardcoded object Button has a “pressed” signal in the editor, I’m a little lost as to what I’m doing wrong. Any help would be appreciated.

:bust_in_silhouette: Reply From: kidscancode
var mainBox = get_node(".")

This gets the current node, which is a Node2D. Node2D does not have a “pressed” signal.

Your button is referenced by newButton, so that’s the node you should call connect() on.

newButton.connect("pressed", self, "buttonHandler_pressed")

I spent three days banging my head against this. It works perfectly now, thank you!

occultnix | 2020-01-23 06:06

For me the issue was that I was trying to connect to the wrong node (parent node of emitting node)

When I realize that, it was just a matter of adding /KinematicBody2D to the following code:

get_node('Player/KinematicBody2D').connect("dead", self, "_add_new_player")

rslonik | 2020-01-24 03:38

:bust_in_silhouette: Reply From: Bernard Cloutier

Now, it’s my understanding that ‘.connect’ functions like

PARENT_NODE.connect("signal", CHILD_NODE, "signal_method"

This is false. Here is how it works:

[Node that has the signal].connect("signal_name", [Node that handles the signal], "signal_handling_method_name")

So if you want to connect a signal on a button node to a method in your current node, you would write:

get_node("path_to_button_node").connect("pressed", self, "_on_Button_pressed")