Author Sticky: Post Working Scripts Here
Aviston Posted: Monday, 01 July 06:08AM
This is a script for a chest that refills its inventory after a set period of time. I believe the popular way of doing this as of now is to destroy the chest and respawn it. With this script, the original chest remains in tack and just its contents are destroyed and refilled. It will also allow a creature to spawn the first time the chest is opened after it has been reset.

There are a few things that must be edited in order for it to work properly. First, the value must be changed to how many heartbeats you want to wait before the chest resets (the default is 6). Second, you must change the CreateItemOnObject() in the OnOpen script to reflect the item you want to clone (and add more function calls to CreateItemOnObject() if you want the chest to contain more than one item). Lastly, you must uncomment the CreateObject() call in the OnOpen script and modify it to reflect the creature you want to clone if you want a creature to spawn. I have added comments throughout the code so these things should be quite easy to find.

First, the OnHeartbeat script:
NWScript:
// Refilling Chest - Used in OnHeartbeat for the chest in question

//

// This script causes a chest to refill its contents after a predetermined

// amount of time without recreating itself and destroying the original chest.

//

// --Aviston--

void main()

{

    // If the chest has already reset, we do not want to do anything

    if (GetLocalInt(OBJECT_SELF, "NW_DO_ONCE") == 0)

        return;

    // Grab the variable value from respawn_counter and store in nCounter

    int nCounter = GetLocalInt(OBJECT_SELF, "respawn_counter");

    // Increment nCounter by one (this will happen every 6 seconds or so)

    nCounter++;

    // Change respawn_counter to the new, incremented value of nCounter

    SetLocalInt(OBJECT_SELF, "respawn_counter", nCounter);

    // Test to see if nCounter has grown large enough, if so, set the local

    // variable NW_DO_ONCE back to 0, which will be used in the OnOpen script

    // Note: Change the value you are testing nCounter against depending on

    // how long you want it to wait before it respawns.  Multiply the value

    // you choose by 6 seconds to get a rough estimate on actual respawn time

    if (nCounter > 6)

        SetLocalInt(OBJECT_SELF, "NW_DO_ONCE", 0);

}



Next, the OnOpen script:

NWScript:
// Refilling Chest - Used in OnOpen for the Chest in question

//

// This script causes a chest to refill its contents after a predetermined

// amount of time without recreating itself and destroying the original chest.

//

// -Aviston-

#include "NW_O2_CONINCLUDE"

// This function is called from main() to refill the chests contents.  You

// will have to manually make a CreateItemObject() for each object you want

// replaced in the chest.

void FillInventory()

{

    object oObject = GetFirstItemInInventory();

    // First, do a while loop to destroy any contents that the chest might

    // contain

    while (oObject != OBJECT_INVALID)

    {

        DestroyObject(oObject);

        oObject = GetNextItemInInventory();

    }

    // Next, recreate the items you wish the chest to contain.  Change the

    // blueresref to whatever you wish.  You can add as many CreateItemOnObject

    // calls as you want and/or the chest's inventory can hold.

    CreateItemOnObject("NW_WAXHN001");

}

void main()

{

    // Check to see if NW_DO_ONCE is set to 0, if it isn't, do nothing

    if (GetLocalInt(OBJECT_SELF,"NW_DO_ONCE") != 0)

        return;

    // Set the respawn_counter to 0, this is used by the OnHeartbeat script

    // to decide when it's time to respawn

    SetLocalInt(OBJECT_SELF, "respawn_counter", 0);

    // Set the NW_DO_ONCE variable to 1 to indicate it has now been opened and

    // needs to start waiting for a respawn

    SetLocalInt(OBJECT_SELF,"NW_DO_ONCE",1);

    ShoutDisturbed();

    // If we've made it this far, we can now safely repopulate the chest's

    // inventory

    FillInventory();

    // Here we spawn a monster each time the chest has items and is opened

    // just for fun.  Uncomment this if you wish a monster to be

    // spawned each time the chest is opened after it resets.  Also, change

    // the blueresref to the creature you'd like to spawn.

    // CreateObject(OBJECT_TYPE_CREATURE, "NW_GOBLINA", GetLocation(OBJECT_SELF));

}