Echo Hollow

Gender-Bending Interactive Stories! :D

User Tools

Site Tools


libtf:documentation:api:inventory

This is an old revision of the document!


<-- Back

LibTF.Inventory

Inherits From: LibTF.PersistentObject

An Inventory is an object that contains other things. An inventory is used to track items that a Person carries. A different specialized inventory is used to track what Apparel a Person is wearing. A trunk or bookshelf might use an Inventory to track what is held within on on it. In a more traditional sort of location-based Twine IF, each location might even use an inventory to track what items the player can find or has dropped there.

Fields

Inherited from LibTF.PersistentObject: id, parentId, parent, name, aName, nameIsProper, nameIrregularArticle

Methods

Inherited from LibTF.PersistentObject: hasPersistent(), getPersistent(), setPersistent(), resetPersistent()

(Inventory).add( itemOrId )

  • Arguments:
    • itemOrId : the thing to be added to the inventory.
  • Returns: Nothing, although it probably ought to return the thing that was added, or null if it wasn't added…
  • Throws: Error if: itemOrId is invalid or malformed, or if the preAdd() callback does not return a proper Boolean value.

This method adds the given itemOrId to the inventory. It does not remove it from any other inventories that might also contain it. itemOrId may be a persistence id string, or a fully instantiated PersistentObject (or subclass thereof).

tfGet("joe").inventory.add( "some_widget" );

(Inventory).remove( itemOrId )

  • Arguments:
    • itemOrId: the thing to be removed from the inventory.
  • Returns: Nothing, although it probably ought to return the thing that was removed, or null if it wasn't removed…
  • Throws: Error if: itemOrId is invalid or malformed, or if the preRemove() callback does not return a proper Boolean value.

This method removes the given itemOrId from the inventory. itemOrId may be a persistence id string, or a fully instantiated PersistentObject (or subclass thereof).

tfGet("joe").inventory.remove( "some_widget" );

(Inventory).has( itemOrId )

  • Arguments:
    • itemOrId: the thing to be checked for in the inventory.
  • Returns: (Boolean) True if the inventory contains the specified thing, or False if not.
  • Throws: Error if: itemOrId is invalid or malformed.

This method checks to see if the inventory contains the given itemOrId. itemOrId may be a persistence id string, or a fully instantiated PersistentObject (or subclass thereof).

if( tfGet("joe").inventory.has("some_widget") )
	alert( "Joe has a widget!" );

(Inventory).get()

  • Arguments: None.
  • Returns: (Array) An array of fully instantiated PersistentObjects (or subclasses thereof).
  • Throws: Nothing.

Returns an array of fully instantiated PersistentObjects (or subclasses thereof) contained in the inventory.

FIXME: We ought to have a method that just returns ids, too…

alert( "Joe is carrying " + LibTF.Util.commaList(tfGet("joe").inventory.get()) + ".  Kinky." );
// prints "Joe is carrying a ball of string, an octopus, and a European swallow.  Kinky."
  • Arguments:
    • examineLink: set to True to include links to object passages in the returned value.
  • Returns: (String) A string listing the contents of the inventory, or “Nothing” if the inventory is empty.
  • Throws: Nothing.

Returns a natural language string describing the contents of the inventory.

If examineLink is True, the returned string will contain links to any “examine passages” available for objects in the inventory. For example, if there is an object with the id “octopus”, and you create a passage titled “Examine.octopus” (note the dot there), then a link to that passage will be included in the returned string. This is useful for examining or manipulating objects in inventory.

// Assuming that there is a passaged titled "Examine.octopus", but none for the ball of string or the swallow...
alert( "Joe is carrying " + tfGet("joe").inventory.getListString() + ".  Kinky." );
// prints "Joe is carrying a ball of string, [[an octopus->Examine.octupus]], and a European swallow.  Kinky."

(Inventory).sort( a, b )

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

This is the function used by .get() to sort the inventory. It follows the rules of Javascript's (Array).sort().

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

(Inventory).preAdd( id )

  • Arguments:
    • id: the persistence id of the thing being added to the inventory.
  • Returns: (Boolean) True to allow the item to be added, False to deny it.
  • Throws: Nothing.

This callback method is called before id is added to the inventory. Returning True allows the add to continue, while False will cancel it.

This method is meant to be overridden by derived classes that can only contain certain sorts of things or exhibit special behaviors. Unless you are extending the library, you'll probably never need to use this method.

(Inventory).postAdd( id )

  • Arguments:
    • id: the persistence id of the thing that was added to the inventory.
  • Returns: Nothing.
  • Throws: Nothing.

This callback method is called after id has been added to the inventory.

This method is meant to be overridden by derived classes that can only contain certain sorts of things or exhibit special behaviors. Unless you are extending the library, you'll probably never need to use this method.

(Inventory).preRemove( id )

  • Arguments:
    • id: the persistence id of the thing being removed from the inventory.
  • Returns: (Boolean) True to allow the item to be removed, False to deny it.
  • Throws: Nothing.

This callback method is called before id is removed from the inventory. Returning True allows the removal to continue, while False will cancel it.

This method is meant to be overridden by derived classes that can only contain certain sorts of things or exhibit special behaviors. Unless you are extending the library, you'll probably never need to use this method.

(Inventory).postRemove( id )

  • Arguments:
    • id: the persistence id of the thing that was removed from the inventory.
  • Returns: Nothing.
  • Throws: Nothing.

This callback method is called after id has been removed from the inventory.

This method is meant to be overridden by derived classes that can only contain certain sorts of things or exhibit special behaviors. Unless you are extending the library, you'll probably never need to use this method.

<-- Back

libtf/documentation/api/inventory.1519586631.txt.gz · Last modified: 2018/02/25 11:23 by lee