Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | CowThing | |
Old Version | Published before Godot 3 was released. |
I have an array with dictionaries in it, each dictionary is a different item. I’m trying to save and load this inventory. The file is saving correctly, and when I load the file I have a print() which is printing the correct value from the line it’s reading. However after all the items have been appended, the inventory is just full of whatever item was in the last slot.
For example if I save an inventory with item A, item B, and item C. After loading the inventory would be 3 duplicates of item C.
This is my code:
func save_game():
var savegame = File.new()
savegame.open("user://savegame.save", File.WRITE)
for i in range(inventory.size()):
var data = inventory[i]
savegame.store_line(data.to_json())
savegame.close()
func load_game():
var savegame = File.new()
if !savegame.file_exists("user://savegame.save"):
return
var current_line = {}
savegame.open("user://savegame.save", File.READ)
while(!savegame.eof_reached()):
var line = savegame.get_line()
if line.empty():
break
current_line.clear()
current_line.parse_json(line)
print(current_line) #this is printing the correct value
inventory.append(current_line) #this is not appending the correct value
savegame.close()
I had a similar problem with a dictionary of dictionaries. See my related question and the answers on reddit here
duke_meister | 2016-03-29 05:44