Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Blinking color for some time

Seagel Neville
Far East User
Join date: 2 Jan 2005
Posts: 1,476
12-12-2005 18:51
Hello there,
Although I thought this was easy, I couldn't do well. :( So please anyone, help me.

I want an object to blink black and white for ten seconds when I touched it.
CODE
integer condition;

default
{
touch_start(integer total_number)
{
llSetTimerEvent(10);
do
{
llSetColor(<0, 0, 0>, ALL_SIDES);
llSleep(0.1);
llSetColor(<1, 1, 1>, ALL_SIDES);
llSleep(0.1);
}
while(!condition);
}
timer()
{
condition = !condition;
}
}
I don't know how to use do-while loop correctly, either. I thought that it would stop when condition was changed, but it didn't. Please help me. Thanks.
_____________________
:) Seagel Neville :)
Osgeld Barmy
Registered User
Join date: 22 Mar 2005
Posts: 3,336
12-12-2005 19:15
using no timers

CODE

integer x;
default
{
touch_start(integer whatever)
{
for (x = 1; x <= 5; x++)
{
llSleep(1);
llSetColor(<0,0,0>,ALL_SIDES);
llSleep(1);
llSetColor(<1,1,1>,ALL_SIDES);
}
}
}
Kala Bijoux
Material Squirrel
Join date: 16 Nov 2004
Posts: 112
12-12-2005 19:47
The reason your while loop didn't stop is because the script is locked inside that while loop. llSleep blocks the script, but your touch_start function never returns. So even though the timer fires, the timer event is sitting in the queue waiting to be processed, and timer() never gets called because the script is still executing the infinite loop in the touch_start function. Try adding some llOwnerSay debug messages, and you'll see what's happening. For instance, add

CODE
llOwnerSay("condition = " + (string)condition);


somewhere inside the while loop, and add

CODE
llOwnerSay("handling timer event");
inside the timer handler.

In order to make it work, you'll either have to do what Osgeld suggested, which is to make sure the function returns, and doesn't get locked into an endless loop. If you want to use timers, you'll have to do the blinking inside the timer event, and keep count of how many you've done. If you want to do it with a 0.1s timer (not recommended :), but it looks like you want the eyes to blink really fast for 10 seconds)

CODE

integer counter = 0;
integer blinkState = 0;

default
{
touch_start(integer total_number)
{
counter = 0;
llSetTimerEvent(0.1);
}

timer()
{
if (blinkState == 0)
{
llSetColor(<0, 0, 0>, ALL_SIDES);
blinkState = 1;
}
else
{
llSetColor(<1, 1, 1>, ALL_SIDES);
blinkState = 0;
}

counter++;
if (counter >= 100) // 10 seconds, 0.1s timer, so that's 100 ticks
{
llSetTimerEvent(0);
}
}
}


With SL's lag, I'm not sure that will be very predictable - it requires the code inside the timer function to execute within 0.1s, with some time left over. But, as a hypothetical example :), that's one way you could do it with a timer.
Seagel Neville
Far East User
Join date: 2 Jan 2005
Posts: 1,476
12-12-2005 20:15
Thank you, Osgeld and Kala. :)
And Kala, I appreciate your detailed explanation. I will try those. :)
_____________________
:) Seagel Neville :)