Trying to load up a previous sprite position in a scene, but I always get a error when trying to read from the file.

My file is in the right directory and the file content isn't empty.

If someone can tell me what I'm doing wrong that be appreciated.

The error message says invalid index on base nil, suggesting that the object, current_line, is null. Since it was returned in the previous line from a parse_json call, that could mean that the parsing didn't succeed. Perhaps because of badly formatted json data.

The json data in question, what we can see of it, is a dictionary where the first entry, "filename":"the url string" looks ok. The next entry has a name which starts with "pos: (that's all we can see of the start of the name key). And that looks suspicious, having a ":" in the name key. There's probably something missing of the key name after the "".

I think we need to see the complete line.

This is how I'm saving it.

This is the complete entry inside the "savegame.save" file.

I see the issue, but I don't really know how to solve it. Every time I save the file, there is a white space (as seen on the above screenshot. Line 2) after the entry thats causing the issue. How can I save it in the function without the white space?

I do not know if it will work, but maybe check to see if current_line has the filename key using the has (documentation) function before you define new_object? That way if the dictionary does not have filename as a key in the dictionary, the code should skip trying to parse it.

You could also check to see if current_line is an empty dictionary with the empty function, which might also work around the issue.

I'm guessing Godot includes the white space automatically, probably because functions like get_line and store_line use the newline character to tell when a line starts and ends. I am not sure if there is a way to remove it though.

I tried using has() on current_line but it still end up with the error in base: 'Nil'.

It could be that the while loop iterates twice. The first line is actually processed correctly, then the second line containing the blank space (or whatever it is) goes wrong naturally, when parsing it as json data.

So maybe you should first get what the line contains:

var text = save_game.get_line()

and check for an empty string and checking so the first character is a '{', before sending it to parse_json.

@uaknight Using your advice, I use an if statement to see if save_game.get_line() was empty and it isn't as shown here.

But though it's not empty, the interpreter is still reading it as empty. Showing the error base: 'Nil'

Looking at the picture above, it appears that current_line is equal to null, which appears to be why it is failing.

I do not know if it it will work or not, but I rewrote the code visible in the picture above to, hopefully, include all the conditionals needed to work around newlines and empty strings. I formatted the code a bit so it would fit better in this post.

I would copy/backup the script file before trying the code below, as I have no idea if it will work or not.

# starting at line 34
while not save_game.eof_reached():
	var current_line = save_game.get_line()
	if current_line == null:
		continue
	elif current_line.empty():
		continue
	else:
		var json_dictionary = parse_json(current_line)
		if json_dictionary == null:
			continue
		else:
			if json_dictionary.has("filename"):
				var new_object = load(json_dictionary["filename"]).instance()
				add_child(new_object)
				new_object.position = Vector2(json_dictionary["pos_x"],
					json_dictionary["pos_y"])
				for i in json_dictionary.keys():
					if i == "filename" or i == "parent" or i == "pos_x" or
						i == "pos_y":
						continue
					new_object.set(i, json_dictionary[i])
save_game.close()

It might be a tad overdone, but the code above should hopefully work.

@TwistedTwigleg Thanks! You fix the loading game issue.

No problem, happy to help! I'm glad it works :smile:

3 years later