How do I add an Enemy counter

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By HayJay

I was wandering if I can add a enemy counter so when I kill one it goes down.

If you want the code for the enemy AI:

extends KinematicBody2D

var motion = Vector2()

func _ready():
	pass # Replace with function body.

func _physics_process(delta):
	var Player = get_parent().get_node("Player")
	
	position += (Player.position - position)/50
	look_at(Player.position)
	
	move_and_collide(motion)

func _on_Area2D_body_entered(body):
	if "Bullet" in body.name:
		queue_free()
:bust_in_silhouette: Reply From: Wakatta

It may be best to connect your enermies to a signal and emit that signal in the _on_Area2D_body_entered(body): function

Or directly reference the counter

func _on_Area2D_body_entered(body):
    if "Bullet" in body.name:
        $Counter_node.counter -= 1
        queue_free()

Or an easier method would be to add your enemies to a group

And in your counter code let’s say it’s a Label node you can do

func _process(delta):
    text = get_tree().get_nodes_in_group("enemies").size()