Save file - always one empty line added

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

I followed this tutorial and did everything exactly the same way.

But for some reason a unnecessary line is always added to the save file, so it doesn’t work.

enter image description here

If I delete that line myself, the loading works!
But every time the game saves, this empty line is added and the loading doesn’t work anymore because of that.
Error message: "Invalid get index ‘filename’ (on base: ‘Nil’)
How can I fix this?

The line break at the end of file shouldn’t cause it to become invalid – it works here with a JSON file that has a final LF line break.

Is the file saved using CRLF or LF line endings, and does it work if you convert it to LF then save it? (You can see that in the bottom-right corner of Visual Studio Code; click it to change the line ending type).

Calinou | 2019-02-07 00:28

It doesn’t works neither with CRLF nor with LF.
And my code is exactly as in the tutorial.
Is there maybe a way to automatically delete the last line of a file by code?

Banduck | 2019-02-07 09:23

:bust_in_silhouette: Reply From: Banduck

I finally solved the problem!
If someone has the same problem:
You just have to add if current_line != null: after var current_line = parse_json(save_game.get_line())

So this part of code should look like this:

while not save_game.eof_reached():
    var current_line = parse_json(save_game.get_line())
    if current_line != null:
        var new_object = load(current_line["filename"]).instance()