0 votes

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():

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)

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())


func _physics_process(delta):

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

in Engine by (25 points)

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

1 Answer

0 votes

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)

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

_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.

by (1,789 points)
edited by
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.