0 votes

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

Godot version v3.3.2 stable official
in Engine by (25 points)

2 Answers

+1 vote
Best answer

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

by (86 points)
selected by

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

0 votes

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.

by (162 points)

Thank's for the help bro

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 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 webmaster@godotengine.org with your username.