How to make a Enemy counter for a HUD?
Asked Answered
M

7

0

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

Methodius answered 5/2, 2023 at 2:31 Comment(0)
S
0

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?

Skricki answered 5/2, 2023 at 4:38 Comment(0)
C
0

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)
Chapman answered 5/2, 2023 at 17:20 Comment(0)
C
0

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?

Congregate answered 5/2, 2023 at 21:35 Comment(0)
M
0

Skricki

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

Methodius answered 5/2, 2023 at 23:22 Comment(0)
M
0

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()
Methodius answered 5/2, 2023 at 23:30 Comment(0)
Y
0

It looks like the HUD has to be updated, among other things. Congregate 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.

Yardman answered 6/2, 2023 at 2:13 Comment(0)
M
0

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

Methodius answered 9/2, 2023 at 20:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.