How do I fix the 'Invalid get index' error?

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

I’ve got a saving system for my settings working and tried to add get other stuff to save but they didn’t work although the game would still run with no errors. After adding Save.load_data() to my _ready() func where the the variables are on the global script, I’ve starting getting the error;
"Invalid get index 'fullscreen_on' (on base: 'Dictionary')."
This error shows up in my global settings script in the first line of the _ready() func.

V global_settings.gd _ready() part V

func _ready():
	display_options_btn.select(1 if Save.game_data.fullscreen_on else 0)
	GlobalSettings.toggle_fullscreen(Save.game_data.fullscreen_on)
	master_volume_slider.value = Save.game_data.master_vol
	music_volume_slider.value = Save.game_data.music_vol 

V save.gd V

extends Node

const SAVEFILE = "user://SAVEFILE.save"

var game_data = {}

func _ready():
	load_data()

func load_data():
	var file = File.new()
	if not file.file_exists(SAVEFILE):
		game_data = {
			"fullscreen_on": true,
			"master_vol": 0,
			"music_vol": 0,
		}
		save_data()
	file.open(SAVEFILE, File.READ)
	game_data = file.get_var()
	file.close()
	

func save_data():
	var file = File.new()
	file.open(SAVEFILE, File.WRITE)
	file.store_var(game_data)
	file.close()

`here is the image of save.gd. can't get formatting to work

Could someone please help? I’m fairly new to this.

Edited to fix code formatting…

jgodfrey | 2023-02-13 04:19

:bust_in_silhouette: Reply From: jgodfrey

So, the error is pretty straightforward. Your Dictionary simply doesn’t contain a key named fullscreen_on. You’ll need to figure out why that is…

Thanks for the reply but the key is in the dictionary but it doesn’t seem to register. I have included a screenshot of the dictionary.

redteller | 2023-02-13 02:31

I’m confident that the key is not in the dictionary at the time it’s being accessed. It could be a timing issue, or something in your save logic, or ??. If you put a break point on the line where you’re getting the error and inspect the dictionary, I assume you’ll see that it does not contain the key in question…

jgodfrey | 2023-02-13 04:17