User:Tribal Toland/slaves

From Second Life Wiki
< User:Tribal Toland
Revision as of 09:36, 4 December 2007 by Tribal Toland (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Slave Scripts !!

In general these are used fairly often.

First, we get the amount of slaves that are in the object:

integer slaveCount = 0;
get_slaves()
{
   slaveCount = 0;
   integer i = 0;
   integer num = llGetInventoryNumber(INVENTORY_SCRIPT);
   do
   {
       string name = llGetInventoryName(INVENTORY_SCRIPT,i);
       if(llSubStringIndex(llToLower(name),"slave") == 0)
       {
           ++slaveCount;
       }
       ++i;
   }  
   while(i < num);
   llOwnerSay("Number of slaves: "+(string)slaveCount);
}

If you call this in the state_entry event, it will search the objects inventory for scripts where the name begins with "slave" (case in-sensitive).

Lets break this bit of code down:

get_slaves() //name of function
{
   slaveCount = 0; //sets slaveCount to 0 so we don't double up
   integer i = 0; //create variable for loop
   integer num = llGetInventoryNumber(INVENTORY_SCRIPT); //this gets the number of scripts in the objects inventory
   do //start the do-while loop
   {
       string name = llGetInventoryName(INVENTORY_SCRIPT,i); //gets the name of the script at position i in the objects inventory
       if(llSubStringIndex(llToLower(name),"slave") == 0) //puts the name in lower case. If the name has slave at the beginning increase the number of slaves
       {
           ++slaveCount; //increment slaveCount by 1
       }
       ++i; //increment the loop variable by 1. This stops the loop being infinite 
   }  
   while(i < num); //do the do-while loop whilst the variable i (loop variable) is less than the num variable (number of scripts in object)
   llOwnerSay("Number of slaves: "+(string)slaveCount); //Say out loud the number of slaves in inventory
}

You can call this function in the state_entry event.

default
{
    on_rez(integer val)
    {
        llResetScript();
    }
    
    state_entry()
    {
        get_slaves();
    }
}

Next, we need to create our function that decides what slave to use.