Save Load image using JSON

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

I want to save image located in a sprite.texture using json and load it.
Any help would be appreciated.

i already created json file with some data but the images to be saved needs a json pro to teach us.

Sha6er | 2020-09-07 05:21

:bust_in_silhouette: Reply From: njamster

Not sure what makes you believe storing an image path is any different from storing other information in JSON. I’d argue there is no such thing is a json pro": it’s a way of writing down text-based information in a structured way, nothing advanced.

var data = {}

func save_game():
	data["image_path"] = $Sprite.texture.resource_path

	var file = File.new()
	var error = file.open("user://savegame.json", File.WRITE)
	if error == OK:
	   file.store_line(to_json(data))
	file.close()

func load_game():
	var file = File.new()
	var error = file.open("user://savegame.json", File.READ)
	if error == OK:
	   data = parse_json(file.get_line())
	file.close()

	$Sprite.texture = load(data["image_path"])

perhaps he wants to store the image itself in the json, encoding to base64 or something?

p7f | 2020-09-07 14:08