Author Sticky: Post Working Scripts Here
Morz Posted: Tuesday, 02 July 04:05PM
This is a script to allow your Mobs to pick up items in body bags near by them left after another creature dies. So, it's essentially a Janitor script (since an empty body bag destroys itself), with the benefits that you don't need a separate janitor object and it makes a little more thematic sense that mobs would loot bodies themselves.

The mob does a victory animation when they loot something, and will equip items they get which are better then what they already have.

One quirk is that mobs can only hold a certain amount of items in inventory, so after they fill up, while they will keep picking items, those items will 'go away'. Useful to clear up the trash, but sometimes they'd lose something they'd want more than the stuff they're filled up with. Also note that mobs near the PCs which aren't in combat might very well snatch something for themselves that the PC is trying to get, the same way a groupmemeber might when they're both looting the same thing.

This is written as a user defined event set to OnHeartbeat.


NWScript:
void main()

{

    object oBodybag;

    int nEvent=(GetUserDefinedEventNumber());

    if (nEvent=1001)

    {

 

        if (GetDistanceToObject(GetNearestObjectByTag("Body Bag", OBJECT_SELF,1)) > 0.0 && GetDistanceToObject(GetNearestObjectByTag("Body Bag", OBJECT_SELF,1)) < 10.0)//looks for existing bags near them. change 10.0 to whatever distance you feel is more appropriate) 

        {

        oBodybag = GetNearestObjectByTag("Body Bag", OBJECT_SELF,1);

        ActionTakeItem(GetFirstItemInInventory(oBodybag), oBodybag);//the ActionTakeItem function makes them move to the container so there's no need for a move to object function

        ActionTakeItem(GetNextItemInInventory(oBodybag), oBodybag);//they'll grab the first couple of items, then go after what's left if the body bag is still around during the next hearbeat. If the bag is empty it will have self destructed and the guy will move to the next one nearby if there is one.

        DelayCommand(2.0,ActionPlayAnimation(ANIMATION_FIREFORGET_VICTORY1));//seems appropriate that they'd be happy that they got some loot

        ActionEquipMostDamagingRanged();//will equip best melee if there's no ranged

        ActionEquipMostEffectiveArmor();

        }

     }

}