problem in instancing light2D in a loop

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

hi guys i have a node named enemy-fire and it has a light2d as a child …
i have enemies in my game that shoot randomly between (0.5 , 1.5) seconds…
and each time i instance one enemy_fire and then each time the enemy_fire goes out of screen or hit player or walls they will be queue_free()… but there is a problem each time i instance a new enemy_fire , a tiny lightning happens at the top left corner of the screen…
i do the same for my player but each time Input.is_action_just_pressed() happens and its ok i did the same for enemy and its ok but only when i put it in a loop to shoot this problem happens and i dont know why ?!
here is the code …

var FIREBALL = preload("res://Fireball/Enemy_fireball.tscn")

.
.
.

func shoot():
var direction = true
while true:
	var fireball = FIREBALL.instance()
	if direction == true :
		fireball.direction = -1
	if direction == false:
		fireball.direction = 1
	fireball.position = $Position2D.global_position
	fireball.visible = true
	get_parent().add_child(fireball)
	yield(get_tree().create_timer(rand_range(1 , 1.5)) , "timeout")
	direction = !direction
pass

and shoot() function will be called in func _ready()
can anybody help?
thanks!

You could in my opinion use _process() or _physics_process(), or better you could create a timer in the gui (and maybe disable the one_shot), connect its signal and start the timer again from there (Timer — Godot Engine (3.1) documentation in English). I would try to avoid endless while loops. However, i’m quite new too and I don’t see how it’s related to the lighting in the top left. It may very well be because it isn’t synced with the engine’s other parts. Is it only there for a single frame?

1234ab | 2020-03-25 14:51

thanks!
i used timer and its OK now!!
but i still don’t know what was the problem that was in a loop and this depends on timer
but as u can see i put delay in while loop too,
anyway really thank you for spending your time on my question
and… something else
i uploaded the video of my problem here .

abolfazl98 | 2020-03-25 16:00

It looks really strange. I think it helped that you used a timer because the engine knows when exactly it can call the timer’s code, but with yield it may be executed at a bad time and while the node was initializing the frame drawing happened. I’m happy that you were able to solve the problem.

1234ab | 2020-03-25 16:10

actually its more stranger now !!
i use yield in shoot() function … when timer times out it call shoot() and for making random shooting in my game i use the same yield code in my shoot function and there is no problem!!!
yeah i think you are right ! system can handle timer better than random delays in always true while loop !!
thanks again!!

abolfazl98 | 2020-03-25 20:19