Godot crash when trying to write save data

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

Hello, I’m making a Sudoku game for mobile when I tried to save the current puzzle position Godot become unresponsive.

Here’s how the Save Game code implemented:

func save_current_puzzle():
	var save_puzzle = File.new()
	var save_name = "user://puzzle.dat"
	if save_puzzle.file_exists(save_name):
		var puzzle_list = read_saved_puzzle()
		var current_puzzle = all_board_number
		if not current_puzzle in puzzle_list:
			var latest_line = get_saved_puzzle_latest_line() + 1
			var save_dict = {'no':latest_line, 'puzzle': current_puzzle}
			save_puzzle.open(save_name, File.READ_WRITE)
			save_puzzle.store_line(to_json(save_dict))
		else:
			print("This puzzle is already exists in databases")
	else:
		var current_puzzle = all_board_number
		var save_dict = {'no':0, 'puzzle': current_puzzle}
		save_puzzle.open(save_name, File.WRITE_READ)
		save_puzzle.store_line(to_json(all_board_number))
	save_puzzle.close()
	print("Save game written.")

Save puzzle reader code:

func read_saved_puzzle():
	var save_puzzle = File.new()
	var save_name = "user://puzzle.dat"
	if save_puzzle.file_exists(save_name):
		var puzzle_list = []
		save_puzzle.open(save_name, File.READ)
		while save_puzzle.get_position() < save_puzzle.get_len():
			var sdc = parse_json(save_puzzle.get_line())
			puzzle_list.append(sdc.puzzle)
		save_puzzle.close()
		return puzzle_list
	else:
		print("There is no save data exists.") 

save puzzle last line getter:

func get_saved_puzzle_latest_line():
	var save_puzzle = File.new()
	var save_name = "user://puzzle.dat"
	if save_puzzle.file_exists(save_name):
		var puzzle_line = 0
		save_puzzle.open(save_name, File.READ)
		while save_puzzle.get_position() < save_puzzle.get_len():
			puzzle_line += 1
		save_puzzle.close()
		return puzzle_line
	else:
		print("Save data does not exists.") 

How the saved puzzle data looks like:

{“no”:0,“puzzle”:[[0,0,0,0,0,4,0,0,0],[0,1,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0]]}

What’s wrong with it? Please help.

:bust_in_silhouette: Reply From: jgodfrey

I’d guess the reason the code is hanging is due to this block:

    while save_puzzle.get_position() < save_puzzle.get_len():
        puzzle_line += 1

You seem to be attempting to seek the end of the file there, but you’re not doing anything in that loop to advance the file pointer. Since nothing is being changed in the loop, it’ll just run forever - hanging your game.

Ah… I forgot to add save_puzzle.get_line() to make the cursor move… thanks I didn’t see that,.

razor7131 | 2020-11-29 16:15