Every time I release and playtest with friends I find more bugs. This time, the logic I created to handle the NPCs' shops went way out of wack.
If I add an item to an NPC's shop list, next time that NPC loads, I want them to take it from their shop "listings" and instantiate that into their inventory that they can then sell it from. This way, I can avoid resetting the NPC's inventory entirely when I add new items to their shop offerings. As well, items the player's items they've sold to that NPC persist in the NPC's inventory, so resetting the inventory means potentially losing the player's items they could go back to buy again.
However, what actually happened with my system, is that no NPC that was last loaded in v0.1.0 had anything in their inventory on a fresh save or a save in v0.1.0 or after. But if the last loaded game version before this was v0.0.9, the NPCs could all get their items properly.
I'm kind of astounded by this one so I'm going to post the relevant code and see what you all think. Written in GDScript:
The solution I came upon was simply to turn that last if conditional into this:
If I add an item to an NPC's shop list, next time that NPC loads, I want them to take it from their shop "listings" and instantiate that into their inventory that they can then sell it from. This way, I can avoid resetting the NPC's inventory entirely when I add new items to their shop offerings. As well, items the player's items they've sold to that NPC persist in the NPC's inventory, so resetting the inventory means potentially losing the player's items they could go back to buy again.
However, what actually happened with my system, is that no NPC that was last loaded in v0.1.0 had anything in their inventory on a fresh save or a save in v0.1.0 or after. But if the last loaded game version before this was v0.0.9, the NPCs could all get their items properly.
I'm kind of astounded by this one so I'm going to post the relevant code and see what you all think. Written in GDScript:
Code Select
func add_shop_items_to_inventory():
# ... process removing inventory slots that don't belong anymore ...
# for each shop item slot in the NPC shop object:
for shopItemSlot: ShopInventorySlot in npcShop.shopItemSlots:
var existingSlot: InventorySlot = inventory.get_slot_for_item(shopItemSlot.item)
# if there is no slot for this object and it should be updated: add it to the inventory
# should_add(version) takes in the game version the NPC was last saved in, and returns true if the shop item was created after that version for this NPC:
if existingSlot == null and shopItemSlot.should_add(data.version):
inventory.add_slot(shopItemSlot)
The solution I came upon was simply to turn that last if conditional into this:
Code Select
if existingSlot == null:
inventory.add_slot(shopItemSlot)
As I no longer destroy inventory slots for NPCs if there are 0 items in that slot. This way, if the slot exists, it's already been created for the NPC. If it doesn't exist, it's an item I added to the shop listings since the last time the save file was loaded. I don't really like this solution, though, because it requires me to save essentially meaningless data -- potentially a whole bunch of empty item slots. If I simply could track the last saved version of the NPC (which I already do) and use that combined with the version that each shop listing was created in, I could definitely determine which items have been added to the shop listing since the last time this NPC was saved.