Inventory
The Container model introduced a mapped object to represent bags, bank slots, the player's backpack, and any other container capable of holding items.
The Inventory model is a concept that groups all containers available to the player in a way to represent all items the player has.
Although instantiable just like almost every model in the Stormwind Library, it's recommended to use the instance stored in the library object as a good source of truth.
For a more detailed explanation of the Inventory model and its available methods and properties, please refer to the library technical documentation.
Inventory tracking
When the library is instantiated, it's possible to tell it to track the
player's inventory by setting the inventory.track
property to true
(read more here). By default, it won't
track inventory, at least until its performance is measured and improved.
If inventory tracking is set to true
, upon initialization, the library will
automatically instantiate the player's inventory and store its instance. After
that, it will "try its best" to keep it updated with the player's containers,
so it's the best source of truth for getting the player's inventory items.
-- this will return a table with instances of Item
local playerItems = library.playerInventory:getItems()
The inventory tracking feature is still experimental as of version 1.4.0, when it was introduced. It's recommended to use it with caution and expect changes in future versions, especially in performance.
By default, the library won't track the player's inventory.
What's available in inventory tracking?
- Updates when the player's bags are updated, which means the
library.playerInventory
instance will be updated when players get, remove, move, etc, items in their bags. - Items mapped as objects with the Item model, so it's possible to access all item properties and methods and implement new features in the next versions.
What's not available yet?
- Caching items in bank slots
- Item quantities
- Slot mapping
- Programmatically item operations like moving, removing
What's on the radar for future versions of inventory tracking?
- The items listed above
- Performance improvements - as of now, every
BAG_UPDATE
event will trigger a full inventory update and that should be reviewed in the future, because sometimes this event may be triggered multiple times in a short period of time, so it's important to polish this feature to avoid performance issues.
The outdated flag
A flag called outdated
was introduced in the library version 1.8.0 to set a
state in the inventory instance that indicates that the items list is outdated.
This flag works similarly to the one in the Container model. And the reason both classes have this "redundant" flag is that Inventory and Container must be able to exist independently.
Thanks to this flag, the methods that manipulate the inventory items will scan all the containers and update the items list only when needed.
As said before, when the library.playerInventory
instance is active, it will
watch the BAG_UPDATE
event, but now instead of updating the items in each
container like it used to work when it was introduced in previous versions, it
will only update the outdated flag in the inventory instance. Once any method
is called in the inventory that requires the items list, then items will be
scanned and updated.