Connecting a Signal to a Function in another Script

<4.2.1 Godot Version>

Question

Hello! Recently I found a script for Solitaire in Godot but it’s in Godot 3.0 version of the language. I’ve been translating it but got stuck on an issue I think has something to do with signals and how to connect them to functions on a different script. I’m still pretty new to this so I tried looking at the documentation for signals and couldn’t figure it out.

I’m trying to connect the signals from the “CardTexture” script with the functions from the “Board” script. So I added a preload of the “Board” script and tried doing this:

var board = preload("res://scripts/board.gd")

signal dragged

func _ready():
	board = get_parent()
	dragged.connect(_on_Card_dropped)

But it gives me this error message:

Identifier “_on_Card_dropped” not declared in the current scope.

Which I assume means I haven’t connected the two scripts correctly. Or the preload isn’t enough for the signal to call the function from the “Board” script.
This is the function I’m trying to call on the “Board” script (along with some others):

func _on_Card_dragged(_card, _position):
	current_move = Move.new()
	current_move.initialize(_card, slots, deck, pool)
	current_move.card_back_in_pool.connect(update_pool_layout)
	if _card.slot_idx >= 0:
		var slot : CardSlot = slots[_card.slot_idx]
		slot.remove_card(_card)
	dragged_from = _card.slot_idx
	_card.dragged_from = _card.slot_idx
		
	if _card in pool:
		card_drawn_from_pool = true
		pool.erase(_card)
	remove_frames()
	update_label()
	$HintTimer.stop()

And this is what was originally in the “CardTexture” script with the 3.0 syntax:

func _ready():
	board = get_parent()
	connect("dragged", board, "_on_Card_dropped")

With this error message:

Cannot pass a value of type “String” as “int”.
Invalid argument for “connect()” function: argument 3 should be “int” but is “String”.

Which I guess is the syntax issues from Godot 3.0 to 4.0. Any help would be greatly appreciated. I’ve been stuck on this for a while now and am not sure where to look next. Thank you in advance!

Is the “CardTexture” script in a scene that you have multiple instances of?

No they both have their own respective scenes. The Board script has its own scene and the CardTexture script has its own scene as well.

Oh! Also I forgot to mention when the old “CardTexture” script sets up the variable for Board it does it like this:

var board

signal dragged

No preload, no anything… So that was kind of confusing to me. I also thought that maybe it had something to do with the get_parent() method but I am not very familiar with that either.

I’m pretty sure that error means it’s looking on the wrong script for your function.
try:
dragged.connect(board._on_Card_dropped)

and the preload is unnecessary…the
board = get_parent()
is overwriting it anyway when the script runs _ready. Assuming the ‘board’ node is the parent of the script on which the signal is being connected, it should work.

2 Likes

Thank you so much that’s exactly what I needed. Im still very unfamiliar with the syntax of GDscript so I couldn’t figure it out for the life of me.

Also in the “Board” script I needed to change the ‘instance’ method to ‘instantiate’ so the “CardTexture” script could properly be added to the Board script as a child.