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

What's the output from the print(enemycount) statements?

This should only be done once:
enemy.connect("on_die_signal", HUD, "_on_Skirmisher_on_die_signal")
Placing it in _process() results in it getting executed every few milliseconds.

Does "Skirmisher" mean enemy?

This is unnecessary. queue_free() will do that:
remove_from_group("Enemies")

Are there any error messages?

    You may need to do another layer of abstraction to change your enemy count. Because a variable is a reference, it may be referencing it at the wrong time? It often helps me to split it up rather than add directly. Just my guess.

    Perhaps this small change....

    func _on_Skirmisher_on_die_signal():
            var _newEnemyCount = enemycount-1
            enemycount = _newEnemyCount
            print(enemycount)

    Doesn't look like the HUD is ever updated, have you tried adding $Panel/Counter.text = String(enemycount) at the end of _on_Skirmisher_on_die_signal?

      DaveTheCoder

      The print gives off the number, which is the correct amount of enemies i have in the test level, but when i kill one it does not print anything else

      Skirmisher is the enemy in this instance

      No, errors, just warnings about somethings not being used initially

      For some reason, some of the Pellet code got cut off when i copy pasted it, which happened to be the only relevant part to this

      func _on_bullet_body_entered(body):
      	if body.is_in_group("Enemies"):
      		body.take_damage(body)
      	queue_free()

      It looks like the HUD has to be updated, among other things. soundgnome has a point there. When the function _on_Skirmisher_on_die_signal() is called, it should update the Counter, like this:

      func _on_Skirmisher_on_die_signal():
          enemycount -= 1
          $Panel/Counter.text = str(enemycount)

      Maybe also change the Global thing like this:

      extends Node
      
      onready var HUD = get_node("/root/TestMap/HUD")
      onready var enemy = preload("res://Enemies/Skirmisher.tscn")
      var count = 0
      
      func _ready():
           enemy.connect("die", HUD, "_on_Skirmisher_on_die_signal")

      In the Skirmisher code, maybe change it to this:

      signal die
      func take_damage(body):
          if "pellet" in body.name:
      	health -= damage
      	if health <= 0:
                          emit_signal("die")
                          queue_free()

      Hope this helps.

      Sadly, I couldn’t get any of the methods worked, i will try to do this again at some point but for now i will move on with the project

      Thank you all for your help