Jump to content

Random Timer


thesammy58
 Share

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

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

Recommended Posts

This is very irritating... I copied in what you wrote and now it still doesn't seem to be choosing a random time. I can predict in my head when the timer is going to go off again, and that's not what I want. I'm doing the math and everything but it just doesn't seem to be choosing a random number.

 

I have the timer going off over and over again, like in a loop. Should I reset the script each time it goes off or something?

Link to comment
Share on other sites

llFrand(X) produces a random number between X and zero if X is negative, and between 0 and X is X is positive. You don't want a negative timer value (though it would be super cool if we could go back in time!),  so X must always be positive. If you want a random time that is one second at the minimum and 10 seconds at the maximum (lets call those two values "min" and "max"), then llFrand(X) alone won't do the job, as it alway has the potential to produce... zero. 

So, you must add something to the llFrand(X) result to keep it at or above your "min" value. And, since llFrand(X) can go all the way to zero, the value you must add actually is your min value. So you've now got something of the form...

min + llFrand(X)

You're not done yet though. What do you use as the value of X if you want the result of that statement to have your maximum value of 10? You can't use 10 as "max", as you've now added "min" to it. So you must decrease max by exactly the value of min, producing...

min + llFrand(max-min)

And there's the value you'll use in your llSetTimerEvent() call.

So, to make your script easy to understand and edit, declare two global variables (min and max) at the beginning of the script and set their values as you wish. Then, rather than trying to do the math every time you want to change the times, you just plug in the minimum and maximum times you want and let the LSL compiler do the heavy lifting.

Remember, as in the LSL example page, you must call llSetTimerEvent both initially, to get the timer going, and within the timer event itself, to keep it going. Should you wish to stop the timer, you must have llSetTimerEvent(0) in whatever event handler (touch_start() in the example) you want to cause the stoppage.

Yes, we're making you do some thinking here. We want you to join us in the ranks of people who can raise Holy Hell by writing scripts.

Good luck, Sammy!

  • Like 1
Link to comment
Share on other sites

default{     state_entry()     {          llResetTime();    // This is just to set a marker for the start time, to demonstrate          llSetTimerEvent(1.0 + llFrand(9.0));     }     timer()     {          llSay(0, "This timer was triggered " + (string)llGetAndResetTime() + " seconds ago.");     }}

 

Link to comment
Share on other sites

In that case....

default{     state_entry()     {          llResetTime();    // This is just to set a marker for the start time, to demonstrate          llSetTimerEvent(1.0 + llFrand(9.0));     }     timer()     {          llSay(0, "This timer was triggered " + (string)llGetAndResetTime() + " seconds ago.");          llSetTimerEvent(1.0 + llFrand(9.0));   // a NEW timer interval.        }}

 

Link to comment
Share on other sites

llResetScript is a clumsy way out.  You should never have to reset a script just to change the clock.  It might have helped if we knew which of Maddy's two possible cases you were trying for.  I gave you sample scripts for both now.  Pick one.  :smileywink:

Link to comment
Share on other sites

Put another way, llResetScript() lacks finesse.  Why would you want to stop an entire script that may be doing dozens of things -- listening, moving things around, turning lights onand off --- just because you want to change the clock?  As Maddy says, resetting the script will make it forget everything that it was doing, which is a good practical reason for not doing it.  As far as I'm concerned, it's just inelegant, like using a shotgun to kill a fly.

Link to comment
Share on other sites


Rolig Loon wrote:

As far as I'm concerned, it's just inelegant, like using a shotgun to kill a fly.

Geez Rolig, that's an awesome way to kill a fly. Couldn't you find a better analogy, like using using a jack hammer to drive a scr... no wait... wearing a padded bra to attract a... never mind.

...goes looking for a fly to kill.

Link to comment
Share on other sites

To my mind, llResetScript() is one of the two functions beginner scripters should be very careful about using (the other, for rather different reasons, is llSleep()).

The trouble with llResetScript() is that it can, indeed, fix a multitude of problems.   However, because it's not fixing whatever the underlying cause of the problem is, it can make the the script very difficult to adapt in future.  Better, by far, to get to the root of the problem and fix that, so you don't need to reset everything, which often you will find you really don't want to anyway.

Save llResetScript for circumstances where you're sure you want to wipe everything (a change of owner, for example).

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 3637 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...