I don't understand the error

Godot Version

godot 4.4.1

Question

I don’t understand this errore "E 0:00:23:521 player.gd:34 @ _process(): Node not found: “AnimatedSprite2d” (relative to “/root/Player”).
<Erreur C++> Method/function failed. Returning: nullptr

scene/main/node.cpp:1877 @ get_node() player.gd:34 @ _process()" here is my program: `
 extends CharacterBody2D

var hero = []
 
@export var speed = 0
func _ready():
	hide()
	pass

func class_player(type):
	hero = Personnage.extract_class(type)
	speed = hero[type][0]["characteristique"][0]["speed"]
	print("Vitesse :", speed)
	print(hero)
	

func _process(delta):
	var velocity = Vector2.ZERO
	
	if Input.is_action_pressed("walk down"):
		velocity.y +=0.5
	if Input.is_action_pressed("walk up"):
		velocity.y -=.5
	if Input.is_action_pressed("walk right"):
		velocity.x +=.5
	if Input.is_action_pressed("walk left"):
		velocity.x -=.5
		
	
	
	if velocity.length() >0:
		velocity = velocity*speed;
		$AnimatedSprite2D.play("walk_side")
		$AnimatedSprite2D.flip_h = velocity.x > 0
	else :
		$AnimatedSprite2D.stop()
	
func start():
	show()
	$CollisionShape2D.disabled = false


`

plus my scene

can you help me :folded_hands:

Did you make your player script a Global?

1 Like

yes why

Making your Player node an Autoload will effectively load it twice into the scene:

  1. Inside your main scene, as it would normally
  2. Under the root node, as an empty Node with just the player.gd script, because you set the player.gd script as an Autoload (see the error message refering to the Player node under root (relative to “/root/Player”))

Because the autoload node is empty, it doesn’t have any of the children you set up on your player node initially, like $AnimatedSprite2D, so now any pointers related to these will just return null - and this is the error you’re having.

Just remove the player from your Autoload setup. Basically Player should never be an Autoload, this seems like a bad practice. Setting only a .gd script file as an autoload is also a bad practice, you should make a .tscn scene file an autoload instead, if you need it (but not the Player).

What’s the reason you set the Player up as an Autoload? I’m sure we can find a better solution for that.

1 Like