Invalid set index 'animation' (on base: 'KinematicBody2D (player.gd)') with value of type 'String'.

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

Here is the code:

extends KinematicBody2D


func _ready():
	savex(position.x)
	savey(position.y)
	
func savex(content):
	var file = File.new()
	file.open("user://pos_x.dat", file.WRITE)
	file.store_line(str(content))
	file.close()
func savey(content):
	var file = File.new()
	file.open("user://pos_y.dat", file.WRITE)
	file.store_line(str(content))
	file.close()

func loadx():
	var file = File.new()
	file.open("user://pos_x.dat", file.READ)
	var content = file.get_line()
	file.close()
	print(content)
	position.x = float(content)
func loady():
	var file = File.new()
	file.open("user://pos_y.dat", file.READ)
	var content = file.get_line()
	file.close()
	print(content)
	position.y = float(content)



export var jump_impulse = -20
const GRAVITY = 10700
export (int) var speed = 200
var velocity = Vector2()
var jumping = false
func get_input():
	velocity = Vector2()
	if Input.is_action_pressed("ui_right"):
		velocity.x += 1
	if Input.is_action_pressed("ui_left"):
		velocity.x -= 1
	if Input.is_action_pressed("ui_accept"):
		velocity.y -= 1
	velocity = velocity.normalized() * speed

func _physics_process(delta):
	get_input()
	velocity = move_and_slide(velocity)
	if Input.is_action_pressed("ui_right"):
		$AnimatedSprite.animation = "right"
		$AnimatedSprite.play()
		velocity.x += speed
	elif Input.is_action_pressed("ui_accept"):
		$AnimatedSprite.animation = "jump"
		$AnimatedSprite.play()
		jumping = true
		velocity.y = jump_impulse
	elif Input.is_action_pressed("ui_left"):
		$AnimatedSprite.animation = "left"
		$AnimatedSprite.play()
		velocity.x -= speed
	else:
		$AnimatedSprite.animation = "idle"
		$AnimatedSprite.play()
	get_input()
	velocity.y += GRAVITY * delta
	if jumping and is_on_floor():
		jumping = false
	velocity = move_and_slide(velocity, Vector2(0, -1))


func _on_Area2D_body_entered(body):
	# print("COLL")
	print("Whoa, ur so cool u won.")
	savex(position.x)
	savey(position.y)
	get_tree().change_scene("res://level_2.tscn")
	

Note: This error only occured when I autoloaded the Player as a singleton. Otherwise, the script works fine. :confused:

:bust_in_silhouette: Reply From: japhib

When you load a script as a singleton, it doesn’t load a scene, it just loads that one script. So you can’t access other nodes in the way you’re used to. So currently in your code you call $Animator.animation = “jump” but that doesn’t work because the auto load script doesn’t have any child called “Animator”.

:bust_in_silhouette: Reply From: Inces

Autoload script resides away of the scene tree. It cannot have children, so it will never get reference to $AnimatedSprite. Code like this should not be Autoloaded