Saving with JSON doesn't write the current variable data

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

this is how looks my Global script:

extends Node

# Game Data

var score = 0
var hp = 100
var bullets = 10
const magazin = 20

var max_trees = 300
var max_zombies = 350
var max_ammo = 75
var max_heal = 25

var kill = 0

var player_position

# Save system

var gamedata = {
	"player" : {
		"score" : score,
		"hp" : hp,
		"bullets" : bullets,
		"zombies" : max_zombies,
		"ammount" : max_ammo,
		"apteca" : max_heal,
		"position" : player_position

var file =

func save():'user://', File.WRITE)


func load():'user://', File.READ)
	var json = parse_json(file.get_as_text())
	score = int(json.player.score)
	hp = int(json.player.hp)
	bullets = int(json.player.bullets)

And, even after LONG playing it prints me this:

** Debug Process Started **
Godot Engine v3.1.2.stable.official -
OpenGL ES 3.0 Renderer: Gallium 0.4 on llvmpipe (LLVM 3.8, 128 bits)
{player:{ammount:75, apteca:25, bullets:10, hp:100, position:Null, score:0, zombies:350}}
** Debug Process Stopped **

So, saving gamedata WORKS, but not correctly… Why does it happen???

:bust_in_silhouette: Reply From: Ryukuzo

As I’m new to godot, I could probably be wrong but here’s my answer.

The possible mistake is here : var json = parse_json(file.get_as_text())
I think you should define a new dictionnary, for example :

var gamedata = {}

Then, put the result of the json file into this dictionnary :

gamedata = json.result

Now you can close the file as you don’t need it anymore. You can immediatly reach your data by referring to your dictionnary. For example :

var score = gamedata.player.score

Tell me if it solved the problem :wink:

Thanks for answer, but problem is not there. The problem is in function save, not load.
As the debug is writing data of gamedata and there is not changed data of player, it means that file.store_line(to_json(gamedata)) saves primordial data. I don’t think, that it can be because all changing happens in the other script. it’s like:

func shot():
	if (!G.bullets <= 0):
		G.bullets -= 1        #this changing data

it really changing data, but may it be because the save function is in Global script?
I don’t think so, because it worked if i write json as a “string” in G script.
Or may it be also because name of the file ends in .save, not .json?

GoodCrafting | 2020-05-05 19:21

When you open your file do you see the data stored in there ?

If yes, that means either the problem comes from the load function, or that your not calling your save function.

If not, either try using a .json file and not a .save file, or change the file name. Maybe your system doesn’t create files with a “-”

Ryukuzo | 2020-05-07 13:37

No, it doesn’t work anyway. I’m trying it already two months. I’m new in godot too, so I don’t know what exactly I have to do. Can I send somewhere my whole project with all script?

GoodCrafting | 2020-05-08 16:32