Get_tree().change_scene_to_file error Parameter "data.tree" is null

Godot Version

4.2.1 stable

Question

I’m beginner in Godot, I’ve looked everywhere for a solution, but I can’t find one, someone says it’s a bug. Error is: E 0:00:07:0670 player.gd:47 @ _physics_process(): Parameter “data.tree” is null.
<C++ Source> scene/main/node.h:413 @ get_tree()
player.gd:47 @ _physics_process()

I wanna play animation of death then leave to menu. i write “await anim.animation_finished”. without this line of code, scene is changing. maybe it’s really bug of engine?

player.gd (end of the code is error)

extends CharacterBody2D


const SPEED = 300.0
const JUMP_VELOCITY = -400.0

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")

@onready var anim = $AnimatedSprite2D
var health = 100
var player_alive = true
func _physics_process(delta):
	if player_alive:
		if not is_on_floor():
			velocity.y += gravity * delta
		
		
		# Handle jump.
		if Input.is_action_just_pressed("ui_accept") and is_on_floor() and velocity.y >= 0:
			velocity.y = JUMP_VELOCITY
			anim.play("Jump")

		# Get the input direction and handle the movement/deceleration.
		# As good practice, you should replace UI actions with custom gameplay actions.
		var direction = Input.get_axis("ui_left", "ui_right")
		if direction:
			velocity.x = direction * SPEED
			if velocity.y  == 0:
				anim.play("Run")
		else:
			velocity.x = move_toward(velocity.x, 0, SPEED)
			if velocity.y  == 0:
				anim.play("Idle")
		if direction == -1:
			anim.flip_h = true
		elif direction == 1:
			anim.flip_h = false
		if velocity.y > 0:
			anim.play("Fall")
	if health < 0:
		health = 0
		player_alive = false
	if health == 0 and player_alive == false:
		anim.play("Death")
		await anim.animation_finished
		get_tree().change_scene_to_file("res://menu.tscn")
	move_and_slide()

skeleton.gd (there is no bugs, but just in case)

extends CharacterBody2D

var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")

var chase = false
var speed  = 100
var skeleton_hit = false
@onready var anim = $AnimatedSprite2D
var skeleton_alive = true

func _physics_process(delta):
	# Add the gravity.
	if not is_on_floor():
		velocity.y += gravity * delta
	var player = $"../../Player/Player"
	var direction = (player.position - self.position).normalized() 
	if skeleton_alive == true:
		if chase == true:
			velocity.x = direction.x * speed
			anim.play("Run")
		else:
			velocity.x = 0
			anim.play("Idle")
		if direction.x < 0:
			anim.flip_h = true
		else:
			anim.flip_h = false
		move_and_slide()
func _on_detector_body_entered(body):
	if body.name == "Player":
		chase = true
		
func _on_detector_body_exited(body):
	if body.name == "Player":
		chase = false


func _on_death_body_entered(body):
	if body.name == "Player" and body.velocity.y >= 0:
		body.velocity.y -= 300
		death()
		
func _on_death_2_body_entered(body):
	if body.name == "Player":
		if skeleton_alive == true:
			body.health -=40
		death()
	
func death ():
	skeleton_alive = false
	anim.play("Death")
	await anim.animation_finished
	queue_free()

i hope you will help me

You’re doing it the hard way; use this
Add this to your scene.

func death_animation_finished ():
get_tree().change_scene_to_file(“res://scene.tscn”)

After that, go to the death animation and Add_Track/Call_Method_Track
Next, select the node that has the script and connect death_animation_finished.
the function will run if the animation goes over that key.

I’m a little dumb and beginner and I don’t understand how to do it . so i add this function to my level script (or i need to do that in player script?)
i have death animation by animatedsprite2d and i don’t found add track/call method track
so i add animationplayer

and found this but i don’t know what to do after, can you help again please?)

Alright, if you’re using that, it’s much easier in your AnimatedSprite2D node. Go to Signals, and you’ll see the _animation_finished(). Connect that to the player.

func _on_animated_sprite_2d_animation_finished():
    if (player_alive == false):
        get_tree().change_scene_to_file("res://control.tscn")
1 Like

thank you bro it works

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.