Pumpkin Carver Posted September 24, 2015 Share Posted September 24, 2015 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 More sharing options...
Rolig Loon Posted September 24, 2015 Share Posted September 24, 2015 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); } } 1 Link to comment Share on other sites More sharing options...
Pumpkin Carver Posted September 24, 2015 Author Share Posted September 24, 2015 Thank you so much! And for the second part, this is something I would not have considered if you hadn't pointed it out. I didn't know that that could happen, so thanks for that info! Link to comment Share on other sites More sharing options...
Rolig Loon Posted September 24, 2015 Share Posted September 24, 2015 Yeah, half of writing good scripts is anticipating how users are likely to mess up, and getting there first. Failsafes can make a script a lot bigger, but they make the script more user-friendly and minimize the trouble calls you'll get later. Link to comment Share on other sites More sharing options...
Bent Luik Posted September 25, 2015 Share Posted September 25, 2015 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 More sharing options...
Recommended Posts
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