+3 votes

So I followed Game Endeavor's tutorial on YouTube and managed to create a save file, but I have one problem: They didn't cover how to link the directory to the actual game. Example script:

extends Sprite

const SAVE_DIR = "user://saves/"

var path = SAVE_DIR + "playerInfo.dat"

func _on_Save_pressed():
    var defaultData = {
    "player" : {
        "name" : "Bob",
        "race" : "Human",
        "gender" : "Male",
        "hairC" : "Brown",
        "hairS" : "Combed"
        },
    "options" : {
        "master" : 50,
        "music" : 50,
        "sfx" : 50
        }
    }

    var dir = Directory.new()
    if !dir.dir_exists(SAVE_DIR):
        dir.make_dir_recursive(SAVE_DIR)

    var file = File.new()
    var error = file.open_encrypted_with_pass(path, File.WRITE, "$3GAM$E_/SAvE!1")
    if error == OK:
        file.store_var(defaultData)
        file.close()

What I want is for the data in defaultData to actually have an effect on the game. Help would be appreciated.

Godot version 3.3.2
in Engine by (309 points)

2 Answers

0 votes
func load():
var file = File.new()
if file.file_exists(save_path):
    file.open(save_path,File.READ)
    defaultData = file.get_var()
    file.close()

the order that you save is the order that you load

by (467 points)

That isn't what I asked. I asked how to implement the save/load function in the actual game. For instance, how does the save function detect the "health" of the player?

To implement these values, you need a class or function. In this function you can do something like setting player in his saved position or checkpoint. To do the you first have to extract or parse the JSON file. You can use Godot built-in JSON Parser function. It will extract and save all of your values in a dictionary ex. player_data .
Then (assume you are in the level script) you can write something like that,
$Player.position = player_data["position"]
It'll set player position.

The best thing to implement Save And Load, is to have an Autoload script. Then it'll set up music, SFX, level and player's data.

(My English is very bad. I'll be very happy if it helps you )

0 votes

The directory the file is saved is that user://, which is different depending on platform: Windows, Linux, Web. I know you can use ./ instead, like ./savefile.dat, to save wherever the game actually is, or ./saves/save1.dat, but I know it won't work for Web and I don't know if it'll work for Android.

To load, you'll have to do something like this:

var error = file.open_encrypted_with_pass(path, File.READ, "$3GAM$E_/SAvE!1") #Open it in READ mode
defaultData = file.get_var()
file.close()

Do note that anything that isn't in the defaultData struct won't be saved, so you'll want to throw anything else you want to save inside it. Health, items, score, unlocked flags, current map, etc.

by (294 points)
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.