Author Topic: Check and Take more than one item at a time?  (Read 349 times)

Legacy_Buddywarrior

  • Hero Member
  • *****
  • Posts: 512
  • Karma: +0/-0
Check and Take more than one item at a time?
« on: February 20, 2015, 10:17:44 pm »


               

I would like to check if a player has 20 gems, then take 20 gems, but I can't find a function that would elegantly allow me to do so. HasItem and GetItemPossessedBy looks like it doesn't check for stack number. Sadly, apparently I've never needed to do this in the past.


 


//how to check and take more than a single item at a time?
if (GetItemPossessedBy(oPC, "somegem")!= OBJECT_INVALID)
   {
      //do something...
   }


               
               

               
            

Legacy_meaglyn

  • Hero Member
  • *****
  • Posts: 1451
  • Karma: +0/-0
Check and Take more than one item at a time?
« Reply #1 on: February 20, 2015, 10:42:32 pm »


               

For that you'd probably want to do a loop through everything in the inventory and when you find the right kind of gem you


destroy it and decrement your needed amount by the stack size. Then keep going until you have them all. If you need to


check first you may need to do the loop twice, once to count them and once to take/destroy them.


 


Trying to be more fancy and save the items you found in a list or something is probably not worth the effort.


 


Let me know how you make out. I can post my versions but I think you can do it '<img'>



               
               

               
            

Legacy_Kato -

  • Hero Member
  • *****
  • Posts: 747
  • Karma: +0/-0
Check and Take more than one item at a time?
« Reply #2 on: February 21, 2015, 09:46:10 am »


               
Simply replace sRef by sTag and pass the tag(and change GetResRef for GetTag) if you prefer to scan by tag instead of resref.

 

int GetNumItemsOnPC(string sRef, object oPC)

{

   int nNth;

   object oItem = GetFirstItemInInventory(oPC);

   while(GetIsObjectValid(oItem))

   {

      if(GetResRef(oItem) == sRef) nNth += GetItemStackSize(oItem);     

      oItem = GetNextItemInInventory(oPC);

   }

   return nNth;

}

 

 

void DelNumItemsFromPC(string sRef, object oPC, int nNmb)

{

   int nDeleted, nItemStack;

   object oItem = GetFirstItemInInventory(oPC);

   while(GetIsObjectValid(oItem) && nDeleted < nNmb)

   {

      if(GetResRef(oItem) == sRef)

      {

         nItemStack = GetItemStackSize(oItem);

         if(nItemStack <= (nNmb-nDeleted))

         {

            nDeleted += nItemStack;

            DestroyObject(oItem);

         }

         else

         {

            SetItemStackSize(oItem, nItemStack-(nNmb-nDeleted));

            nDeleted += (nNmb-nDeleted);

         }

      }

      oItem = GetNextItemInInventory(oPC);

   }

}

 

 

Kato


               
               

               
            

Legacy_meaglyn

  • Hero Member
  • *****
  • Posts: 1451
  • Karma: +0/-0
Check and Take more than one item at a time?
« Reply #3 on: February 21, 2015, 02:08:52 pm »


               

I was going to let Buddywarrior do some of that work since that's how you learn rather than being spoon fed, but yes, that's the idea...



               
               

               
            

Legacy_Kato -

  • Hero Member
  • *****
  • Posts: 747
  • Karma: +0/-0
Check and Take more than one item at a time?
« Reply #4 on: February 21, 2015, 02:43:20 pm »


               

Sorry about that, I only meant to help, I should have read your answer more carefully, Meaglyn  ':unsure:'


 


 


Kato



               
               

               
            

Legacy_Buddywarrior

  • Hero Member
  • *****
  • Posts: 512
  • Karma: +0/-0
Check and Take more than one item at a time?
« Reply #5 on: February 21, 2015, 04:20:07 pm »


               

GetItemStackSize() was the function I needed. Thanks folks!