Aller au contenu

Photo

Check and Take more than one item at a time?


  • Veuillez vous connecter pour répondre
5 réponses à ce sujet

#1
Buddywarrior

Buddywarrior
  • Members
  • 256 messages

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...
   }


#2
meaglyn

meaglyn
  • Members
  • 811 messages

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 :)



#3
Kato -

Kato -
  • Members
  • 392 messages
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


#4
meaglyn

meaglyn
  • Members
  • 811 messages

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...


  • Lightfoot8 et Buddywarrior aiment ceci

#5
Kato -

Kato -
  • Members
  • 392 messages

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

 

 

Kato



#6
Buddywarrior

Buddywarrior
  • Members
  • 256 messages

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