Echo Hollow

Gender-Bending Interactive Stories! :D

User Tools

Site Tools


smutbook:classes:inventory:generalinventory

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.

  • Type: Function
  • Arguments:
    • item: The PersistentObject (or subclass) to be moved.
    • srcInventory: The source GeneralInventory (or subclass) from which item is moved. FIXME What if srcInventory is undefined/null?
    • destInventory: The destination GeneralInventory (or subclass) to which item is moved.
    • actor: The Person doing the moving. FIXME What is actor is undefined/null?
  • Returns: true if the move was successful, or false if it was not.
  • Throws: Nothing

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.

  • Type: Array of strings, or empty array

.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.

  • Type: Function
  • Arguments:
    • item: the thing to be added to the Inventory.
    • actor: the (optional) Person that is doing the adding.
    • force: if true, the pre- and post- functions will not be called, and item will be added regardless.
  • Returns: true if the item was added or was already in the inventory, or false if the item could not be added to the inventory.
  • Throws: Nothing

.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.

  • Type: Function
  • Arguments:
    • item: the thing to be removed from the Inventory.
    • actor: the (optional) Person that is doing the removal.
    • force: if true, the pre- and post- functions will not be called, and item will be removed regardless.
  • Returns: true if the item was removed or was not in the inventory to begin with, or false if the item could not be removed from the inventory.
  • Throws: Nothing

.has( item )

Checks to see if the inventory contains the given item.

  • Type: Function
  • Arguments:
    • item: the thing to be checked for in the Inventory.
  • Returns: true if the inventory contains the specified item, or false if it does not.
  • Throws: Nothing

.isEmpty()

Check if the inventory is empty.

  • Type: Function
  • Arguments: None
  • Returns: true if the inventory contains no items, false otherwise.
  • Throws: Nothing

.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.

  • Type: Function
  • Arguments:
    • filterClass: an optional filter class.
    • filterFunction(item): an optional filter function.
  • Returns: an array containing the fully instantiated contents of the inventory, less those filtered out. This array will be sorted alphabetically by item.name, and further sorted by any .sort() function defined on the inventory (see below).
  • Throws: Nothing

.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.

  • Type: Function
  • Arguments:
    • a: a thing in the inventory.
    • b: another thing in the inventory.
  • Returns: <0 if a<b, >0 if a>b, or 0 if a==b.
  • Throws: Nothing

.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.

  • Type: string (or undefined)

.toString()

Overridden.

  • Type: Function
  • Arguments: None
  • Returns: a serial-comma-separated-list of the inventory's contents' .aName fields.
  • Throws: Nothing

.theString()

Description.

  • Type: Function
  • Arguments: None
  • Returns: a serial-comma-separated-list of the inventory's contents' .theName fields.
  • Throws: Nothing

smutbook/classes/inventory/generalinventory.txt · Last modified: 2023/08/08 19:16 by lee