Table of Contents

GeneralInventory

Extends: PersistentObject

GeneralInventory is the base Inventory implementation from which all other Inventory types are derived. It provides default functionality which can be overridden in derived classes.

Usage Notes

STUFF

Static Properties/Functions

.move( item, srcInventory, destInventory, actor )

As actor, try to move item from srcInventory to destInventory. This will call .remove() and .add() on the given inventories. If the move fails for some reason, the source and destination inventories will be left in the state they started in.

Constructor( id )

Do not ever use “new” to instantiate a PersistentObject (or subclass thereof). Instead, store the result of PersistentObject.define() somewhere and reference it when needed, or fetch the object by its 'id' with PersistentObject.fetch().

Properties/Functions

.contents

An array of the id fields of all the things (PersistentObjects and subclasses) contained in this inventory. Duplicates are not allowed. Order does not matter.

.add( item, actor=undefined, force=false )

Adds the given item to the inventory.

It does not remove it from any other inventories that might already contain it. You probably usually want to use InventoryUI or .move() instead.

Before the item is added to the inventory, preAdd(item,thisInventory,actor) (if such a function exists) is called on the actor, the item, and the inventory, in that order, to ask permission for the item to be added to the inventory. If any of these return false, the item is not added to the inventory and nothing else is done.

However, if the item was successfully added to the inventory, postAdd(item,thisInventory,actor) is called on the actor, the item, and the inventory, in that order, to notify everything involved that the item was placed in the inventory so that they can react to it if needed.

.remove( item, actor=undefined, force=false )

Removes the given item from the inventory.

Before the item is removed from the inventory, preRemove(item,thisInventory,actor) (if such a function exists) is called on the actor, the item, and the inventory, in that order, to ask permission for the item to be removed from the inventory. If any of these return false, the item is not removed from the inventory and nothing else is done.

However, if the item was successfully removed from the inventory, postRemove(item,thisInventory,actor) is called on the actor, the item, and the inventory, in that order, to notify everything involved that the item was removed from the inventory so that they can react to it if needed.

.has( item )

Checks to see if the inventory contains the given item.

.isEmpty()

Check if the inventory is empty.

.get( filterClass=undefined, filterFunction=undefined )

Returns the fully-instantiated contents of the Inventory.

If a class is passed to filterClass, only items that are of that class (or a subclass thereof) will be included in the returned array.

If a function is passed to filterFunction, that function will be called once on every item in the inventory. If this function returns true, the item will be included in the returned array. If the function returns false, the item will not be included.

.sort( a, b )

An optional function used by .get() to sort the inventory contents. It follows the rules of Javascript's (Array).sort(). This function is not defined on GeneralInventory, but can be defined by subclasses to alter the inventory sort order.

This function is generally only used by derived classes that need to sort differently. Unless you are extending the library, you'll probably never need to define or call it.

.override

When defined, the override value sets the value that .toString() returns, ignoring the actual contents (if any) of the inventory. When set to undefined, toString() returns the actual inventory contents as normal.

This is useful for minor characters and objects who do not actually need a fully-featured inventory, as it avoids all the hassle of defining flavor objects to populate the inventory with, that the protagonist will otherwise never be able to interact with. See the Tutorial for details.

.toString()

Overridden.

.theString()

Description.