![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | Diet Estus |
I am trying to implement a save feature for my inventory system.
My inventory is a simple array in which I store nodes corresponding to my items. For example,
var inventory = [get_node("Item1"), get_node("Item2"), get_node("Item3")]
Item nodes have a save_dict()
method which returns a dictionary of their persistent attributes (in this example, just count
).
When I save my game, I iterate over these nodes:
func save_game():
var inventory_data = {}
for item in inventory:
inventory_data[item.file_path] = item.save_dict()
# save inventory data as JSON
...
The overall structure of inventory_data
is
{
"res://Item1.tscn":{"count": value},
"res://Item2.tscn:{"count":value},
"res://Item3.tscn:{"count":value}
}
I save this dictionary as a JSON file and when I load my game, I use this dictionary to reconstruct my inventory.
func load_game():
# load inventory data from JSON
...
for file_path in inventory_data.keys():
var item = load(file_path).instance()
for attribute in inventory_data[file_path]:
item.set(attribute, inventory_data[file_path][attribute]
inventory.append(item)
The problem is that when my player collects items, they are added to inventory
in a certain order. When he pulls up the inventory menu they are displayed in that order.
But when he saves the game, the items are iterated over in the correct order, but they are added into the inventory_data
dictionary in a different order. (I suspect the dictionary automatically sorts the keys alphabetically.)
Thus, when the game is loaded, the items in inventory
are in a different order than they were before the save. They correspond to the order of the dictionary.
My question: How can I save my inventory data in such a way as to keep the order of inventory
?