Checking whether a file is finished saving or ignoring errors returned from load_jpg_from_buffer

Godot Version

Godot Version 4.4

Question

Hi there, I’m currently writing a program that takes JPG input from another program. I am drawing JPG input from this program many times a second. To preface this, my program works perfectly fine as intended. However, what’s troubling me is that the function “load_jpg_from_buffer(buffer)” is filling my debugger with Errors with every run of the program.

image

This error is always an ERR_PARSE_ERROR due to how the JPG loaded may be corrupted as it has not fully been saved by the other program. However, I cannot know if the JPG is corrupted without first checking if it’s corrupted with the function, which prints an error message to the debugger.

My workaround for now is to limit the number of error messages a second to give myself a small piece of mind. Would anyone know of a way to ignore this error? Otherwise, would anyone know of a better way to check whether the external file is finished saving and only loading it then?

    var fileName : String = "../images/" + parseStr.substr(0, strLength - decimal) + str(fileNum) + ".jpg"
	var jpg = FileAccess.open(fileName, FileAccess.READ)
	if jpg == null:
		print("Error opening file: ", fileName)
		return null
	if jpg.get_length() < 100000:
		print("Jpg too small")
		return null

	var buffer = jpg.get_buffer(jpg.get_length()) 
	
	var tempError = tempImage.load_jpg_from_buffer(buffer)
	if tempError != OK:
		print("Error Code: ", tempError)	
		return null

	```

Would a file_exists check help?

if FileAccess.file_exists(fileName):

In fact if the program saving it is waiting, perhaps you need a loading queue, where you make a load attempt, if file doesn’t exist, wait a bit before trying again, perhaps 3 tries max, and then throw an error?

Anyway, something like that.

PS

I would not try to ignore that error. Seems quite an important error that to me you should be handling.

You could try checking the modified time with FileAccess.get_modified_time() and only open it when it changes.

1 Like