+1 vote

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!")
    add_child(newButton, true)
    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.

in Engine by (16 points)

2 Answers

+3 votes
Best answer
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")
by (21,930 points)
selected by

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

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")

0 votes

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")
by (1,244 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 [email protected] with your username.