Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Pausing Timers

Lylani Bellic
Evil Genius
Join date: 2 Jul 2008
Posts: 42
09-04-2008 02:09
Well okay, lots of searching and the only viable thing I found was some massive jumble of incomprehensible code so I figure it's safe to start a new thread on the issue.

I am looking for a way to pause, and resume a timer. My timer script will receive one of two linked messages, one to pause the timer, another to resume it. These could hit seconds apart and repeatably (I use a variable TRUE/FALSE to prevent the script from doing extra work by pausing an already paused timer etc)

Obviously irrelevant of what the timer is or has left I've been trying to find out how to store its current time remaining into a buffer variable, stop the timer, and then restore the timer from the buffer when the resume command comes through. From what I gathered this deals with SubStrings, a part of LSL I still cant get my head around so I've come to ask for some help.

I can provide the timer script if it's necessary for this function.

Any help understanding how to do this, and SubStrings in general, would be much appreciated.

-- Lylani Bellic
Very Keynes
LSL is a Virus
Join date: 6 May 2006
Posts: 484
09-04-2008 03:57
If you can show us the application or your test code we could tweak it for you, but basically all yo need is a global variable to use as a count down, a timer set to your clock tick interval of say 1 second, and another global variable to act as a pause start flag. I will knock up some code quickly to show you how.

CODE

//Very Keynes - 2008

integer count = 30; //in this case we want to cout for 30 seconds
integer pause = FALSE; //our pause control

default
{
state_entry()
{
llSetTimerEvent(1.0);// tick every second
llOwnerSay("Touch to pause");
}
touch_start(integer total_number)
{
pause = !pause;
if(pause)
llOwnerSay("touch to resume countdown");
}
timer()
{
if(!pause)
llOwnerSay("current count is "+(string)(--count));
if(0 == count)
{
llOwnerSay("BANG!"); //countdown compleat
llSetTimerEvent(0.0); //stop timer
}
}
}
Lylani Bellic
Evil Genius
Join date: 2 Jul 2008
Posts: 42
09-04-2008 04:25
CODE

// Global Variables
// Integers
integer channel;
integer TimerinUse = FALSE;
integer CurrentValue;
integer Time;
integer Running = TRUE;
// Strings
string smin;
string sseconds;
// Floats
float Timer = 0.0;
float TimerBuffer;
// Keys
key Key_Holder = NULL_KEY;
key Lock_Holder = NULL_KEY;
// Lists
list KeyPad = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
list Observation = ["-", "-", "-"];

string uuFormated(integer value)
{
return llGetSubString("0" + (string)(value / 100), -2, -1) + ":"
+ llGetSubString("0" + (string)(value % 100), -2, -1) + ":00";
}

integer uuConverted(float value)
{
integer hours = (integer)(value / 3600.0);
return hours * 100 + (integer)(value - ((float)hours * 3600.0)) / 60;
}

default
{
link_message (integer sender_num, integer num, string message, key id)
{
if ((num == 393) && (message == "NONCOMPLIANCE") && (Running)) {
//
Running = FALSE;
} else if ((num == 393) && (message == "COMPLIED") && (!Running)) {
//
Running = TRUE;
}
}
state_entry()
{
channel = ( -16 * (integer)("0x"+llGetSubString((string)llGetKey(),-732,-24)) );
llListen(channel, "", "", "");
}
listen (integer channel, string name, key id, string message)
{
Key_Holder = id;
Lock_Holder = id;

if (message == "Timelock") {
llDialog(Key_Holder, "\t----Timelock----\nTimer Duration: " + uuFormated(CurrentValue),["Start", "0", "Clear", "1", "2", "3", "4", "5", "6", "7", "8", "9"],channel);
} else if (message == "Clear") {
CurrentValue = 0;
llSetTimerEvent(0.0);
llDialog(Key_Holder, "\t----Timelock----\nTimer Duration: " + uuFormated(CurrentValue),["Start", "0", "Clear", "1", "2", "3", "4", "5", "6", "7", "8", "9"],channel);
} else if (message == "Start") {
Timer = (float)(CurrentValue / 100) * 3600.0 + (float)(CurrentValue % 100) * 60.0;
llSetTimerEvent(Timer);
llMessageLinked(LINK_SET, 0, "TimerActivated", "");
TimerinUse = TRUE;
integer seconds = (integer)Timer;
integer min = llFloor(seconds/60);
seconds = seconds - (min*60);
integer hour = llFloor(min/60);
min = min - (hour*60);
if(hour > 12) {hour = hour - 12;}
string shour = (string)hour;
if(min < 10) {smin = "0"+(string)min;}
else { smin = (string)min;}
if(seconds < 10) { sseconds = "0"+(string)seconds;}
else {sseconds = (string)seconds;}
string Time = shour + " Hours : " + smin + " Minutes : " + sseconds + " Seconds";
llMessageLinked(LINK_SET, 351, Time, "");
llWhisper(0, "Timelock set for " + Time);
} else {
integer digit = llListFindList(KeyPad, [message]);
if (digit != -1) {
integer new_value = (CurrentValue * 10) + digit;
if ((new_value % 100) < 60) {
if ((new_value / 100) < 24) {
CurrentValue = new_value;
}
}
llDialog(Key_Holder, "\t----Timelock----\nTimer Duration: " + uuFormated(CurrentValue),["Start", "0", "Clear", "1", "2", "3", "4", "5", "6", "7", "8", "9"],channel);
}
}
}
timer ()
{
llSetTimerEvent(0);
llMessageLinked(LINK_SET, 0, "TimerFinished", "");
TimerinUse = FALSE;
}
}


It's a nice script, thanks largely in part to someone here on the forums. It works great, I love it; but my knowledge of it is limited. The idea of using intervals came to mind but wasn't sure how exactly to implement it. Either way, this is my timer script. Hope it helps.
Lylani Bellic
Evil Genius
Join date: 2 Jul 2008
Posts: 42
09-04-2008 14:38
I think I got it figured. I changed the llSetTimerEvent to a (TickGap, 1.0s) and then used the already defined float Timer in the timer() event ticking down by 1, Timer = Timer -1, and when Timer == 0 it goes off. Simply using llSetTimerEvent(0); will pause the timer, forcing the ticks to not count down.

Effective pause of timers :)

Thanks a bunch Very Keynes for showing it in such a easy to understand fashion.

Now of course if I could only get it to display in the Hours/Minutes/Seconds format but I'm happy with just pausing a invisible timer or one made of displayed total seconds.