Invalid get index on base dictionary

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

I’m trying to save many variables on a dictionary, but something’s wrong, I add 3 variables on the dictionary but only the second variable does not work, when I try to load this variable on another this error appears

Invalid get index 'the variable' (on base dictionary)

This happens whit me on other projects but I just change the name of the variable on the dictionary and work, but now I don’t know what’s the problem

The Code:

func save():
var data := {
	"dictuoinaruyadfs": Game.Monkes2,
	'Score': Game.score,
	'Monkes_inst': Game.Monkes
	

}

var save_file = File.new()
var error = save_file.open('res://Save/save.tres' , File.WRITE)
if error == OK:
	save_file.store_var(data)
	save_file.close()
else:
	print('ocorreu um erro ao salvar o arquivoo')

save_file.close()

  func lod():
    var save_file = File.new()
    var erro = save_file.open('res://Save/save.tres'  , File.READ)

if not erro:
	var saved_data = save_file.get_var()
	Game.score = saved_data['Score']
	Game.Monkes = saved_data['Monkes_inst']
	Game.Monkes2 = saved_data["dictuoinaruyadfs"]
	

	for position in Game.Monkes:
		var new_monkes = preload('res://Scenes/MONKE.tscn')
		var monke_spawn = new_monkes.instance()
		monke_spawn.position = position
		get_parent().add_child(monke_spawn)
	
	for position in Game.Monkes2:
		var new_monkes2 = preload('res://Scenes/Monke2.tscn')
		var monke_spawn2 = new_monkes2.instance()
		monke_spawn2.position = position
		get_parent().add_child(monke_spawn2)

	for position in Game.Monkes3:
		var new_monkes3 = preload('res://Scenes/Monke2.tscn')
		var monke_spawn3 = new_monkes3.instance()
		monke_spawn3.position = position
		get_parent().add_child(monke_spawn3)

else:
	print('erro ao carregar')
	
save_file.close()

The Variables:

var Monkes = []
var Monkes2 = []
var Monkes3= []

Ps: these variables are on the autoload, so it’s in another code

:bust_in_silhouette: Reply From: r.bailey

It’s hard to tell just by the code you have, but a quick assumption is that you should see what is in the dictionary from your lod method. Assuming this is where the issue is. Put a breakpoint around where I wrote breakpoint below, and see what data you have coming into saved_data. Is it what you are expecting? Is it empty? If so then maybe the key names don’t match up. Another possibility is that saving the data and reading it from the file isn’t done a proper way as well.

func lod():
save_file = File.new()
var erro = save_file.open('res://Save/save.tres'  , File.READ)
if not erro:
#breakpoint below. 
var saved_data = save_file.get_var()
Game.score = saved_data['Score']
Game.Monkes = saved_data['Monkes_inst']
Game.Monkes2 = saved_data["dictuoinaruyadfs"]

Hope this leads you in the right direction.

Thank’s for the help bro

Juwu | 2021-08-03 17:47

:bust_in_silhouette: Reply From: uniquegamesofficial

Instead of 'res://Save/save.tres' use "user://save", delete the 'res://Save/save.tres' and run the game again

Wtf??? it worked, why did it work?

Juwu | 2021-08-03 17:46

:bust_in_silhouette: Reply From: Losatu

In my case it was conversion to json and back from json changing the type in dictionary from int to string. While

my_dict[2]

worked before, I would have to use

my_dict["2"]

after.

1 Like