Connecting signal to instanced node throws error

Godot Version

4.3 stable

Question

(very new to Godot and programming in general, trying to graduate from Scratch 2.0 to something professional)

I’m trying to connect a signal with a variable to an instanced scene, and whenever the connect … command? line? (idk what it’s called) is executed, the game window crashes and it gives the following error:

Invalid access to property or key 'on_level_coordinate' on a base object of type 'Node2D'.

I’ve tried a lot of ways I’ve read to connect the signal using gdscript, since I know I can’t use the editor directly because the scene is being instanced after the game starts (so it doesn’t exist yet), but nothing has worked. As the error says, my scene “Pipe” IS a Node2D, but I don’t understand how or why that would be a problem.

Here’s my code for reference (being run in the base node of my tree, “Level”, also a Node2D):

extends Node2D
signal coordinate
var pipe_scene: PackedScene = load("res://Scenes/pipe.tscn")

func _on_player_dropped(coordinate_0: Vector2) -> void:
	var Pipe = pipe_scene.instantiate()
	$Pipes.add_child(Pipe)
	connect("coordinate",Pipe._on_level_coordinate) #this makes it crash
	emit_signal("coordinate",coordinate_0)

I’m also expecting this scene “Pipe” to be instanced multiple times, and for all of them to be connected to the signal “coordinate”. I hope this is enough context and I hope I used all of these words at least sort of correctly. Thanks in advance for any help anyone can give.

It means there is no function in Pipe called on_level_coordinate, please make sure of it.

1 Like

This is the function in pipe.gd:

func _on_level_coordinate(coordinate: Vector2) -> void:

is this somehow not right or doesn’t match?

Looks weird with your codYour code looks weird, try this:

extends Node2D

signal coordinate(example_parameter) 

var pipe_scene: PackedScene = load("res://Scenes/pipe.tscn")

func _on_player_dropped(coordinate_0: Vector2) -> void:
	var Pipe = pipe_scene.instantiate()
	$Pipes.add_child(Pipe)
	coordinate.connect(Pipe._on_level_cordinate) 
	coordinate.emit(coordinate_0)

This code does what your code should do, but I’m not sure you’re looking for that, this code (which is a modified version of your code) adds a pipe every time the player falls, and connects the signal to its function, and finally calls the signal with the vector parameter (which will also activate the function).
I have no idea what this will do for you, so I guess the logic of this code isn’t exactly what you’re looking for, if so, please explain what you want to do and share with us the function in the pipe.

I wrote out a huge explanation and just realized what was wrong. The pipe.gd script somehow got disconnected from my pipe scene and I didn’t even notice. It works fine now. I’m so sorry lmao :sob:

1 Like