Hi everyone.
I am slowly starting to understand a few things about GDScript and Godot. More importantly, I feel I am understanding the error messages a bit better too!
I am currently following a tutorial about creating a shooter game with space ships. Since I feel that doing the tutorials as they are is a bit boring, I try to add as much extra stuff as I can (just to see if I can).
I have succeeded in creating what I wanted, but am getting an error. I come to you for advice on a workaround.
E 0:00:04.207 resume: Resumed function 'createlaser()' after yield, but class instance is gone. At script: res://scripts/enemyclever.gd:20
<C++ Error> Method failed. Returning: Variant()
<C++ Source> modules/gdscript/gdscript_function.cpp:1882 @ resume()
The problem lies in this part of the script. The original version had the enemy fire at fixed times and indefinitely. I wanted to change it to a more randomized version. The issue is that I am getting this error in the debugger (game works fine, btw. No crashes)
From what I understand, another function I have is erasing the enemy ship while the loop is happening. Correct? How do I solve this? There has to be something simple I am missing. :D
Learning is fun!
Regards!
func create_laser():
var times = [1,2,3]
var shots = 0
var cooldown = rand_range(1,3)
while shots!= times[randi() % times.size()]:
yield(get_tree().create_timer(cooldown),"timeout") #line 20
var laser = scn_laser.instance()
laser.set_position($cannon.get_global_position())
get_tree().get_root().call_deferred("add_child",laser)
shots += 1