Why null instance on get_tree() ??

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

Sometimes i get the error :

“Attempt to call function create_timer in base null instance on null instance”
And it comes from yield(get_tree().create_timer(0.2),“timeout”)

What are the situations when get_tree() returns null ? Error happens in the middle of game, everything is ready for a long time, this code is not in Autoload… I use yield with dynamic timers everywhere in my code and it generally works

:bust_in_silhouette: Reply From: Bean_of_all_Beans

get_tree() will return null if whatever is calling it is not in the SceneTree. This can be fixed by simply adding the node to the SceneTree, then having the node call get_tree() wherever it needs to.

I see. Node calling yield responsible for this error actually was in scene tree, but could it mean that it queued itself free or was deparented while this 0.2 timer was going ? My code might have let such situation happen

Inces | 2021-03-11 19:48