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)
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
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!
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