Hello! I am trying to make an Enemy counter for my top down shooter, but cannot get the enemy counter to tick down once one of the enemies is killed. I've tried many methods to get it to work,searched for ways to fix it, and gotten help from someone, but all the answers i but i still can get through this problem. I reverted back to my first attempt to solve this as it's the one i felt i can the closest on.
HUD code:
extends CanvasLayer
var enemies
var enemycount
func _ready():
enemies = get_tree().get_nodes_in_group("Enemies")
enemycount = enemies.size()
$Panel/Counter.text = String(enemycount)
print(enemycount)
func _on_Skirmisher_on_die_signal():
enemycount -= 1
print(enemycount)
Pellet code:
extends Area2D
var vector = Vector2.ZERO
var speed = 850
func _physics_process(delta: float) -> void:
position += transform.x * speed * delta
func _on_Timer_timeout():
get_node("Sprite").visible = false
get_node("CollisionShape2D").disabled = true
func _on_bullet_body_entered(body):
if body.is_in_group("Enemies"):
body.take_damage(body)
queue_free()
Global code:
extends Node
onready var HUD = get_node("/root/TestMap/HUD")
onready var enemy = preload("res://Enemies/Skirmisher.tscn")
var count = 0
func _process(delta):
enemy.connect("on_die_signal", HUD, "_on_Skirmisher_on_die_signal")
Skirmisher code:
func take_damage(body):
if "pellet" in body.name:
health = health - damage
if health <= 0:
on_die_signal()
print(health)
func on_die_signal():
queue_free()
remove_from_group("Enemies")
many thanks to anyone that tries to help