#Attempting to create tween on null value when reloading scene

5 messages · Page 1 of 1 (latest)

warm badge
#

I recognize this probably isn't the best way to go about looping two tweens, but it resulted in a strange issue that I would like to understand better. When the scene reloads, it results in the error shown in the image. Can anyone explain why this is happening and what I should do to avoid it?

*Note; the tween times are set to 0 for testing purposes, they are normally 0.25

#

Tested a bit more, it may be that the code is trying to run while the scene is unloading the object?

severe fractal
#

Yes. The currently loaded scene is removed from the tree before it is freed, resulting in a single frame in which get_tree() will return null. What I'd recommend doing is putting both propertytweeners onto a single tween, and then call .set_loops() on that tween (with no number set). This will cause it to loop infinitely, or until killed/freed. Do note that you will need to set a delay greater than 0, otherwise it will be treated like infinite recursion and force killed after a few loops.

warm badge
#

So is awaiting tween.finished before creating a new tween a bad idea in general? While I was testing it happened in a similar setup (except that one wasn't in a while loop).

severe fractal
#

It can be, if the scene is going to be changed before that new tween is created. Otherwise the thread can resume after the scene is removed from the tree, causing that next line to fail.

Although, it might be worth trying what happens if you simply remove the get_tree() part. If you call create_tween() directly on a node the tween created is bound to that node, so it may be automatically freed when the node is being removed, which could solve your issue.