The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

Hello everybody!

I used the code from here to create a button at runtime and attach a function to it:
https://docs.godotengine.org/en/stable/classes/class_button.html
func ready():
var button = Button.new()
button.text = "Click me"
button.pressed.connect(self.
buttonpressed)
add
child(button)

func buttonpressed():

But when I run my program, it gives me the following error message:
Invalid get index 'buttonpressed' (on base: 'Node2D (palya1kodja.gd)'). Did you mean '.buttonpressed()' or funcref(obj, "buttonpressed") ?

If I try so: button.pressed.connect(self.buttonpressed())
I also get an error message: Invalid call. Nonexistent function 'connect' in base 'bool'.
What is wrong with this? Maybe button creating is not working in Godot 3.3 in this mode, only in Godot 4.0?

Godot version 3.3
in Engine by (52 points)

2 Answers

+1 vote

Try changing button.pressed.connect(self.buttonpressed) to button.connect("pressed",self,"buttonpressed")

The syntax for connecting to signals is different from Godot 4 and Godot 3 I beleive

by (1,434 points)
0 votes

You're looking at the Godot 4.0 documentation. The way that signals are hooked up changed in Godot 4. Take a look at the docs for Godot 3.3.

by (320 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.