Author Topic: Ensuring Only 1 Item Is In A Container  (Read 443 times)

Legacy_JediMindTrix

  • Sr. Member
  • ****
  • Posts: 383
  • Karma: +0/-0
Ensuring Only 1 Item Is In A Container
« on: June 09, 2015, 03:26:52 pm »


               

I've an inkling that accomplishing this is done using GetNextItemInInventory but I am a little stumped on how to implement it at the moment. Can anyone help?


My goal is to use that function to make sure there is not more than one item inside an inventory before moving on to the rest of the script.



               
               

               
            

Legacy_Proleric

  • Hero Member
  • *****
  • Posts: 1750
  • Karma: +0/-0
Ensuring Only 1 Item Is In A Container
« Reply #1 on: June 09, 2015, 11:39:06 pm »


               

 oContainer = ...
int       nCount       = 0;
object oItem = GetFirstItemInInventory(oContainer);

while (GetIsObjectValid(oItem))
{
++nCount;
object oItem = GetNextItemInInventory(oContainer);
}

if (nCount != 1) ...

If you allow bags/boxes within the container, and need to count their contents, make this iterative.
               
               

               
            

Legacy_MrZork

  • Hero Member
  • *****
  • Posts: 1643
  • Karma: +0/-0
Ensuring Only 1 Item Is In A Container
« Reply #2 on: June 11, 2015, 04:47:10 am »


               

Proleric's code actually counts all the items in the container. If all you need to do is determine if there is no more than one item in the container, you could also use



# returns TRUE if there the number of items in the container is 1 or 0, FALSE otherwise.
int bContainsOneOrLess(object oContainer)
    {
    object oItem = GetFirstItemInInventory(oContainer);
    if GetIsObjectValid(oItem)  # there is at least one item
        {
        oItem = GetNextItemInInventory(oContainer);
        return !(GetIsObjectValid(oItem);   # FALSE if a second item was found
        }
    return TRUE;    # there are zero or one items
    }

Which may be slightly faster if there is a possibility of many items in the container.


 


This isn't what you said, but if you need to know if there is exactly one item (and not zero), then



# returns TRUE if there the number of items in the container is exactly 1, FALSE otherwise.
int bContainsOne(object oContainer)
    {
    object oItem = GetFirstItemInInventory(oContainer);
    if GetIsObjectValid(oItem)  # there is at least one item
        {
        oItem = GetNextItemInInventory(oContainer);
        return !(GetIsObjectValid(oItem);   # FALSE if a second item was found
        }
    else    # there are zero items
        {
        return FALSE;
        }
    return TRUE;    # there is exactly one item
    }

If you are going for brevity, the following should do the same thing (return TRUE when the container has exactly one item)



// returns TRUE if there the number of items in the container is exactly 1, FALSE otherwise.
int bContainsOne(object oContainer)
    {
    object oItem = GetFirstItemInInventory(oContainer);
    return GetIsObjectValid(oItem) && !(GetIsObjectValid(GetNextItemInInventory(oContainer)) );
    }