Save and load wont work

so im making a save and load system in my game and the player does not go back to the position same with everything else i placed a print function and it never did print so mesly needs halp :frowning:
the code
extends Control
var config = ConfigFile.new()
var err = config.load(“res://l1.tscn”)

Called when the node enters the scene tree for the first time.

func _ready() → void:
if Input.is_action_just_pressed(“save”):
config.set_value(“player”,“x”, round($player.position.x))
config.set_value(“player”,“y”, round($player.position.y))
config.save(“res://l1.tscn”)
config.set_value(“money”,“x”, round($money.position.x))
config.set_value(“money”,“y”, round($money.position.y))
print(position)
var err = config.load(“res://l1.tscn”)
if Input.is_action_just_pressed(“load”) and err == OK:
$player.position.x = config.get_value(“player”, “x”)
$player.position.y = config.get_value(“player”, “y”)
$money.position.x = config.get_value(“money”, “x”)
$money.position.y = config.get_value(“money”, “y”)

  1. You’ll want to check if the player is holding down the save key every frame, not just when the node enters the tree. Move that code to _process.
  2. If the player doesn’t immediately press the load key as soon as the node enters the tree, the data will not load. I’d recommend removing the Input.is_action_just_pressed("load") and just checking that there isn’t an error.
  3. It looks like you’re calling config.save before you save every value. Make sure to save after you set every value.

By the way, you can add ``` before and after your code to properly format it.

If you post code, it looks better if you wrap it in backticks:

```gdscript
code code code
```

With yours, it looks like (assuming I got the indenting right…):

extends Control

var config = ConfigFile.new()
var err = config.load(“res://l1.tscn”)

func _ready() → void:
    if Input.is_action_just_pressed(“save”):
        config.set_value(“player”,“x”, round($player.position.x))
        config.set_value(“player”,“y”, round($player.position.y))
        config.save(“res://l1.tscn”)
        config.set_value(“money”,“x”, round($money.position.x))
        config.set_value(“money”,“y”, round($money.position.y))

    print(position)
    var err = config.load(“res://l1.tscn”)

    if Input.is_action_just_pressed(“load”) and err == OK:
        $player.position.x = config.get_value(“player”, “x”)
        $player.position.y = config.get_value(“player”, “y”)
        $money.position.x = config.get_value(“money”, “x”)
        $money.position.y = config.get_value(“money”, “y”)

There are a couple of things here:

  • you’re creating a config file (config) in memory, but you’re writing it out to disk too early; you need to config.save("user://some/path/name.save) after all the set_value() calls
  • save files need to be in user:// or you won’t be able to write them when you export; the release version of the game won’t let you write to res:// – it might not work even in debug, depending on your platform
  • I’d suggest an explicit test for err != OK and complain to the logs or something; what you have here will silently fail if the config load fails
  • edit: Oh, yeah, @ProgrammerOnCoffee is right; calling that from _ready() probably isn’t going to work, you definitely want to move that code to _process()
1 Like

thanks it worked god bless

1 Like