Retrieving Custom Resource Data from JSON File

Godot Version

4.7

Question

I am following this tutorial on saving and loading data. Everything is working fine, but I am having trouble with loading my custom resources. I want to be able to save an array of my custom resources, shopItemData, so that I can load them back to my game at a later playthrough. When I try to load them, however, I would get a string and I don’t understand how to use it:

(res://Shop/Item Resources/epicItem.tres):<Resource#-9223371978956601765>

I tried using both “load()” and “JSON.to_native()” functions – both a futile attempt to try and getting something to work-- to no avail. Also, the saveData.shopItems is an array that holds my custom resource, shopItemData.

Is there a way for me to retrieve my custom resources more easily?

For reference, here are the saving and loading functions.

func _save() -> void:
	
	var file : FileAccess = FileAccess.open_encrypted_with_pass(FILE_PATH, FileAccess.WRITE, "1225penumbra")
	
	var dataToSave = {
		"money" : saveData.money,
		
		"scene" : saveData.scene,
		
		"quitFromShop" : saveData.quitFromShop,
		"shopItems" : saveData.shopItems
	}
	
	var jsonVer = JSON.stringify(dataToSave)
	file.store_string(jsonVer)
	file.close()
	
	saveComplete.emit()

func _load() -> void:
	if FileAccess.file_exists(FILE_PATH):
		var file : FileAccess = FileAccess.open_encrypted_with_pass(FILE_PATH, FileAccess.READ, "1225penumbra")
		var data = JSON.parse_string(file.get_as_text())
		file.close()
		
		saveData = SaveDataResource.new()
		
		# Apply Save Data to Player Data
		saveData.money = data.money
		saveData.scene = data.scene
		
		saveData.quitFromShop = data.quitFromShop
		

        # The issue lies here
		for i in data.shopItems:
			print(i)
			var item = JSON.to_native(i, true)
			print(item)
			saveData.shopItems.append(item)
		
	else:
		print("Save not Found!")
		
	loadComplete.emit()

JSON cannot stringify all data types, this happens to include Object, Resource, and Node types. You will need to convert shopItems into a stringable format, like an Array of strings. What data type is saveData.shopItems now? What data from that item resource do you want saved?

ah, i didnt know it couldnt do that lol

saveData.shopItems was an array that held shopItemData (Resource). I need to save several variables; ID, price, name, description, stack, etc.

After posting this, I will change it to an Array that holds Strings instead. I was thinking of using “get_id_path” to save the shopItemData.

If your resource are all saved as files, and you do not duplicate or edit the resources mid-game (like changing the price), then you should use .resource_path so you can load it with load. Getting resource UIDs may fail or prove much harder on an exported game.

OMG I FINALLY GOT IT WORKING!!! THANK YOU SO MUCH!!

For strangers who have the same issue as I, here’s the tl;dr:

  • JSON can not stringify all data types (Object, Resource, Node, etc)
  • When saving Resource to file, use “.resource_path” to get the unique path to the resource
  • When loading the resource, use the “load()” method to get the resource