Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

two timers

Gearsawe Stonecutter
Over there
Join date: 14 Sep 2005
Posts: 614
03-28-2009 15:05
When you wish you had two timers. Using the SensorRepeat as a timer. I would never use it for a fast timer. Something like a timer to remove a listen handle would be a good use when you are using the timer for something else . Warning if your sensor should happen to sense something you will need to have an empty sensor() event. but since it is impossible to have an agent with name of "none" and key of "none" you should not have to worry about it.

default
{
state_entry()
{
llSensorRepeat("none", "none", AGENT, 0.01, PI, 10.0);
llSetTimerEvent(1.0);
}
timer()
{
llOwnerSay("timer";);
}
no_sensor()
{
llOwnerSay("no_sense timer";);
}
}
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
03-28-2009 16:40
You should be able to do something like this too:

CODE

float TIMER1_PERIOD = 3.0;
float TIMER2_PERIOD = 5.5;
float TIMER_EPSILON = 0.03;

float nextTimer1Event;
float nextTimer2Event;

float min(float a, float b)
{
if (a < b)
{
return a;
} else
{
return b;
}
}

startTimers()
{
nextTimer1Event = TIMER1_PERIOD;
nextTimer2Event = TIMER2_PERIOD;

llSetTimerEvent(min(TIMER1_PERIOD, TIMER2_PERIOD));
}

handleTimerEvent()
{
llSetTimerEvent(0.0);

float nextEvent;
if (nextTimer1Event < nextTimer2Event)
{
nextEvent = min(TIMER1_PERIOD, nextTimer2Event-nextTimer1Event);
nextTimer1Event += TIMER1_PERIOD;

timer1();
} else
{
nextEvent = min(TIMER2_PERIOD, nextTimer1Event-nextTimer2Event);
nextTimer2Event += TIMER2_PERIOD;

timer2();
}

if (nextEvent < TIMER_EPSILON)
{
handleTimerEvent();
} else
{
llSetTimerEvent(nextEvent);
}
}

timer1()
{
// Do stuff associated with timer 1
}

timer2()
{
// Do stuff associated with timer 2
}


state_entry()
{
state_entry()
{
startTimers();

// ...
}

timer()
{
handleTimerEvent();
}
}
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
03-28-2009 23:17
another method would be to use a least common denominator for the set timer call and a modulus function (beware of floating point errors) in the timer to see if that codes runs on that event pass, or just a simple if switching mechanism.

an easy way to set timeouts, in addition to a regular running timer is to have it check against a list that saves a time code and check it vs current time
_____________________
|
| . "Cat-Like Typing Detected"
| . This post may contain errors in logic, spelling, and
| . grammar known to the SL populace to cause confusion
|
| - Please Use PHP tags when posting scripts/code, Thanks.
| - Can't See PHP or URL Tags Correctly? Check Out This Link...
| -