Variables not updating in dictionary...

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

So I’m working on a game and I followed this video to try and implement a save system. However, it is not updating. Here is my global script code, any help appreciated as I am a newbie.

extends Node2D

#default values
var jayson_index = 0
var brock_index = 0
var steven_index = 0
var jodi_index = 0
var gabe_index = 0
var town_index = 0
var jaysondoor_index = 0
var world_index = 0


func update_jayson_index():
	jayson_index = jayson_index + 1
func update_brock_index():
	brock_index = brock_index + 1
func update_steven_index():
	steven_index = steven_index + 1
func update_jodi_index():
	jodi_index = jodi_index + 1
func update_gabe_index():
	gabe_index = gabe_index + 1
func update_town_index():
	town_index = town_index + 3
func update_jaysondoor_index():
	jaysondoor_index = jaysondoor_index + 1
func update_world_index():
	world_index = world_index + 1 

var game_data : Dictionary

func _ready():
	update_data()
	pass

func update_data():
	game_data = { "playerdata": 
	{"jayson": jayson_index,
	"brock": brock_index,
	"steven": steven_index,
	"jodi": jodi_index,
	"gabe": gabe_index,
	"town": town_index,
	"jaydoor": jaysondoor_index,
	"world": world_index}}

func do_save():
	var file: File = File.new()
	file.open("res://saved_game/game.dat", File.WRITE)
	file.store_line(to_json(game_data))
	file.close()

func do_load():
	var file : File = File.new()
	
	file.open("res://saved_game/game.dat", File.READ)
	
	game_data = parse_json(file.get_as_text())
	
	file.close()

func _physics_process(delta):
	if(Input.is_action_just_pressed("save_key")):
		do_save()

I use the update functions in different scripts but it doesnt change the value in the dictionary… when i do_save()

Not sure but I think that might be the problem.

func do_save():
var file: File = File.new()

func do_load():
var file : File = File.new()

you opened two files
Saving games — Godot Engine (stable) documentation in English
or
https://forum.godotengine.org/80759/how-to-save-objects-to-a-file-with-json

ramazan | 2022-01-20 08:53

:bust_in_silhouette: Reply From: DaddyMonster

First, a little tip: All these var brock_index/ update_brock_index pairs could be covered with a single dict and a single method. Something like this:

var indices = {"Brock": 0, "Jaysen": 0, "etc": 0}

func increment_index(idx):
    indices[idx] += 1

It’s just more concise. You don’t need update_data or the second dict game_data either.

Honestly, I wouldn’t use json, it’s a headache. You can just save the variable like this:

func do_save(data, path):
    var file: File = File.new()
    file.open(path, File.WRITE)
    file.store_var(data)
    file.close()

func do_load(path):
    var file : File = File.new()
        if file.file_exists(path):
            file.open(path, File.READ)
            indices = file.get_var()
            file.close()

_physics_process is called 60 times a second and saving is slooooow. So, let’s do this instead:

func _input(event):
    if event.is_action_pressed("save_key"):
        do_save(indices, "res://saved_game/game.dat")

Obviously you need to call increment_index("Brock") for the value to change before you save.

Hope it put you on the right lines.