Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

setTimeout ("something", xxx)

nonnux white
NN Dez!gns
Join date: 8 Oct 2004
Posts: 90
02-16-2005 14:13
hi geeks! got a big problem for u to resolve... (maybe no solution, but...)

let me try to explain, is gonna be hard - my native language is not english...

i want to build a function to trigger an action ("something";) after xxx, just like the setTimeout in Javascript.

ok, by this time u must be thinking, yeah we got llSetTimerEvent, what kind of wuestion is that?

i want to trigger severals actions, not just "something", also "another thing", "the thing", "the lord of the things", "etc thing :)", and i want to trigger it at diferent times (allawys random).

problem 1:
- i cannot past arguments into timer() with llSetTimerEvent. i could use a global list, to store the actions, is not a big problem...

problem 2:
- i want to trigger it once only! and be able to trigger the other actions. if i stop the timer event, to stop this action after it is triggered, it will just stop listen for other timers...


oh my... this is really hard to explain, i hope Eggy L. comes here and sharpens some text :)

nonnux
_____________________
Rhombur Volos
King of Scripture & Kebab
Join date: 6 Oct 2004
Posts: 102
02-16-2005 14:29
Globals will be the only way to pass arguments from any event, function or state into a timer-event.
To stop the timer use the syntax llSetTimerEvent(0.0);

example:

CODE


integer w;

default {
state_entry()
{
w = 12;
llSetTimerEvent(2.0);
}

timer()
{
for(integer n = 0; n<w;n++)
{
llSay(0, "spam, spam, spam, spam.");
}
llSetTimerEvent(0.0);
}
}



all you need to do to have a particular command run only once is to use a global integer that defines wether the command has been run or not.

As in:

CODE


integer commandhasrun = 0;

default {
state_entry()
{
w = 12;
llSetTimerEvent(2.0);
}

timer()
{
if(!commandhasrun) {
llSay(0, "spammety spaaaaaam!");
commandhasrun = 1;
}
}
}
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
02-16-2005 14:33
Rhombur:

Bad!
Using global variables that are letters.
Anyway i've reserved global w for my listen handles. :D
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey
nonnux white
NN Dez!gns
Join date: 8 Oct 2004
Posts: 90
02-16-2005 14:36
From: Rhombur Volos
Globals will be the only way to pass arguments from any event, function or state into a timer-event.
To stop the timer use the syntax llSetTimerEvent(0.0);


your example is the basic way to use timers...

imagine i want to say "spam spam spam" * 12 after 2 seconds, and after 5 seconds say "do it again", each 10 seconds "what are u doing", after llFrand(10) say "this script is boring"...

can this be done?

note: using 1 only script
_____________________
Rhombur Volos
King of Scripture & Kebab
Join date: 6 Oct 2004
Posts: 102
02-16-2005 14:39
From: Strife Onizuka
Rhombur:

Bad!
Using global variables that are letters.
Anyway i've reserved global w for my listen handles. :D


Meh. I usually don't do that, but i tend to use whatever syntax i find handy.
People complaining about my code usually get a revised version that uses jumps to replace all for- and while-loops xD
nonnux white
NN Dez!gns
Join date: 8 Oct 2004
Posts: 90
02-16-2005 14:40
From: Rhombur Volos


all you need to do to have a particular command run only once is to use a global integer that defines wether the command has been run or not.

As in:

CODE


integer commandhasrun = 0;

default {
state_entry()
{
w = 12;
llSetTimerEvent(2.0);
}

timer()
{
if(!commandhasrun) {
llSay(0, "spammety spaaaaaam!");
commandhasrun = 1;
}
}
}


but, i think a script like that with many llSetTimerEvent's will use to much resources... i could stop the timer, just before trigger it again, but, like i said, i have many timer events, and need to start much more

i think about 1 day of runniing, the script will be out of memory, or too much timers in the queue
_____________________
Rhombur Volos
King of Scripture & Kebab
Join date: 6 Oct 2004
Posts: 102
02-16-2005 14:42
From: nonnux white
your example is the basic way to use timers...

imagine i want to say "spam spam spam" * 12 after 2 seconds, and after 5 seconds say "do it again", each 10 seconds "what are u doing", after llFrand(10) say "this script is boring"...

can this be done?

note: using 1 only script


You could use llGetTime() or a counter that checks how many times the timer event has been run, but making a single timer behave as if it were several would take some work.
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
02-16-2005 14:42
OffTopic: Yeah you must be a basic programer too :P We don't die just GOSUB without a RETURN.
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey
nonnux white
NN Dez!gns
Join date: 8 Oct 2004
Posts: 90
02-16-2005 14:58
From: Strife Onizuka
OffTopic: Yeah you must be a basic programer too :P We don't die just GOSUB without a RETURN.

we are all basic programmers since LSL is a basic language. so instead of offtopic blaming, get us a solution for this function, just make it work like the javascript setTimeout, and i will galdly be called basic
_____________________
Rhombur Volos
King of Scripture & Kebab
Join date: 6 Oct 2004
Posts: 102
02-16-2005 15:50
From: nonnux white
we are all basic programmers since LSL is a basic language. so instead of offtopic blaming, get us a solution for this function, just make it work like the javascript setTimeout, and i will galdly be called basic


Basic is a programming language, one quite different from LSL. ;)

Oh, and i think that post was directed at me. (jump is a variant of the Basic command GOTO, which some programmers get seisures from even noticing in any source code)
SuezanneC Baskerville
Forums Rock!
Join date: 22 Dec 2003
Posts: 14,229
02-16-2005 17:00
I think this may be doing something along the lines of what you are looking for.

CODE
integer ticks = 0 ;
integer random = 5 ;
default
{
state_entry()
{ llSetTimerEvent(1) ;
}

timer()
{ if ( (ticks % 2) == 0 )
{ llWhisper( 0, "spam spam spam" ) ;
}
if ( ( ticks % 5 ) == 0 )
{ llWhisper( 0, "do it again") ;
}
if ( ( ticks % random) == 0 )
{ llWhisper( 0, "this script is boring" ) ;
random = ((integer) llFrand(10)) + 1 ;
}
ticks += ;
}


}
_____________________
-

So long to these forums, the vBulletin forums that used to be at forums.secondlife.com. I will miss them.

I can be found on the web by searching for "SuezanneC Baskerville", or go to

http://www.google.com/profiles/suezanne

-

http://lindenlab.tribe.net/ created on 11/19/03.

Members: Ben, Catherine, Colin, Cory, Dan, Doug, Jim, Philip, Phoenix, Richard,
Robin, and Ryan

-
DoteDote Edison
Thinks Too Much
Join date: 6 Jun 2004
Posts: 790
02-16-2005 18:17
My attempt without using a constant 1-second timer:

CODE

integer ticks = 0;

default {
state_entry() {
llSetTimerEvent(2);
}

timer() {
if(ticks == 0) {
llWhisper(0, "first-run of spam spam spam");
ticks = 1;
llSetTimerEvent(5.0);
llReturn;
}
if(ticks == 1) {
llWhisper(0, "second-run of spam spam spam");
ticks = 2;
llSetTimerEvent(10.0);
llReturn;
}
if(ticks == 2) {
llWhisper(0, "third-run of spam spam spam");
ticks = 3;
llSetTimerEvent(llFrand(10) + 1);
llReturn;
}
if(ticks == 3) {
llWhisper(0, "random-run of spam spam spam");
}
}
}
nonnux white
NN Dez!gns
Join date: 8 Oct 2004
Posts: 90
02-16-2005 21:01
oki thx all for the help, we are close to a possible solution, but this is not yet my main problem.

i started the tread, asking for a function to do that. the values i used are just for testing purpose. and there are much more time events needed, so this ticker is not the solution.

i want to be able, at any time in my script to call the function:
CODE

setTimeout(string action, integer seconds){
doSomethingHere();
}


each time i call setTimeout(), will be with diferents values, and i dunno how many setTimeout's i will need. the only thing that is somehow fixed is the "string action". maybe 10 diferent actions... i started coding with this ticks, but i think i got to use a ticker for each action, or a function for each action, but i stoped because it was already too much non sense, and too slow...

i think i'm gonna give up this script and this function, since it looks like there is not solution, like i said in the opening thread

thx for all brainstorm!
_____________________
DoteDote Edison
Thinks Too Much
Join date: 6 Jun 2004
Posts: 790
02-16-2005 22:05
Okay, there's always a way. If you want to randomly call the timer event at random intervals, yet be able to sustain a particular interval until the script is ready to generate a new random interval:

use 2 globals - the first will be TRUE or FALSE as to whether or not the timer should continue at the same interval. The second global is an integer representing the number of times to repeat a certain timer interval... if it's -1, then repeat until told otherwise.

CODE

integer repeat = FALSE;
integer numRepeats = 0;

default {
state_entry() {
llSetTimerEvent(10);
}
timer() {
if(!repeat) {
llWhisper(0, "I'll only say this once!");
llSetTimerEvent(0);
}
if(repeat && numRepeats == 0) {
llSetTimerEvent(0);
}
if(repeat && numRepeats > 0) {
llWhisper(0, "I'll repeat this until numRepeats equals 0!");
numRepeats--;
}
}
}


Obviously, you have to include code somewhere to set the timer for the interval you need, and to set the repeat and numRepeats globals.
DoteDote Edison
Thinks Too Much
Join date: 6 Jun 2004
Posts: 790
02-16-2005 22:18
If you want to do it your way (and if you're not using a sensor), you could hack the sensor functions/events.

call llSensorRepeat() and have it search the minumum arc and distance and search for something it will NEVER find. You can have it search at the "timeout" rate you prefer. The sensor will call the no_sensor() event at your "timeout" rate. Put the code to execute in that event. When the "timeout" period is over, call llSensorRemove(). When the script needs to "timeout" again, repeat the process.
Christopher Omega
Oxymoron
Join date: 28 Mar 2003
Posts: 1,828
02-16-2005 22:52
CODE

// The timeout trigger time can only be as accurate as
// the timer interval set here, for example, if you called:
// setTimeout("foo", 2.4) and the TIMER_INTERVAL was 1,
// the "foo" timeout will be triggered after 3 seconds, not 2.4.
// Note that a smaller interval creates more calls to timer(),
// and may contribute to more lag.
float TIMER_INTERVAL = 1;
list timeoutData;
float timePassed;
setTimeout(string identifyer, float time) {
timeoutData += time + timePassed;
timeoutData += identifyer;

timeoutData = llListSort(timeoutData, 2, TRUE);
}

timeoutFunctions(string identifyer) {
// Declare the functions that need to trigger on the timeout here.
}

default {
state_entry() {
llSetTimerEvent(TIMER_INTERVAL);
}

timer() {
timePassed += TIMER_INTERVAL;

integer i;
integer numTimers = llGetListLength(timeoutData);
for (i = 0; i < numTimers; i += 2) {
float triggerTime = llList2Float(timeoutData, i);
if (triggerTime <= timePassed) {
// Trigger the appropriate timeout function.
timeoutFunctions(llList2String(timeoutData, i + 1));
// Timeout has been triggered, remove it from list.
timeoutData = llDeleteSubList(timeoutData, i, i + 1);
} else {
// Since the list is sorted, when we encounter a timeout
// that shouldn't trigger yet, we can stop iterating over the list.
return;
}
}

}
}


Here's an example implementation:
CODE

float TIMER_INTERVAL = 1;

list timeoutData;
float timePassed;
setTimeout(string identifyer, float time) {
timeoutData += time + timePassed;
timeoutData += identifyer;

timeoutData = llListSort(timeoutData, 2, TRUE);
}

someFunction() {
llSay(0, "Five seconds elapsed!");
// Do something...
}

timeoutFunctions(string identifyer) {
// Declare the functions that need to trigger on the timeout here.
if (identifyer == "someFunction") {
someFunction();
}
}

default {
state_entry() {
llSetTimerEvent(TIMER_INTERVAL);
// someFunction will trigger after 5 seconds.
setTimeout("someFunction", 5);
}

timer() {
timePassed += TIMER_INTERVAL;

integer i;
integer numTimers = llGetListLength(timeoutData);
for (i = 0; i < numTimers; i += 2) {
float triggerTime = llList2Float(timeoutData, i);
if (triggerTime <= timePassed) {
// Trigger the appropriate timeout function.
timeoutFunctions(llList2String(timeoutData, i + 1));
// Timeout has been triggered, remove it from list.
timeoutData = llDeleteSubList(timeoutData, i, i + 1);
} else {
// Since the list is sorted, when we encounter a timeout
// that shouldn't trigger yet, we can stop iterating over the list.
return;
}
}

}
}


==Chris
nonnux white
NN Dez!gns
Join date: 8 Oct 2004
Posts: 90
02-16-2005 23:04
From: Christopher Omega

The timeout trigger time can only be as accurate as
the timer interval set here, for example, if you called:
setTimeout("foo", 2.4) and the TIMER_INTERVAL was 1,
the "foo" timeout will be triggered after 3 seconds, not 2.4.
Note that a smaller interval creates more calls to timer(),
and may contribute to more lag.
==Chris


alright!!!!

i could try to script this all day long, and also tomorow, i bet with u i couldnt do better then this!

the timer interval is good enought, since is a looong running script!

thx a lot! thx also for making it so silent, 1 second timer is good...

thx all, i hope to come back soon, with the final project!
_____________________
Harris Hare
Second Life Resident
Join date: 5 Nov 2004
Posts: 301
02-18-2005 09:11
After skimming this thread I wondered...

Why not just have a separate script for handeling the SetTimeout function? You could use MessageLinked to pass the "SetTimeout script" commands for activating its own timer which would thus remain separate from the main script's own timer events.
nonnux white
NN Dez!gns
Join date: 8 Oct 2004
Posts: 90
02-18-2005 09:48
From: Harris Hare
After skimming this thread I wondered...

Why not just have a separate script for handeling the SetTimeout function? You could use MessageLinked to pass the "SetTimeout script" commands for activating its own timer which would thus remain separate from the main script's own timer events.


i understand that it would be much more easy doing that. but i need to do it inside the main script, since i only have this main script. security purposes, and it is a script for die. i mean, is gonna die after 3 minutes, or reseted. no problem.

if u want to check out the work, just IM me in world
_____________________