////////////////////////
// Function GetNewInventoryName by ZenMondo Wormser
//
// This function returns a list of the names of
// inventory items added to an object's inventory
// since the last time the function was called.
//
// This function works by keeping a list of inventory items
// and comparing against that list as inventory changes.
//
// NOTE: If items are deleted between calls and nothing is added
// this function will return an empty list.
////////////////////////
list gOld_Inventory; //This stores the Inventory since the last function call.
//It is global so it persists between function calls.
//It may also be handy to have a list of your object's inventory.
list GetNewInventoryName()
{
list found_new = []; //This list will contain the names of New Inventory items.
integer inventory_type = INVENTORY_ALL; //Change to look at inventory type you want.
integer inventory_num = llGetInventoryNumber(inventory_type);
list new_inventory = []; //This list will contain the current inventory.
integer counter = 0;
while(counter < inventory_num)
{
new_inventory = (new_inventory=[]) + new_inventory + llGetInventoryName(inventory_type, counter);
counter ++;
}
integer list_length = llGetListLength(new_inventory);
counter = 0;
list scratch;
while(counter < list_length)
{
scratch = llList2List(new_inventory, counter, counter);
if(llListFindList(gOld_Inventory, scratch) == -1) //New Inventory Object
{
found_new += llList2String(scratch,0); //Add the name of the new inventory item to the found_new list
}
counter ++;
}
gOld_Inventory = new_inventory; //Store the Inventory List to be compared the next time the function is called.
return found_new; //Return a list of the new inventory items.
}
default
{
state_entry()
{
GetNewInventoryName(); //Populate the list on reset.
}
touch_start(integer num_detected)
{
///////////
// Example Use in touch_start()
// Because Multiple Items could have been
// added between calls we work through
// the list that is retunred.
///////////
list new_inv = GetNewInventoryName();
llSay(0, "New Inventory Items:");
integer list_len = llGetListLength(new_inv);
integer counter;
for(counter = 0; counter < list_len; counter++)
{
llSay(0, llList2String(new_inv, counter));
}
}
changed(integer mask)
{
//////////////////
// Example Used in changed()
// This is much simpler, as changed()
// will be triggered each time something
// is added to the object's inventory
// resulting in only one result in the
// returned list from GetNewInventoryName()
////////////////////
if(mask & CHANGED_INVENTORY)
{
list new_inv = GetNewInventoryName();
llSay(0, "New Inventory Item:" + llList2String(new_inv, 0));
}
}
}
Examples