Author Topic: Need a GetIsItemInBag  (Read 484 times)

Legacy_PhantomSkyfire

  • Newbie
  • *
  • Posts: 14
  • Karma: +0/-0
Need a GetIsItemInBag
« on: March 09, 2011, 09:24:33 am »


               I require a simple function that can determine whether an inputted item is in an inventory container such as a Bag of Holding. I remember having a working function at one point but I seem to have misplaced it and this one always seems to return FALSE:

int GetIsItemInBag(object oItem)
{
    if (GetIsObjectValid(oItem) == FALSE)
    {
         return FALSE; //:: Sanity check!
    }
    else return (GetObjectType(GetItemPossessor(oItem)) == OBJECT_TYPE_ITEM);
}

Any suggestions, fixes, or code snippits?
               
               

               


                     Modifié par PhantomSkyfire, 13 mars 2011 - 12:49 .
                     
                  


            

Legacy__Knightmare_

  • Full Member
  • ***
  • Posts: 191
  • Karma: +0/-0
Need a GetIsItemInBag
« Reply #1 on: March 09, 2011, 02:48:07 pm »


               I imagine you would need to run two (nested) loops in your code (ie. a loop within another loop). The first loop would look for bag items in the player's inventory (by checking if the found item does have an inventory itself). The second loop would search through the inventory of the found bag.
               
               

               
            

Legacy_Baragg

  • Sr. Member
  • ****
  • Posts: 496
  • Karma: +0/-0
Need a GetIsItemInBag
« Reply #2 on: March 09, 2011, 04:54:49 pm »


               Just to ask, why would you want to check if it is in a bag on the pc, instead of just checking if the pc is in possession of said item?
               
               

               
            

Legacy_PhantomSkyfire

  • Newbie
  • *
  • Posts: 14
  • Karma: +0/-0
Need a GetIsItemInBag
« Reply #3 on: March 09, 2011, 08:22:03 pm »


               

_Knightmare_ wrote...

I imagine you would need to run two (nested) loops in your code (ie. a loop within another loop). The first loop would look for bag items in the player's inventory (by checking if the found item does have an inventory itself). The second loop would search through the inventory of the found bag.

Augh. I was afraid I'd have to do that. Well, at least this way I know I can find the right bag, instead of just any bag. Thanks..

Baragg wrote...

Just to ask, why would you want to check
if it is in a bag on the pc, instead of just checking if the pc is in
possession of said item?

My module dumps dozens of tokens and OOC widgets onto the player, and to keep it from being overwhelming I'm using a script to auto-sort items OnAcquire. The problem is that OnAcquire runs for every item in a PC's inventory as soon as they enter the module, and I need some sort of check to see if the item is already sorted into a container before I go messing with it.
               
               

               


                     Modifié par PhantomSkyfire, 09 mars 2011 - 08:23 .
                     
                  


            

Legacy_Baragg

  • Sr. Member
  • ****
  • Posts: 496
  • Karma: +0/-0
Need a GetIsItemInBag
« Reply #4 on: March 10, 2011, 03:29:37 am »


               I will try to work something up for ya.
               
               

               
            

Legacy_Baragg

  • Sr. Member
  • ****
  • Posts: 496
  • Karma: +0/-0
Need a GetIsItemInBag
« Reply #5 on: March 10, 2011, 03:31:56 am »


               Do you happen to have a list of tags of the items you are looking to find? I can input the tags directly into the script, and send feedback on any that are not if you wish.
               
               

               


                     Modifié par Baragg, 10 mars 2011 - 03:39 .
                     
                  


            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Need a GetIsItemInBag
« Reply #6 on: March 10, 2011, 03:57:54 am »


               I think this may do what you want.  Compiled, Untested.

object GetInInventoryContainer(object oItem, object oBagHolder)
{
  object oContainer = GetFirstItemInInventory(oBagHolder);
  while(GetIsObjectValid(oContainer))
  {
     if (GetHasInventory(oContainer))
     {
        object oInTheBag = GetFirstItemInInventory(oContainer);
        while(GetIsObjectValid(oInTheBag))
        {
          if (oItem == oInTheBag) return oContainer;
          oInTheBag = GetNextItemInInventory (oContainer);
        }
     }
     oContainer = GetNextItemInInventory (oBagHolder);
  }
  // Note: At this point the item is not in a container
  // and oContainer = OBJECT_INVALID
  return oContainer;

               
               

               
            

Legacy_Baragg

  • Sr. Member
  • ****
  • Posts: 496
  • Karma: +0/-0
Need a GetIsItemInBag
« Reply #7 on: March 10, 2011, 04:08:16 am »


               You beat me to it light here is what I came up with:

[nwscript]
int DetermineIfItemInBag(object oPC, object oItem)
{
object oFirst = GetFirstItemInInventory(oPC);
object oBagItem;
string sTag = GetTag(oItem);

if(GetItemPossessedBy(oPC, sTag) != OBJECT_INVALID)
{//if the PC actually possess the item in their invo somewhere
   while(oFirst != OBJECT_INVALID)
   {//while the PCs inventory items are valid
      if(GetHasInventory(oFirst))
      {//if object oFirst has an inventory run through its items
           oBagItem = GetFirstItemInInventory(oFirst);
           while(oBagItem != OBJECT_INVALID)
           {
               if(oBagItem == oItem) return TRUE;
               else oBagItem = GetNextItemInInventory(oFirst);
           }
       }
       else oFirst = GetNextItemInInventory(oPC);
     }
}

return FALSE;
}[/nwscript]
               
               

               
            

Legacy_Baragg

  • Sr. Member
  • ****
  • Posts: 496
  • Karma: +0/-0
Need a GetIsItemInBag
« Reply #8 on: March 10, 2011, 04:10:12 am »


               K, how do I keep the forums from removing my indents?
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Need a GetIsItemInBag
« Reply #9 on: March 10, 2011, 04:27:36 am »


               I use the [ code] [ /code] blocks to keep the endent from going By By. 

One comment on your code.  I see no point for useing: 

if(GetItemPossessedBy(oPC, sTag) != OBJECT_INVALID)

To make sure the item is in the inventory before running the rest of the code.  

The reason is that  GetItemPossessedBy loops through the PC inventory looking for the item.  ( also It may just be another item with the same tag as oItem and not oItem) .  There is really no reason to have that function loop through,  then loop through again with your own code. 
               
               

               
            

Legacy_GhostOfGod

  • Hero Member
  • *****
  • Posts: 1490
  • Karma: +0/-0
Need a GetIsItemInBag
« Reply #10 on: March 10, 2011, 05:24:33 am »


               

Baragg wrote...

K, how do I keep the forums from removing my indents?


I always paste scripts into the standard form as apposed to the quick reply. I haven't had any indent problems even without using "/quote" "/code".
               
               

               
            

Legacy_Baragg

  • Sr. Member
  • ****
  • Posts: 496
  • Karma: +0/-0
Need a GetIsItemInBag
« Reply #11 on: March 10, 2011, 01:31:53 pm »


               Lol, in the old forums I originally used the code blocks, but then learned to use the nwscript blocks to make the script look right. Now I gotta get use to using code blocks again.
               
               

               
            

Legacy_PhantomSkyfire

  • Newbie
  • *
  • Posts: 14
  • Karma: +0/-0
Need a GetIsItemInBag
« Reply #12 on: March 11, 2011, 03:01:01 am »


               

Baragg wrote...

Do you happen to have a list of tags of the items you are looking to find? I can input the tags directly into the script, and send feedback on any that are not if you wish.

There is no specific list of tags. My criteria is any item with a tag prefixed with "dmfi_" , "hlslang_", "twig_" or "ttkn". Currently the list does not include any "twig_" or "ttkn_" items, though.

To everyone else: I am a competant scripter and can write simple loops like this on my own, but I apperciate everyone taking their time to help me. Double-checking your code will save me some time from writing my own from scratch, at least.
               
               

               
            

Legacy_Baragg

  • Sr. Member
  • ****
  • Posts: 496
  • Karma: +0/-0
Need a GetIsItemInBag
« Reply #13 on: March 11, 2011, 03:16:43 am »


               As is often the case with me I tend to over think things and had envisioned a grand script with a listing of tags, and other functions, lol.
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Need a GetIsItemInBag
« Reply #14 on: March 11, 2011, 03:39:10 am »


               

PhantomSkyfire wrote...

To everyone else: I am a competant scripter and can write simple loops like this on my own, but I apperciate everyone taking their time to help me. Double-checking your code will save me some time from writing my own from scratch, at least.


If you do not want code and a function, you realy should not request a function or code.

PhantomSkyfire wrote...

I require a simple function that can determine whether an inputted item is in an inventory container such as a Bag of Holding. I remember having a working function at one point but I seem to have misplaced it and this one always seems to return FALSE:

.....

Any suggestions, fixes, or code snippits?