I'm making an asteroid like game and I'm trying to build this function where if the enemy leaves the screen on the bottom, he will be ported to the top again (so enemies you dont kill are re-spawned on top)
func _on_viewport_exited(_viewport):
print("out of screen")
$RespwnTimer.start()
yield($RespwnTimer,"timeout")
var stage_width = get_viewport_rect().size
var n = rand_range(0, stage_width.x) #stage width
self.position = Vector2(n,-32) #port to top
print("respawned on top")
this works flawlessly... untill I shoot an enemy... and when he dies and queue_free()
is triggered, my respwn function is also triggered. The mob will correctly die, queue_free()
and will not respwn, but the on_viewport_exited()
will ALSO trigger and give an error that the timer doesnt exist (obviously)... and print "out of screen"
so... How do I queue_free()
my enemy without triggering _on_viewport_exited()
.. OR... how do I detect it left the viewport/screen that doesnt conflict with queue_free()
I understand that queueing_free makes the enemy leave the screen and viewport... so how should I go about this? I'm confused.
EDIT:
I added a boolean "died" before my death animation and it fixed the problem... but now when I reload the scene to start a new game (simple get_tree().reload_current_scene()
) it screams that the $respwntimer doesnt exist...
(E 0:01:14.838 start: Timer was not added to the SceneTree. Either
add it or set autostart to true. <C++ Error> Condition
"!isinsidetree()" is true. <C++ Source> scene/main/timer.cpp:111
@ start() Enemy.gd:84 @ onviewport_exited()
not sure whats going on... was this the right way to do it?
func _got_hit(): #gets hit by Player Bullet
died = true
$AnimationPlayer.play("death") #removes mob (queue_free on anim)
func _on_viewport_exited(_viewport):
if not died:
print("out of screen")
$RespwnTimer.start()
yield($RespwnTimer,"timeout")
var stage_width = get_viewport_rect().size
var n = rand_range(0, stage_width.x) #stage width
self.position = Vector2(n,-32) #port to top
print("respawned on top")