Author Topic: Delete Multiple of same item - a better way?  (Read 348 times)

Legacy_Buddywarrior

  • Hero Member
  • *****
  • Posts: 512
  • Karma: +0/-0
Delete Multiple of same item - a better way?
« on: June 15, 2012, 04:02:59 pm »


                Is their a better way to delete multiple, but not all of the same item on a player?

This is what I'm using now.

object oPC = GetPCSpeaker();
object oItem;   
oItem = GetItemPossessedBy(oPC, "unassignedmana");
       
if (GetIsObjectValid(oItem))       
         DestroyObject(oItem);
       
if (GetIsObjectValid(oItem))       
         DestroyObject(oItem);
       
if (GetIsObjectValid(oItem))       
         DestroyObject(oItem);

edit: to fix the jargon without the [code=auto:0] feature.
               
               

               


                     Modifié par Buddywarrior, 15 juin 2012 - 03:14 .
                     
                  


            

Legacy_Kato -

  • Hero Member
  • *****
  • Posts: 747
  • Karma: +0/-0
Delete Multiple of same item - a better way?
« Reply #1 on: June 15, 2012, 04:13:21 pm »


               object oPC = GetPCSpeaker();
int nNth;
for(nNth; nNth < 3; nNth++)
{
    DestroyObject(GetItemPossessedBy(oPC, "unassignedmana"));
}

Change the number(3) according to your needs.

EDIT: If your item can be stacked the code would be somewhat different, however.

Kato
               
               

               


                     Modifié par Kato_Yang, 15 juin 2012 - 03:18 .
                     
                  


            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Delete Multiple of same item - a better way?
« Reply #2 on: June 15, 2012, 07:28:34 pm »


               #include "nw_i0_plot"

void main
{

  object oPC = GetPCSpeaker();
  TakeNumItems( oPC,"unassignedmana",3);
}
               
               

               
            

Legacy_Buddywarrior

  • Hero Member
  • *****
  • Posts: 512
  • Karma: +0/-0
Delete Multiple of same item - a better way?
« Reply #3 on: June 15, 2012, 07:42:41 pm »


               perfect thanks Kato. I wasn't sure if something like DestoryObject(GetItemPossessedBy(oPC,"reref",3)) was around. for loop will work too though =) Thank ya kindly!
               
               

               
            

Legacy_Shadooow

  • Hero Member
  • *****
  • Posts: 7698
  • Karma: +0/-0
Delete Multiple of same item - a better way?
« Reply #4 on: June 15, 2012, 08:02:25 pm »


               

Buddywarrior wrote...

 Is their a better way to delete multiple, but not all of the same item on a player?

This is what I'm using now.

object oPC = GetPCSpeaker();
object oItem;   
oItem = GetItemPossessedBy(oPC, "unassignedmana");
       
if (GetIsObjectValid(oItem))       
         DestroyObject(oItem);
oItem = GetItemPossessedBy(oPC, "unassignedmana");       
if (GetIsObjectValid(oItem))       
         DestroyObject(oItem);
oItem = GetItemPossessedBy(oPC, "unassignedmana");       
if (GetIsObjectValid(oItem))       
         DestroyObject(oItem);

code fixed

There is nothing wrong on this approach though, using loop does perform more instructions anyway. Assuming it works actually as the DestroyObject is performed later. Are you sure that it works guys? I was always using loop.
               
               

               


                     Modifié par ShaDoOoW, 15 juin 2012 - 07:09 .
                     
                  


            

Legacy_Kato -

  • Hero Member
  • *****
  • Posts: 747
  • Karma: +0/-0
Delete Multiple of same item - a better way?
« Reply #5 on: June 15, 2012, 09:44:57 pm »


               Indeed that's something to consider, ShaDoOoW. Since DestroyObject() is performed at the end and since my example does not work for stacked items I would be eager to use L8 approach since I love short and quick code lol

Kato
               
               

               


                     Modifié par Kato_Yang, 15 juin 2012 - 08:45 .
                     
                  


            

Legacy_Buddywarrior

  • Hero Member
  • *****
  • Posts: 512
  • Karma: +0/-0
Delete Multiple of same item - a better way?
« Reply #6 on: June 16, 2012, 04:19:25 am »


               The DestroyObject() is only removing one at a time, even within a loop or as listed in my original script . I've also tried LightFoot8's idea, and though it runs, it doesn't grab the items.
               
               

               
            

Legacy_Kato -

  • Hero Member
  • *****
  • Posts: 747
  • Karma: +0/-0
Delete Multiple of same item - a better way?
« Reply #7 on: June 16, 2012, 04:47:58 am »


               Ahh true, you remind me that TakeNumItems() does not handle stacks correctly. Here is a function which should do it, provided you pass the correct parameters.

void TakeItemFromCreature(object oPC, string sTag, int nNum)
{
   object oItem = GetFirstItemInInventory(oPC);
   int nDestroyed, nStack, nDel;
   while(oItem != OBJECT_INVALID && nDestroyed < nNum)
   {
      if(GetTag(oItem) == sTag)
      {
          nStack = GetItemStackSize(oItem);
          nDel = nNum - nDestroyed;
          if(nStack <= nDel)
          {
              DestroyObject(oItem);
              nDestroyed += nStack;
          }
          else
          {
              SetItemStackSize(oItem, nStack - nDel);
              nDestroyed += nDel;
          }
      }
      oItem = GetNextItemInInventory(oPC);
   }
}

It's from my old arsenal and thus could probably be optimized but it works well nonetheless '<img'>

Kato
               
               

               


                     Modifié par Kato_Yang, 16 juin 2012 - 03:56 .
                     
                  


            

Legacy_Buddywarrior

  • Hero Member
  • *****
  • Posts: 512
  • Karma: +0/-0
Delete Multiple of same item - a better way?
« Reply #8 on: June 17, 2012, 03:12:10 am »


               Tested and that worked like a charm Kato_Yang. Thank you kindly!