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()