The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

+1 vote

I have been working with Godot for a couple of weeks now and I am new to game development. I decided that I wanted to make an interactive comicbook as my first proper game, and I have figured everything out so far as scene switching and I have even made a stat system for the player script, and now I only need to make a save system. I have read the Docs on save games, but as I said I am pretty new to Godot and gamedevelopment so it is a hard concept for me to grasp. I have made it so every page has its own scene so when you make a choice by pressing a button you switch page/scene, so what I want to do is to make a save and load system that remembers what scene you came to so when you press the continue button on the frontpage it loads the last page that you were on, any help or examples on how would be very much appreciated.

in Engine by (84 points)

1 Answer

0 votes

So this is kind of what I use for loading/saving to a save file:

extends Node

func save_data(save_path : String, data : Dictionary) -> void:
    var data_string = JSON.print(data)
    var file = File.new()

    var json_error = validate_json(data_string)
    if json_error:
        print_debug("JSON IS NOT VALID FOR: " + data_string)
        print_debug("error: " + json_error)
        return

    file.open(save_path, file.WRITE)
    file.store_string(data_string)
    file.close()

func load_data(save_path : String = "user://game.dat"):
    var file : File = File.new()

    if not file.file_exists(save_path):
        print_debug('file [%s] does not exist; creating' % save_path)
        save_data(save_path, {})


    file.open(save_path, file.READ)

    var json : String = file.get_as_text()
    var data : Dictionary = parse_json(json)

    file.close()

    return data

What I think you should do is just save something like {"last_scene_path": "res://path/to/scene.tscn"} whenever you load a new scene. Then, when you press the continue button, do something like:

var game_data = load_data()
some_method_to_change_scene(game_data.last_scene_path)
by (1,663 points)

My file looks kind of so:
{"1":{"a":"hello","b":["bye","paper"]},"2":{"material":"glass"}}
Your solution doesn't work anymore now. What do I have to do?

What does all_data[key] = new_data[key] exactly do?

Is it possible to do nothing if the wanted key doesn't exist without doing in every single script for every key this:

if load_data(user://game.dat).has("any_key"):
do_something()

I want to check for every wanted key if it exists in the save-load-script and if it doesn't it shouldn't do anything with it.

Is there a way to synchronize the variables and the file, so if I do first_var = 1, I don't have to do save_data("user://game.dat",{"first_var":1}) (or save_data("user://game.dat",{"first_var":first_var})) to have the same value at both places?

How to save something on Android? So what path must I need?

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.