+1 vote

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?

in Engine by (24 points)

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

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?

1 Answer

+1 vote
Best answer

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