I have a coin scene and signal from it, which works fine. However, when I put copies of that scene on a level, signal are not working.
I have Coin scene and Player scene, where character touches coins and adds to sum of the coins.
extends Area2D
signal coin_entered
func _on_body_entered(_body):
emit_signal("coin_entered")
queue_free()
level.gd
extends Node2D
@onready var coins = $Coins
# Called when the node enters the scene tree for the first time.
func _ready():
var new_coin: PackedScene = preload("res://coins/coin.tscn")
var coin = new_coin.instantiate()
coin.connect("coin_entered",coin_collected)
add_child(coin)
for i in coins.get_child_count():
coins.get_child(i).connect("coin_entered",coined)
func coined():
emit_signal("coin_funel")
player.gd:
extends CharacterBody2D
@onready var level = $".."
var coins = 0
func _ready():
level.connect("coin_funel",coin_collected)
func coin_collected():
coins += 1
if coins > 0:
print(coins)
extends Node2D
@onready var coins = $Coins
# Called when the node enters the scene tree for the first time.
func _ready():
var new_coin: PackedScene = preload("res://coins/coin.tscn")
var coin = new_coin.instantiate()
coin.connect("coin_entered",coined)
add_child(coin)
for i in coins.get_child_count():
coins.get_child(i).connect("coin_entered",coined)
func coined():
emit_signal("coin_funel")
Thanks for your help. I manually add coins to the scene and now have another issue:
after game runs one instance of coins appears at top left corner with (0,0) coordinates like in Coin scene. How to avoid that?
your old code has this line so I assume you still want to spawn that coin
here is the updated code for
level.gd
extends Node2D
@onready var coins = $Coins
# Called when the node enters the scene tree for the first time.
func _ready():
for i in coins.get_child_count():
coins.get_child(i).connect("coin_entered",coined)
func coined():
emit_signal("coin_funel")