Author Sticky: Post Working Scripts Here
tealeaf Posted: Sunday, 30 June 07:55AM
This is a script to create an automatic TrashCan object that will keep your areas clean of unwanted loot that people didn't pick up (such as countless fire beetle bellies, etc.).

To use it, create a blueprint of an invisible object and put it in your palette. Name it "TrashCan". Then, attach the following script to onHeartbeat event, and the TrashCan is ready.

Now, simply put an instance of this TrashCan on every zone that you want to keep clean, and you're done. If you read the script you can easily adjust how long loot stays on the ground. Currently it defaults to 20 heartbeats.

NWScript:
// Copyright (C) 2002, Leo Lipelis

// need to declare this function ahead of its use

// to avoid compilation error;

// this function will destroy an object and everything

// inside of it recursively

void TrashObject(object obj)

{

    // if this is not a container, just destroy it and we're done

    if (GetHasInventory(obj) == FALSE) {

        DestroyObject(obj);

    } else {

        object oItem = GetFirstItemInInventory(obj);

        // destroy everything in the inventory first

        while (oItem != OBJECT_INVALID)

        {

            TrashObject(oItem);

            oItem = GetNextItemInInventory(obj);

        }

        // destroy the container itself

        DestroyObject(obj);

    }

}

void main()

{

    int iItemDropLifeSpan = 20;

    // iCounterMax must always be greater than iItemDropLifeSpan

    int iCounterMax = 1000;

    string sTrashMark = "iTrashMark";

    int iCounter = GetLocalInt(OBJECT_SELF, "iCounter");

    iCounter++;

    iCounter = iCounter % iCounterMax;

    if (iCounter == 0) { iCounter++; } // 0 is reserved

    SetLocalInt(OBJECT_SELF, "iCounter", iCounter);

    object obj = GetFirstObjectInArea();

    while (obj != OBJECT_INVALID)

    {

        int iObjType = GetObjectType(obj);

        // match based on type first, because comparing int

        // is faster than string; wrong types don't reach

        // string comparison

        if (iObjType == OBJECT_TYPE_PLACEABLE)

        {

            string sTag = GetTag(obj);

            // this assumes that every item drop from a monster

            // comes in a placeable container with a tag name

            // of "Body Bag"; it seems to be true, but if this

            // assumption is false, this code needs to be fixed

            if (sTag == "Body Bag")

            {

                int iTrashMark = GetLocalInt(obj, sTrashMark);

                if (iTrashMark == 0) {

                    SetLocalInt(obj, sTrashMark, iCounter);

                } else {

                    int iTimeAlive = 0;

                    if (iCounter >= iTrashMark) {

                        iTimeAlive = iCounter - iTrashMark;

                    } else {

                        iTimeAlive = iCounterMax - iTrashMark + iCounter - 1;

                    }

                    if (iTimeAlive > iItemDropLifeSpan) {

                        TrashObject(obj);

                    }

                }

            }

        }

        obj = GetNextObjectInArea();

    }

}