Trouble loading the 2d player position, like F9 does not see the Player as loaded

Godot Version



I am having trouble loading the 2d player position.
I can run the game and move the player around, but when i click F9 to update the player location to a value i pulled from a save file, I get terminal output like the player is not loaded.
What am i doing wrong?


The F9 key press runs this code in

		elif Input.is_action_just_released("Load"):
			print("loading from Input.is_action_just_released(Load)")
			print("load attempt complete from Input.is_action_just_released(Load)")

Here is the related code with that $“/root/World”.load_game() function :

extends Node

const Global = preload("res://")
const myplayer = preload("res://")
@onready var sprite_2d: Sprite2D = $Player/Sprite2D
@onready var player : CharacterBody2D = $Player

func load_game():
	print("starting load_game")
	if not (player):
		print("there is no player in world load_game")
		print("in world load_game player.global_position is >  "+str(player.global_position))
	if not FileAccess.file_exists("user://"):
		print("failed to load user://")
	var save_game ="user://", FileAccess.READ)
	while save_game.get_position() < save_game.get_length():
		var json_string = save_game.get_line()
		var json =
		var parse_result = json.parse(json_string)
		var node_data = json.get_data()
			print("loading player x")
			$Player.global_position.x = node_data["positionx"]
			print("loading player y")
			$Player.global_position.y = node_data["positiony"]
			print("no player to update in load_game()")

Here is the terminal output that says the player is not loaded.

loading from Input.is_action_just_released(Load)
starting load_game
there is no player in world load_game
{ “positionx”: 105.187812805176, “positiony”: 51.8545112609863, “score”: 0 }
no player to update in load_game()
load attempt complete from Input.is_action_just_released(Load)

Here in that code in a Public github repository:

Here is a screenshot of the Autoload:

Here is a screenshot of the Node structure:


I googled and worked on this for 2 days :face_with_head_bandage:
Any hints, links or code snippets would be awesome.

Thank you

Do you have world as an autoload-script and as an script to a scene? I think this causes trouble

1 Like

Hi, thanks for the reply!
I just tried removing from autoload.
The game still ran but the issue persisted.

I don’t understand how the player object can be moving around with the WASD keys but the player object is not there when I press F9 to run that $“/root/World”.load_game() function. It’s so weird.

Here is the updated autoload settings:

I tried the little trash can icon instead of just unchecking Enabled.
That fixed it! Thanks!

Is there a tutorial about when to use @onready/preload and when to autoload scripts?

Here is the autoload setting that fixed it.

Autoloads are only meant for scripts that are loaded the whole time the game is running. They usually are used for keeping data between scenes. I recommend to only use autoloads if neccessary, i. e. for my networking i use a autoload because the networking informations are neccessary in all scenes and a player for example only exists during the game and not during the main menu thats why my player is just a normal scene and not an autoload

1 Like