Waiting one frame in Godot 3.0

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By adamGodot
:warning: Old Version Published before Godot 3 was released.

Hello!
I am implementing a camera shake in my game. In the script i need to wait one frame. I know you could achieve it by calling yield(get_tree(),"idle_frame") but I am getting this error “First argument of yield() is null”. Here is the whole script:

 extends Camera2D

var magnitude = 0
var timeLeft = 0
var is_shaking = false

func _ready():
	pass

func shake(new_magnitude, lifetime):
	if magnitude > new_magnitude: return
	
	magnitude = new_magnitude
	timeLeft = lifetime
	
	if is_shaking: return
	is_shaking = true
	
	while timeLeft > 0:
		var pos = Vector2()
		pos.x = rand_range(-magnitude, magnitude)
		pos.y = rand_range(-magnitude, magnitude)
		position = pos
		
		timeLeft -= get_process_delta_time()
		yield(get_tree(),"idle_frame")
		
	magnitude = 0
	is_shaking = false
	position = Vector2(0,0)
	pass

The camera is attached to the World node in the main/game scene. I am calling the shake function in other scripts (like enemy script)

Thank you

get_tree() returns SceneTree only when the node is added to scene tree.
is there any chance to call shake function before it’s added to scene?

volzhs | 2018-01-29 03:09

I have this same question. I’m following the same tutorial and the function shake needs to wait 1 frame. But the line yield(get_tree(),"idle_frame") is from godot 2.14. Is there something similar for godot 3??

Thanks!

arthurZ | 2018-02-20 22:18

Is there a way to solve this First argument of yield() is null error while trying to use yield(get_tree(),"idle_frame")?

arthurZ | 2018-02-21 00:10

I think you’re calling it too soon when it’s not inside the scene tree.
Where do you call it? Probably in a setter or getter?
Check is_inside_tree().

Dlean Jeans | 2018-02-21 03:09

@arthurZ I moved your answer to comment because it’s not an answer.

volzhs | 2018-02-21 06:02

:bust_in_silhouette: Reply From: Lisandro Lorea

Just add a guard and bail out if get_tree() is null

if not get_tree():
   return

The proper solution would be finding out why the shake function is being called before the node is added to the tree.