Jump to content

Help With Script


Pumpkin Carver
 Share

You are about to reply to a thread that has been inactive for 3130 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

Although I think it will be fairly clear from everything I write here, I might as well state I am new to scripting.  I've been around many years, but just decided to start trying to learn.  I've read through some of the tutorials, but it wasn't making much sense until I started trying to implement it.

Today I decided I'd try to make a simple script that would, on touch, give a random item from the object's inventory after a set amount of time.  Finding the information on giving the random item wasn't difficult, and that was executed and working very easily, however, it gives the item instantly, and in order for the overall item to function how I wanted, I need it to wait a set amount of time before it sends the random item out.

I read up on timers in the LSL wiki (which proved a bit confusing to me, as much of it does), then I came to the forums and searched quite a bit trying to figure out how to set this up correctly.  I tried a few things without any results other than the fact that now my script will not give out an item at all.  Simply put, the script is now doing nothing as far as I can tell.

I'm needing to know what I've done wrong here, and what the correct approach would have been.  I understand this is probably a stupid question, and that I need much more practice reading about scripting in general, but as I said before, I was having trouble grasping anything until I actually started trying to dive in.  I'm attaching the working script without a timer event and the script with how I THOUGHT the timer event should be placed.  I appreciate any help that can be provided.

WORKING WITHOUT TIMER EVENT - -

default

{
    touch(integer num_detected)

        {
            key id = llDetectedKey(0);
            
            integer randomIndex = llGetInventoryNumber(INVENTORY_OBJECT);
            
            randomIndex = (integer)llFrand(randomIndex);
            
            string itemName = llGetInventoryName(INVENTORY_OBJECT,randomIndex);
            
            llGiveInventory(id, itemName);
        }
     
}

NOT WORKING - - 

default

{
    touch_start(integer num_detected)

    {
        llSetTimerEvent(10);
    }
    
    timer()
    
    {
        key id = llDetectedKey(0);
        integer randomIndex = llGetInventoryNumber(INVENTORY_OBJECT);
        randomIndex = (integer)llFrand(randomIndex);
        string itemName = llGetInventoryName(INVENTORY_OBJECT,randomIndex);
        llGiveInventory(id, itemName);
        llSetTimerEvent(0);
    }
         
}
Link to comment
Share on other sites

You got the timer figured out properly.  Th only problem is that the timer event won't do anything because it has no idea who

key id = llDetectedKey(0);

is.  The llDetected* functions only make sense in events that actually detect things (touch*, collision*, and sensor* events).  That's a common mistake that even experienced scripters make from time to time.  So, the solution is to detect the user's UUID in the touch_start event and then pass that information as a global variable so that the timer event has access to it.

key gUUID;default{    touch_start(integer num_detected)    {        gUUID = llDetectedKey(0);        llSetTimerEvent(10.0);    }        timer()        {        integer randomIndex = llGetInventoryNumber(INVENTORY_OBJECT);        randomIndex = (integer)llFrand(randomIndex);        string itemName = llGetInventoryName(INVENTORY_OBJECT,randomIndex);        llGiveInventory(gUUID, itemName);        llSetTimerEvent(0.0);    }         }

 Now, if you are really clever, you anticipate the possibility that someone else may walk in and click your device during the ten seconds after the first person touched it.  If so, the second person will get an object and the first prson will be left scratching her head.  So, you disable the touch_start event:

key gUUID;integer gTouched;default{    touch_start(integer num_detected)    {        if (!gTouched)        {            gTouched = TRUE;            gUUID = llDetectedKey(0);            llSetTimerEvent(10.0);        }    }        timer()        {        integer randomIndex = llGetInventoryNumber(INVENTORY_OBJECT);        randomIndex = (integer)llFrand(randomIndex);        string itemName = llGetInventoryName(INVENTORY_OBJECT,randomIndex);        llGiveInventory(gUUID, itemName);        gTouched = FALSE;        llSetTimerEvent(0.0);    }         }
  • Like 1
Link to comment
Share on other sites

I think your first script would work fine if you just added one line with "llSleep".
It will wait the time you set it for and even if someone else click it it won't work until the sleep time is over.
Your script with the sleep line:

default{    touch(integer num_detected)        {            key id = llDetectedKey(0);                        integer randomIndex = llGetInventoryNumber(INVENTORY_OBJECT);                        randomIndex = (integer)llFrand(randomIndex);                        string itemName = llGetInventoryName(INVENTORY_OBJECT,randomIndex);                       llGiveInventory(id, itemName);        }     }
Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 3130 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...