0 votes

extends Node

const FILE_PATH = "user://game.data"

var Coins = 1000

var EXPLODE = 0

var iEXPLODECINCO = 0

var best_score = 0

var last_score = 0

var Language = false

var Sound = false

var BLOCK = false

var Padrao = true
var Bov = false
var Bri = false
var Ya = false
var Peb = false
var Pan = false
var Ninja = false
var Alien = false

var JATPadrao = true
var JATBov = false
var JATBri = false
var JATYa = false
var JATPeb = false
var JATPan = false
var JATNinja = false
var JATAlien = false

var BUYBov = false
var BUYBri = false
var BUYYa = false
var BUYPeb = false
var BUYPan = false
var BUYNinja = false
var BUYAlien = false

var BUYJATBov = false
var BUYJATBri = false
var BUYJATYa = false
var BUYJATPeb = false
var BUYJATPan = false
var BUYJATNinja = false
var BUYJATAlien = false

var GAMEDATA = {
Coins = 0,
EXPLODE = 0,
i
EXPLODECINCO = 0,
best
score = 0,
last_score = 0,
Language = false,
Sound = false,
BLOCK = false,
Padrao = true,
Bov = false,
Bri = false,
Ya = false,
Peb = false,
Pan = false,
Ninja = false,
Alien = false,
JATPadrao = true,
JATBov = false,
JATBri = false,
JATYa = false,
JATPeb = false,
JATPan = false,
JATNinja = false,
JATAlien = false,
BUYBov = false,
BUYBri = false,
BUYYa = false,
BUYPeb = false,
BUYPan = false,
BUYNinja = false,
BUYAlien = false,
BUYJATBov = false,
BUYJATBri = false,
BUYJATYa = false,
BUYJATPeb = false,
BUYJATPan = false,
BUYJATNinja = false,
BUYJATAlien = false
}

func _ready():

load_game()

func save_game():

var file = File.new()

file.open_encrypted_with_pass(FILE_PATH, File.WRITE, OS.get_unique_id())

var save_dados = GAME_DATA

save_dados.Coins = Coins
save_dados.EXPLODE = EXPLODE
save_dados.i_EXPLODE_CINCO = i_EXPLODE_CINCO
save_dados.best_score = best_score
save_dados.last_score = last_score
save_dados.Language = Language
save_dados.Sound = Sound
save_dados.BLOCK = BLOCK
save_dados.Padrao = Padrao
save_dados.Bov = Bov
save_dados.Bri = Bri
save_dados.Ya = Ya
save_dados.Peb = Peb
save_dados.Pan = Pan
save_dados.Ninja = Ninja
save_dados.Alien = Alien
save_dados.JATPadrao = JATPadrao
save_dados.JATBov = JATBov
save_dados.JATBri = JATBri
save_dados.JATYa = JATYa
save_dados.JATPeb = BUYJATPeb
save_dados.JATPan = JATPadrao
save_dados.JATNinja = JATNinja
save_dados.JATAlien = JATAlien
save_dados.BUYBov = BUYBov
save_dados.BUYBri = BUYBri
save_dados.BUYYa = BUYYa
save_dados.BUYPeb = BUYPeb
save_dados.BUYPan = BUYPan
save_dados.BUYNinja = BUYNinja
save_dados.BUYAlien = BUYAlien
save_dados.BUYJATBov = BUYJATBov
save_dados.BUYJATBri = BUYJATBri
save_dados.BUYJATYa = BUYJATYa
save_dados.BUYJATPeb = BUYJATPeb
save_dados.BUYJATPan = BUYJATPan
save_dados.BUYJATNinja = BUYJATNinja
save_dados.BUYJATAlien = BUYJATAlien

file.store_line(to_json(save_dados))

file.close()

func load_game():

var file = File.new()

if !file.file_exists(FILE_PATH):
    print("ERROR")
    return

else:

    var save_dados = {}

    file.open_encrypted_with_pass(FILE_PATH, File.READ, OS.get_unique_id())

    while(!file.eof_reached()):

        var DADOS = file.get_line()

        save_dados = parse_json(DADOS)

        best_score = save_dados["best_score"] #ERROR 

        Coins = save_dados["Coins"]

        EXPLODE = save_dados["EXPLODE"]

        i_EXPLODE_CINCO = save_dados["i_EXPLODE_CINCO"]

        Sound = save_dados["Sound"]

        Language = save_dados["Language"]

        BLOCK = save_dados["BLOCK"]

        Padrao = save_dados["Padrao"]
        Bov = save_dados["Bov"]
        Bri = save_dados["Bri"]
        Ya = save_dados["Ya"]
        Pan = save_dados["Pan"]
        Peb = save_dados["Peb"]
        Ninja = save_dados["Ninja"]
        Alien = save_dados["Alien"]
        JATPadrao = save_dados["JATPadrao"]
        JATBov = save_dados["JATBov"]
        JATBri = save_dados["JATBri"]
        JATYa = save_dados["JATYa"]
        JATPan = save_dados["JATPan"]
        JATPeb = save_dados["JATPeb"]
        JATNinja = save_dados["JATNinja"]
        JATAlien = save_dados["JATAlien"]

        BUYBov = save_dados["BUYBov"]
        BUYBri = save_dados["BUYBri"]
        BUYYa = save_dados["BUYYa"]
        BUYPan = save_dados["BUYPan"]
        BUYPeb = save_dados["BUYPeb"]
        BUYNinja = save_dados["BUYNinja"]
        BUYAlien = save_dados["BUYAlien"]
        BUYJATBov = save_dados["BUYJATBov"]
        BUYJATBri = save_dados["BUYJATBri"]
        BUYJATYa = save_dados["BUYJATYa"]
        BUYJATPan = save_dados["BUYJATPan"]
        BUYJATPeb = save_dados["BUYJATPeb"]
        BUYJATNinja = save_dados["BUYJATNinja"]
        BUYJATAlien = save_dados["BUYJATAlien"]

file.close()
in Engine by (15 points)

1 Answer

0 votes
Best answer

this is what your while block should look like:

var save_dados = {}
var DADOS = ""

while(!file.eof_reached()):
   DADOS = DADOS + file.get_line()

save_dados = parse_json(DADOS)

# now you can do all of your variable assignments

The problem is that you try to parse the json before you've finished reading the file

by (1,656 points)
selected by

Thank you, everything is working properly now.

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 Frequently asked questions and 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 [email protected] with your username.