Nonexistent function connect in base Array when trying to connect signal

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Michael11

Hello guys,

I am trying to use signals for node_groups and somehow getting an error.

This is what I have in the config section in one of my scenes:

onready var enemy = get_tree().get_nodes_in_group("enemy")

Then in ready function in the same scene I have this:

func _ready():
	enemy.connect("enemy_killed", self, "_on_enemy_killed")

When I try to launch the program I get this error: Nonexistent function connect in base Array.

When I use it just for a single node, it works fine. Once I use it for a Group where there are more nodes, it gives me this error.

Why is that? Thank you

:bust_in_silhouette: Reply From: kidscancode

It works with one node because connect() is a function of Node.

get_nodes_in_group() returns an Array. You can’t call connect() on an array.

It is, however, an array of Nodes, so you can do this:

func _ready():
    for enemy in get_tree().get_nodes_in_group("enemy"):
        enemy.connect("enemy_killed", self, "_on_enemy_killed")

Thank you :slight_smile:

Michael11 | 2020-11-02 16:16