.entries() doesn't work, because the constructor only takes what the values would be (the Item objects). Changing it to this.values(), it works.
Edit: Okay, here's what's happening now:
With this code:
clone () {
// Return a new instance containing our current data.
return new Inventory(Array.from(this.values()));
}
toJSON() {
// Return a code string that will create a new instance
// containing our current data.
let data = Array.from(this.values());
return JSON.reviveWrapper('new Inventory($ReviveData$)', data);
}
The Inventory object is meant to, itself, contain complex objects, Items, as its values. Revival turns the Inventory into a Map of the Items' first property, rather than a Map of Items as it's supposed to be.
This seems to be happening even when I comment out the custom toJSON method. Without the custom toJSON method, Inventory revives to a generic object, but its values still change into each Item's "action" property.
The Items' first property is also a complex object, if that's important.
Edit 2: The best I can tell is that this has something to do with Twine's save construction. Console logs tell me that the error is only ocurring when I open the save menu or a save is made.
I've added console logs for every call to clone(), toJSON(), and to the constructor. I have also made a log echoing the result of Array.from(this.values()) in toJSON(). Here is what I see on startup when no saves exist:
Initial Inventory construction
Constructing Inventory. ItemArray =
(20) [Item, Item, Item, Item, Item, Item, Item, Item, Item, Item, Item, Item, Item, Item, Item, Item, Item, Item, Item, Item]
Inventory clone called
Constructing Inventory. ItemArray =
(20) [Item, Item, Item, Item, Item, Item, Item, Item, Item, Item, Item, Item, Item, Item, Item, Item, Item, Item, Item, Item]
(x3)
Inventory toJSON called
(20) [Item, Item, Item, Item, Item, Item, Item, Item, Item, Item, Item, Item, Item, Item, Item, Item, Item, Item, Item, Item]
But when an autosave exists, this is what I see on startup:
Constructing Inventory. ItemArray =
(20) [ItemAction, ItemAction, ItemAction, ItemAction, ItemAction, ItemAction, ItemAction, ItemAction, ItemAction, ItemAction, ItemAction, ItemAction, ItemAction, ItemAction, ItemAction, ItemAction, ItemAction, ItemAction, ItemAction, ItemAction]
(x2)
...followed by the previous logs.
When I open the "Saves" menu with an extant save, this appears in the console:
Constructing Inventory. ItemArray =
(20) [ItemAction, ItemAction, ItemAction, ItemAction, ItemAction, ItemAction, ItemAction, ItemAction, ItemAction, ItemAction, ItemAction, ItemAction, ItemAction, ItemAction, ItemAction, ItemAction, ItemAction, ItemAction, ItemAction, ItemAction]
(x5)
When I load the save, a ton of construct calls are made, and this time toJSON() creates an array of ItemActions rather than Items.
Since the strange constructor calls aren't linked to my functions, I assume they're part of the save functionality, which I don't know anything about. So I'll need to know how that works before I can puzzle this out.