invalid get index on base dictionary

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

I’m trying to set up a program where it loads the player’s position at the start of the level but when I try to get the data from the dictionary I saved it in I get this error.

invalid get index 'player_x' (on base: 'dictionary')

I’ve been loosely following this tutorial ( and my code seems to match up however I have made significant changes to suit my program.
how do I fix this?

relevant code:

code for scene:

    extends Node2D

onready var player = $player

onready var game_data = SaveFiles.game_data

func _ready():
	if SaveFiles.enter == true:
		player.position.x = game_data.player_x
		SaveFiles.enter = false
		player.position = Vector2 (970,300)

code in autoload:

func load_data():
	var file =
	if not file.file_exists(current):
		game_data = {
			"player_name": "farmer",
			"player_x": 100,
			"player_y": 100,
		save_data(), File.READ)
	game_data = file.get_var()
:bust_in_silhouette: Reply From: Inces

it looks very unstable
how do You know what is resolved first - onready var or ready() function ?
I am pretty sure dictionary is not loaded at a time your ready function requests its entry.
Above everything else You run save data at the same time ?

You need to make it simpler, so it is resolved one line after the other.
Make load_data return game_data and request it in ready :

func _ready():
        if file exists :
               var data = load_data()
               player.position.x = data.player.x