Problem with the coins in a platform game

Godot Version

Godot 4

Question

Hi, I’m creating a platform game with coins and doors to change levels. In the code there’s a problem.
This is the code for the levels 1 and 2 (shared)

extends Node2D

@export var tutorial_num = 0

func _ready():
	print("Tutorial " + str(tutorial_num))
	for monete in $Moneta.get_children():
		monete.monete_collezionate.connect(_on_monete_collezionate)

func _on_door_player_entered(tutorial):
	get_tree().change_scene_to_file(tutorial)

func _on_monete_collezionate():
		$CanvasLayer/Monete_collezionate.text = "Monete collezionate: " + str(Globale.gemme_collezionate)

And this is the code of the coins

extends Area2D

signal monete_collezionate

func _on_body_entered(body):
	if body.name == "Player":
		print_debug("Collected")
		Globale.gemme_collezionate += 1
		monete_collezionate.emit()
		queue_free()

It gives me the error:

Invalid get index ‘monete_collezionate’ (on base: ‘CollisionShape2D’).

Can you please give me the reason?

Can you show the scene tree, especially the “Moneta”-node and its children?


This one?

Maybe you mean to use groups in this line:

for monete in $Moneta.get_children():
# instead
for monete in get_tree().get_nodes_in_group("MyMonetesGroup"):

get_children() will go through the child nodes of Moneta, the CollisionShape and the AnimatedSprite, neither of which have a script nor signal attached.

I suspect you want to get every Moneta placed inside the scene, you can assign the Moneta scene a group, in my example “MyMonetesGroup”, then you can get all of the Moneta at the same time.

1 Like

Thanks!

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