Resumed function "_process(" after yield, but class instance is gone. At script: "*script_name*"

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

There’s a code

 #---------------------------------FIRST VER-----------------------#
    func _process(delta):
    	yield(get_tree().create_timer(1), "timeout")  #-----> not necessarily
    	$lighter/AnimationPlayer.play("light_break")

Works perfectly well until I try to start from “menu” scene. Player starts lag and nothing is happens on scene. That’s in second room, first works brilliant. And it also works perfectly when player moves from scene to scene.

Also i tried to give other scene and start from the menu, but… then player doesn’t even contact with it.
here’s the trigger code:

func _physics_process(delta):
	var bodies = get_overlapping_bodies()
	for body in bodies:
		if body.name == "frisk":
			get_tree().change_scene("*some_scene*")

What can i do with it?

To help with the “lagging” part, instead of using get_overlapping_bodies(), use signals instead. The function get_overlapping_bodies() can be process-intensive, and so can lead to sluggish frames.

Ertain | 2021-04-14 20:04

Agreed get_overlapping_bodies() should only ever be used as a test case and not in the way it is here

Wakatta | 2021-04-14 23:41

thanks a lot

pafos_kota | 2021-04-16 13:39

:bust_in_silhouette: Reply From: exuin

The _process function is called every frame. You shouldn’t be using “yield” in it. I’m not sure what the script is on, but based on the error it’s on a node that’s freed when the scene is changed, so you shouldn’t do that.

You also shouldn’t check the overlapping bodies each frame either. Instead, go into the signals tab and connect the “body_entered” signal and check the body there.