Difference between revisions of "Random Object Vendor"

From Second Life Wiki
Jump to navigation Jump to search
m (minor code revision)
m (improved readability)
Line 4: Line 4:


<lsl>
<lsl>
//  Random object vendor by CodeBastard Redgrave
//  Drop items in the prim containing this script
//  Pay the set price amount and it will give you a random object


// Random object vendor by CodeBastard Redgrave
// Pieces stolen from:
// Drop items in the prim containing this script
//     Give inventory to whoever touches the prim, written by Sasun Steinbeck
// Pay the set price amount and it will give you a random object
// <3


// Pieces stolen from:
// Give inventory to whoever touches the prim, written by Sasun Steinbeck (get_inv_list is shweet <3)


list inventory; //holds a list of inventory items to give out on click.
integer price = 10;
list prices = [1];




get_inv_list()
list inventory_list()
{
{
     integer i;
    string thisScript = llGetScriptName();
     string name;
     integer numberOfItems = llGetInventoryNumber(INVENTORY_ALL);
 
 
     inventory = [];
    list listToBeReturned;
     for (i = 0; i < llGetInventoryNumber(INVENTORY_ALL); i++)
     string itemName;
 
     integer index;
     do
     {
     {
         name = llGetInventoryName(INVENTORY_ALL, i);
         itemName = llGetInventoryName(INVENTORY_ALL, index);
         if (name != llGetScriptName()) inventory += [name]; //add everything but this script to the list
         if (itemName != thisScript)
            listToBeReturned += [itemName];
     }
     }
     llOwnerSay("The following items have been found in this prim and will be given out on touch: " + llList2CSV(inventory) + ".");
     while (++index < numberOfItems);
     if (llGetListLength(inventory) > 1)
 
         llOwnerSay("These items will be put into a folder named " + llGetObjectName() + ". Rename this object to rename the destination folder.");
     return
         listToBeReturned;
}
}
 
 
default
default
{
{
    on_rez(integer start_param)
    {
        llResetScript();
    }
    changed(integer change)
    {
        if (change & (CHANGED_OWNER | CHANGED_INVENTORY))
            llResetScript();
    }
     state_entry()
     state_entry()
     {
     {
         get_inv_list();
         key owner = llGetOwner();
        llRequestPermissions(llGetOwner(),PERMISSION_DEBIT);
        llSetPayPrice(PAY_HIDE, prices);


        llRequestPermissions(owner, PERMISSION_DEBIT);
        llSetPayPrice(PAY_HIDE, [price, PAY_HIDE, PAY_HIDE, PAY_HIDE]);
     }
     }
 
 
  money(key id, integer amount)
    money(key id, integer amount)
     {
     {
         if (amount == (integer)llList2String(prices,0))
         if (amount == price)
         {
         {
            list listOfItems = inventory_list();


        integer num_items = llGetListLength(inventory);
            integer numberOfItems = llGetListLength(listOfItems);
        integer random_item = (integer)llFrand(num_items + 1);
            integer randomIndex = (integer)llFrand(numberOfItems);


        if (num_items < 1)
            if (numberOfItems)
             llInstantMessage(id, "No items to offer.");
             {
        else if (num_items == 1)
                key nameOfThisPrim = llGetObjectName();
            llGiveInventory(id, llList2String(inventory, 0));
                llGiveInventoryList(id, nameOfThisPrim, [llList2String(listOfItems, randomIndex)]);
        else //multiple items in a folder named after this prim's name
                llInstantMessage(id, "Your item is in a folder named '" + nameOfThisPrim + "'. Thank you!");
        {
            }
            llGiveInventoryList(id, llGetObjectName(), [llList2String(inventory, random_item)]);
            else
            llInstantMessage(id, "Your items have been given to you in a folder in your inventory named " + llGetObjectName() + ". Thank you!");
            {
        }
                llInstantMessage(id, "No items to offer.");
         
                llGiveMoney(id, amount);
            }
         }
         }
         else
         else
         {
         {
             llWhisper(0,"Sorry that is the wrong amount for this item.");
             llInstantMessage(id, "Sorry that is the wrong amount for this item.");
             llGiveMoney(id, amount);
             llGiveMoney(id, amount);
         }
         }
    }
 
    changed(integer change)
    {
        if (change & CHANGED_INVENTORY)
            get_inv_list(); //refresh inventory list
     }
     }
}
}
</lsl>
</lsl>


{{LSLC|Examples}}
{{LSLC|Examples}}
{{LSLC|Library}}
{{LSLC|Library}}

Revision as of 12:41, 18 October 2012

This script is a simple object vendor that gives out random items when you pay the fixed price. Just drop it in a prim, add items, and change the prices list variable to whatever price you want to sell the items. It will give out the item in a folder named like the container prim.

<lsl> // Random object vendor by CodeBastard Redgrave // Drop items in the prim containing this script // Pay the set price amount and it will give you a random object

// Pieces stolen from: // Give inventory to whoever touches the prim, written by Sasun Steinbeck


integer price = 10;


list inventory_list() {

   string thisScript = llGetScriptName();
   integer numberOfItems = llGetInventoryNumber(INVENTORY_ALL);
   list listToBeReturned;
   string itemName;
   integer index;
   do
   {
       itemName = llGetInventoryName(INVENTORY_ALL, index);
       if (itemName != thisScript)
           listToBeReturned += [itemName];
   }
   while (++index < numberOfItems);
   return
       listToBeReturned;

}

default {

   on_rez(integer start_param)
   {
       llResetScript();
   }
   changed(integer change)
   {
       if (change & (CHANGED_OWNER | CHANGED_INVENTORY))
           llResetScript();
   }
   state_entry()
   {
       key owner = llGetOwner();
       llRequestPermissions(owner, PERMISSION_DEBIT);
       llSetPayPrice(PAY_HIDE, [price, PAY_HIDE, PAY_HIDE, PAY_HIDE]);
   }
   money(key id, integer amount)
   {
       if (amount == price)
       {
           list listOfItems = inventory_list();
           integer numberOfItems = llGetListLength(listOfItems);
           integer randomIndex = (integer)llFrand(numberOfItems);
           if (numberOfItems)
           {
               key nameOfThisPrim = llGetObjectName();
               llGiveInventoryList(id, nameOfThisPrim, [llList2String(listOfItems, randomIndex)]);
               llInstantMessage(id, "Your item is in a folder named '" + nameOfThisPrim + "'. Thank you!");
           }
           else
           {
               llInstantMessage(id, "No items to offer.");
               llGiveMoney(id, amount);
           }
       }
       else
       {
           llInstantMessage(id, "Sorry that is the wrong amount for this item.");
           llGiveMoney(id, amount);
       }
   }

} </lsl>