Author Topic: Delete items in shop?  (Read 335 times)

Legacy_Who said that I

  • Hero Member
  • *****
  • Posts: 931
  • Karma: +0/-0
Delete items in shop?
« on: October 03, 2015, 12:50:41 pm »


               Is there a script that deletes all sold items from the player in the merchants inventory?
               
               

               
            

Legacy_Wawmong

  • Jr. Member
  • **
  • Posts: 99
  • Karma: +0/-0
Delete items in shop?
« Reply #1 on: October 03, 2015, 02:07:15 pm »


               

i didn't make this bit of code, but place in your merchant OnHeartbeat handler, then add your stores tag. 


 


there might be a more suitable way to do this, because heartbeat scripts can cause lag. this is what i use though... just thought i'd return the favor.


 


 


//Heartbeat script

void main()

{

    int iBeats = GetLocalInt(OBJECT_SELF, "BEATS");

    SetLocalInt(OBJECT_SELF, "BEATS", iBeats + 1);

    iBeats = GetLocalInt(OBJECT_SELF, "BEATS");


    //@ approx 10 seconds per heartbeat, 360 HBs is approx 1 hour real time.

    if (iBeats == 348)//2 minutes until inventory is deleted.

    //if (iBeats == 20)//2 minutes until inventory is deleted.

    {

    ActionSpeakString("The Vendor inventory will be deleted in two minutes!", TALKVOLUME_SHOUT);

        object oPC = GetFirstPC();

        while (GetIsObjectValid(oPC))

        {

            SendMessageToPC(oPC, "The Vendor inventory will be deleted in two minutes!");

            oPC = GetNextPC();

        }

    }


    if (iBeats == 360)

    {

        object oStore = GetObjectByTag("tagofstore");

        object oItem = GetFirstItemInInventory(oStore);

        while (GetIsObjectValid(oItem))

        {

            DestroyObject(oItem);

            oItem = GetNextItemInInventory(oStore);

        }

        //Set beats back to 0 and start the proccess over again.

        SetLocalInt(OBJECT_SELF, "BEATS", 0);

    }

}



               
               

               
            

Legacy_MrZork

  • Hero Member
  • *****
  • Posts: 1643
  • Karma: +0/-0
Delete items in shop?
« Reply #2 on: October 05, 2015, 03:08:18 am »


               

If the intent is for the script to delete items in the store's inventory that only are there because a PC sold them, then the script may have to do more than that. Since (to my initial read) it deletes all items on the store, not just sold items from the player/


 


There are probably several ways to do this. Two come immediately to mind.


 


First, use OnStoreOpened to check when the store is first opened and make a list of the resrefs (and maybe stack sizes) of all the items in inventory. Then, write an OnStoreClosed script that runs through the store's inventory when the PC closes the store and gets rid of any items not in the original items list. The OnStoreClosed event could also be done as a heartbeat script and it could do things like delay if any PC is still in the area, etc.


 


Another approach would be to create a duplicate store and, every time the store is closed (or on a heartbeat, but I would hesitate to do this more often than needed, especially if it is potentially running for many stores) delete all the items from the store that the PCs interact with and then copy the items from the duplicate store over. The second approach is similar to the first, the main difference being that it uses another store to (effectively) keep the list of original items. It's potentially lest efficient than the first approach, but it might have the advantage of not allowing multiple copies of items to show up in the store (for instance when a sold item has the same resref as an original item). Whether that's something desired or not is up to the builder.



               
               

               
            

Legacy_WhiZard

  • Hero Member
  • *****
  • Posts: 2149
  • Karma: +0/-0
Delete items in shop?
« Reply #3 on: October 05, 2015, 03:54:36 am »


               

First, use OnStoreOpened to check when the store is first opened and make a list of the resrefs

You can also set a local variable on the store items as well, though this will allow store bought items to be sold and retained in any store.
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Delete items in shop?
« Reply #4 on: October 05, 2015, 05:08:18 am »


               

If your original store and items are from a blueprint,  you could just delete the original store and create a new one.  


 


EDIT:  example, the following script placed in the OnClosed event will clean the store on every 5th closing. 



void main()
{
    int x = GetLocalInt(OBJECT_SELF,"store_usage")+1;

    if (x>4)
    {
      CreateObject(OBJECT_TYPE_STORE,GetResRef(OBJECT_SELF),
                   GetLocation(OBJECT_SELF),FALSE,GetTag(OBJECT_SELF));
      DestroyObject(OBJECT_SELF);
    }
    SetLocalInt(OBJECT_SELF,"store_usage", x);
    // no need to ever reset the local int it will be destroyed along with
    // the old store.
}


               
               

               
            

Legacy_Shadooow

  • Hero Member
  • *****
  • Posts: 7698
  • Karma: +0/-0
Delete items in shop?
« Reply #5 on: October 05, 2015, 06:57:10 am »


               


If the intent is for the script to delete items in the store's inventory that only are there because a PC sold them, then the script may have to do more than that. Since (to my initial read) it deletes all items on the store, not just sold items from the player/




If thats the intent then this feature is implemented in community patch as an optional module switch.

 So either install it or copy the feature from it its in x2_def_mod_aqu script


               
               

               
            

Legacy_kalbaern

  • Hero Member
  • *****
  • Posts: 1531
  • Karma: +0/-0
Delete items in shop?
« Reply #6 on: October 05, 2015, 05:31:05 pm »


               

If the desire is to clean out all items a PC sells to ANY NPC Merchant, then try the following in your Module's OnUnAcquireItem event:


 


#include "x2_inc_switches"
void main()
{
object oItem = GetModuleItemLost();
if (GetObjectType(GetItemPossessor(oItem)) == OBJECT_TYPE_STORE) DestroyObject(oItem, 0.1f);
if (GetModuleSwitchValue(MODULE_SWITCH_ENABLE_TAGBASED_SCRIPTS) == TRUE)
{
SetUserDefinedItemEventNumber(X2_ITEM_EVENT_UNACQUIRE);
int nRet = ExecuteScriptAndReturnInt(GetUserDefinedItemEventScriptName(oItem),OBJECT_SELF);
if (nRet == X2_EXECUTE_SCRIPT_END)
{
return;
}

}

}

 


This will auto delete ALL sold items as they are sold to a merchant.