I have Items that are of type Reference
. I'm building a save system and at the moment I do (a bit simplified):
var file = File.new()
file.open("user://save_game.dat", File.WRITE)
file.store_string(var2str(save_data))
file.close()
This gives me, what seems to be all information about the Item in text form (equipped_shield
is a variable holding a reference to the Item Object):
{
"PlayerData": {
"equipped_shield": Object(Reference,"script":Resource( "res://Shields/Shield.gd"),"item_name":"Large wooden shield","description":"More protection and some fancy dtails.","item_type":1,"rarity":1,"equippable":true,"buy_price":0.0,"sell_price":0.0,"icon_texture":"res://Images/Items/Weapons/Shield_02_BigIcon_A.png","shield_library":Object(Object,"script":Resource( "res://Shields/ShieldLibrary.gd"),"shield_data":Object(Object,"script":Resource( "res://Shields/ShieldData.gd"),"global_constants":Resource( "res://GlobalConstants.gd"),"shield_type":"LARGE_WOOD","defense":2.0,"element":"NONE","traits":[ ],"rnd":Object(RandomNumberGenerator,"seed":-8493446615977093570,"state":-739612688886659976,"script":null),"level":1),
...
}
...
}
When I try to load back the data, doing:
var file = File.new()
file.open("user://save_game.dat", File.READ)
var load_data = str2var(file.get_as_text())
file.close()
I do get back the dictionary. I can see it in the debug view and equipped_shield
has a reference to an Object. But the Object seems to be completely empty.
How do I load back the item properly? All information about the object is in the text file so there has to be a good way to do it. Do I have to create new items and set each variable to the ones in the text file? But how do I get access to those? For example, print(load_data["PlayerData"].equipped_shield.item_name)
doesn't work. It says "Invalid get index 'item_name' in (on base: 'Reference').