Making a counter for number of spawned enemies killed

Godot Version

4.2

Question

I used the code below to spawn mobs on a Path2D Node every time a timer timeouts and I tried to add a counter for how many mobs died by changing the code on the mob.gd so that a count would go up every time it dies but that only went up to 1, and I’m guessing that’s because its technically the same scene being instantiated. Is there anyway to fix it?
The code I used in main.gd to spawn mobs:
func spawn_mob(): var new_mob = preload("res://mob.tscn").instantiate() %PathFollow2D.progress_ratio = randf() new_mob.global_position = %PathFollow2D.global_position add_child(new_mob)

func _on_timer_timeout(): spawn_mob()

just use signal, connect it from the main controller/game controller. emit the signal from mob.gd so the main controller/game controller received the signal and increase the counter. which mean the counter variable stayed on main controller/game controller

I already have a signal in mob.gd that emits when the mob dies but how would I connect that to the main scene since the mob scene is a different one and only gets instantiated into main

You get a ref to main (while inside mob) and then emit on that.

main

signal madspawn
func _ready():
  madspawn.connect(HeyYo)

func HeyYo():
  #a mob spawned
  pass

mob

var MAIN = $main # or get_child(), or an export you set

func spawner():
  #..
  MAIN.madspawn.emit()

when you instantiated it, connect the signal to a function/callable in the main controller

example, the mob.gd has signal died
then right after you instantiate it the mob scene, you connect it
something like this

var new_mob=mob_scene.instantiate()
new_mob.died.connect(increase_counter_function)
add_child(new_mob)

I tried using this method but it gives me an error:
“Invalid get index ‘died’ (on base: ‘CharacterBody2D(mob.gd)’)”

But as you said mob.gd has a signal died and it gets emitted when the mob dies.

if (health == 0):
		const SMOKE = preload("res://smoke_explosion/smoke_explosion.tscn")
		var smoke = SMOKE.instantiate()
		add_sibling(smoke)
		smoke.global_position = global_position
		dead.emit()
		get_tree().call_group("counter", "counterplus")
		queue_free()

This would increase the counter when a mob gets spawned, but I’m trying to increase it when it gets killed.
Thanks for the code though I’ll try to use it!

in here it’s dead.emit(), not died.emit()
also what’s your signal name ?

the signal name is dead and i rewrote everything for the signal and I’m getting a new error now so it was prob a typo before.

E 0:00:02:0434 main.gd:6 @ spawn_mob(): Cannot connect to ‘dead’: the provided callable is null.
<C++ Error> Condition “p_callable.is_null()” is true. Returning: ERR_INVALID_PARAMETER
<C++ Source> core/object/object.cpp:1315 @ connect()
main.gd:6 @ spawn_mob()
main.gd:16 @ _on_timer_timeout()

this doesnt work?
show the line for the error?

this is what i used to spawn the mob

func spawn_mob():
	var new_mob_skeleton = preload("res://mob.tscn").instantiate()
	new_mob_skeleton.dead.connect(counter.counterplus())
	%PathFollow2D.progress_ratio = randf()
	new_mob_skeleton.global_position = %PathFollow2D.global_position
	add_child(new_mob_skeleton)

and in this the new_mob_skeleton.dead.connect(counter.counterplus()) code is the one giving me the error

change to this
new_mob_skeleton.dead.connect(counter.counterplus)

I’m not getting the error anymore but the counter isn’t changing

what is counter?
if it’s a node you referenced, and it has script with counter_plus method, then it should work

if counter_plus method is in the same script of this, then just connect(counter_plus) will do

its a label in the main scene with label.gd with this code

extends Label
var counter = 0

func _ready():
	text = str(counter)

func counterplus():
	counter += 1

is there anyway to just connect the signal dead directly to the counter so that when the signal is emitted i can use the counterplus func

it would be easier if you have an Event Bus global AutoLoad to handle the signal
so you wont need to route it from enemy to spawn to counter, something like that
or have to make the enemy reference the counter label

with the EventBus Autoload:
it would be like
event_bus.gd

signal dead

enemy.gd

EventBus.emit_signal("dead")

counter.gd

func _ready():
	EventBus.dead.connect(counterplus)

see how it skipped the mob spawn script and just straight connect to where it needs via the event bus

I see so I’ll just make a new gd script file and put just the signal there.

need to set it as AutoLoad, follow the link i sent (the gdscript .com), it told how to do it

You haven’'t to update text of that label.
Change counterplus to this:

func counterplus():
    counter += 1
    text = str(counter)