Topic was automatically imported from the old Question2Answer platform.
Asked By
Exile152
I have a enemy and a bullet, the enemy is on the main scene but the bullet is on another scene, my bullet deals 2 damage and the enemy has 4 health how can i make it when the bullet enters the area it deals damage? also how can i get the var from the bullet?
onready var Bullet1 = preload("res://Bullet.tscn")
func _on_ContatoSlime_body_entered(body):
if body.name == "Bullet1":
enemylife = enemylife - Bullet1.Damage
Will only work for the first instance of the Bullet. Godot will name subsequent instances something crazy like @@Bullet2@@ (open the remote debugger while your game is running to see exactly what they’re named).
The best way to handle this is to let the engine handle the work for us. Add add_to_group("bullets") in the _ready() function of your bullets. Then your check becomes if body in get_tree().get_nodes_in_group("bullets"):
Another way you could handle this is adding class_name Bullet to the top of your bullet script then checking: if body is Bullet:. This is a tad more performant. I wanted to show you both ways because they both have their merits.
Once you have determined that your body is a bullet using one of the above methods change: