The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

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. :/

Godot version v3.4.2
in Engine by (24 points)

2 Answers

0 votes
Best answer

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

by (8,188 points)
selected by
0 votes

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”.

by (20 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.