Freezing for a moment while instancing preloaded scenes.

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

I’m working on a project follow a tutorial.

I’m trying to use Timer to set the time when a preloaded scene comes up on stage. It’s a scene consisting of 8 other instanced scenes (I’ve already preloaded it in the stage scenes). When the scene comes up, the game window freeze for a moment which is hard to notice. But if I instance a bunch of scenes like that, the freezing will be obvious.

Thanks in advance.

Stage scene’s scripts:

extends Node2D

var f_as1 = preload("res://elements/formations/f_alpha_straight_toptobot_ch1.tscn")
var aship = preload("res://elements/elements/e_enemy_alpha.tscn")

func _ready():

func _process(delta):
	yield($Timer, "timeout")
	var a = f_as1.instance()

IDK if i understood corectly, but what you are trying to achieve is to instance the preloaded scene everytime $Timer timeouts?

p7f | 2019-01-10 12:10

:bust_in_silhouette: Reply From: p7f

If what you are trying to achieve is to instance the preloaded sceen always timer timeouts, then you should not use yield inside process function. Yield will make process function stop process execution untill timeout happens, and thats the lag. You should connect the timer’s timeout signal to a _on_Timer_timeout() and instance the sceene there. Like:

func _on_Timer_timeout():
    var a = f_as1.instance()

Similar to what the “Your First Game” tutorial does for spawning mobs.

I’m just new to this engine and programming. Thanks for your help!

congbinh75 | 2019-01-10 17:11

You are welcome!

p7f | 2019-01-10 19:01