GetFirstItemInInventory(object)

Determines the first item in an object's inventory.

object GetFirstItemInInventory(
    object oTarget = OBJECT_SELF
);

Parameters

oTarget

The object that has the inventory. (Default: OBJECT_SELF)


Description

Returns the first item in oTarget's inventory (start to cycle through oTarget's inventory) and OBJECT_INVALID if the caller is not a creature, item, placeable or store, or if no item is found.



Remarks

Look at the second example script for an excellent function for determining if a faction has an item or a certain quantity of an item equipped; this can be done with a for loop because the integers associated with INVENTORY_SLOT start at 0 and end at 17 (for 18 total possible equipped slots).


Version

1.31

Example

// This function counts up the total number of items in the first PC's inventory.  (The number of items in a stackable count toward the total)
main()
{
   int nItems = 0;
   object oItem = GetFirstItemInInventory(GetFirstPC());
   while (GetIsObjectValid(oItem) == TRUE)
   {
      nItems = nItems + GetNumStackedItems(oItem);

      oItem = GetNextItemInInventory(GetFirstPC());
   }
}

/*
  CheckFactionForItems example

  With this function, you can check, whether a faction has got a certain amount
  of a certain item in its members inventories or equipped items.

  Created By: Arlas Gilhith (arlas@cyberlife-studios.de)
  Created On: 29.07.2003
*/
int CheckFactionForItems(object oMemberOfFaction, string sItem,
  int nAmount = 2)
{
    int iCnt = 0;
    int i;
    object oMember = GetFirstFactionMember(oMemberOfFaction, FALSE);
    object oItem;

    // loop through members
    while (!(oMember == OBJECT_INVALID) && (iCnt < nAmount))
    {
        oItem = GetFirstItemInInventory(oMember);

        // check inventory for item
        while (!(oItem == OBJECT_INVALID) && (iCnt < nAmount))
        {
            if (GetTag(oItem) == sItem)
              iCnt++;
            oItem = GetNextItemInInventory(oMember);
        }

        // check equipped items for item
        for (i = 0; i < NUM_INVENTORY_SLOTS; ++i)
        {
            if (!(GetItemInSlot(i, oMember) == OBJECT_INVALID)
              && (GetTag(GetItemInSlot(i, oMember)) == sItem))
                iCnt++;
        }

        oMember = GetNextFactionMember(oMemberOfFaction, FALSE);
    }

    return (iCnt >= nAmount);
}

// This example (used as starting conditional in a dialogue) checks whether the
// faction of the speaker has got four hatchets.
int StartingConditional()
{
    return CheckFactionForItems(OBJECT_SELF, "NW_WAXHN001", 4);
}


See Also

functions: GetNextItemInInventory
categories: Inventory Functions


 author: Tom Cassiotis, editor: Charles Feduke, additional contributor(s): Marc Ermshaus
 Send comments on this topic.