Topic was automatically imported from the old Question2Answer platform.
Asked By
StJ
I’ve a World scene with all the elements, one of them is an instance of a scene called “Bullet”, that has to delete one of the hearts that represent the lives every time that touches the player. ¿How can I delete them if it’s a different scene?
P.D: The script is not linked to the instance in the World scene, but in the scene’s root. This is because the code must be executed also by instances created by enemies.
What scene is the Heart in? I take it there is a parent scene that contains both the Heart and the World? I’m sure you can do what you want with signals but without the Node Tree it’s difficult to advise.
#Bullet.gd
func _ready():
connect("body_entered", self, "_on_unit_hit")
func _on_unit_hit(body):
#body will be player node or enemy node
var heart = body.get_node("Heart")
heart.queue_free()
Warning
You can get a reference to the node from root like this
var heart = get_node("/root/World/Enemy/Heart")
But that wont work in cases where you have multiple enemies
if you have multiple heart nodes on the Player/Enemy save them to a list
#Player.gd
onready var hearts = [$Heart1, $Heart2, $Heart3]
and reference them like this
if not hearts.empty():
var heart = hearts.pop_front()