Issue with .connect to objects - scenes in class

Godot v. 4.2.1
Creating an instance of the button scene. Nothing happens from pressing the button. Trying to connect a function to an instance of the button, an error pops up
Error: Invalid call. Nonexistent function ‘connect’ in base ‘Callable’

class Game:
	static var deck_hand = [[],[],[],[],[],[]]
	...
	func _init():
		...
		
		...
	func get_cards(parent):
		...
		deck_hand[card.value].append(preload("res://CardButton.tscn").instantiate())
		parent.add_child(deck_hand[card.value][-1])
		deck_hand[card.value][-1].pressed.connect(parent.click)
		#If I replace this line with
		#deck_hand[card.value][-1].connect("pressed", parent, "click")
		#then this error will come out:
		#Invalid type in function 'connect' in base 'Button (CardButton.gd)'. Cannot convert argument 2 from Object to Callable.

...		
func _ready():
	var game = Game.new()
	get_cards(self)
...
func click():
	print("The button has been pressed!")

So, the connect function to my knowledge cannot specify what node to connect to.

Connect has two parameters:
Error connect(signal: StringName, callable: Callable, flags: int = 0)
the signal you’re connecting to, and the function to connect it to (or the callable, which is basically a function in a variable).

So, in the node you want to connect the signal to, which seems like this “parent” node? you need to call the connect function.

something like

parent.connect("pressed", on_button_pressed)

In Godot 4, you can also do this:

parent.pressed.connect(on_button_pressed)

(this is assuming “parent” in this situation is whatever has the 'connect" signal.

PS:
It looks like on further looking, you want to connect the “pressed” signal in deck_hand[card.value][-1] to your current node.

So you would do the above, but just something like:
deck_hand[card.value][-1].pressed.connect(on_button_pressed)

This could get kind of complicated though, because I’m not sure it will let you do this connection to the same function for all signals, which means you may have to go into lambdas and some other mess…

Let me know how it goes though and if that helps any!

Thank you to your answer.
I tried to do the same thing as in the help of the program, but at least the program started without errors:
deck_hand[card.value][-1].connect("button_down", Callable(self, "_on_button_down"))
or
deck_hand[card.value][-1].button_down.connect(Callable(self, "_on_button_down"))
The buttons didn’t do anything at all, and the buttons weren’t pressed.
I can assume that the error is in the card/button itself that I am creating. Since I am importing the “CardButton.tscn” scene, it may not transmit the button click functions, and that is why they are not recognized.
Anyway, I created a TextureButton inside my class and assigned it all the parameters there, not inside the “CardButton.tscn” scene. It helped

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