Jump to content

Show time left on a running timer?


AutumnSounds
 Share

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

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

Recommended Posts

Hi all.

I'm really stuck here, hope you lovely people can help! ❤️

After initially being touched to turn it green, I'd like to be able to not only PREVENT the object from running the timer again when touched but also show how minutes are remaining via llSay or llDialog.

Any hints and advice would be greatly appreciated.
Thanks in advance!

default
{
    touch_start(integer detected)
    {
            llSetTimerEvent(300); // 5 mins (300 seconds)
            llSetLinkColor(LINK_SET, <0.180, 0.800, 0.251>, ALL_SIDES); // green
    }
    timer()
    {
            llSetTimerEvent(0);
            llSetLinkColor(LINK_SET, <0.694, 0.051, 0.788>, ALL_SIDES); // purple
    }
}
Link to comment
Share on other sites

I'm not totally clear about how you're hoping to combine those two wishes, so this may or may not be helpful.  First, you have several options for preventing a second touch from re-triggering the timer.  The easiest, probably, is to just add a global boolean flag that flips to TRUE when you touch the object and then back to FALSE when the timer finishes. The second wish is a bit trickier.  To accomplish it, you'll need to run two timers -- one to make your color change and another one to keep track of lapsed time.  My preferred method for doing it is to multiplex a single timer rather than running a totally separate one, but you can do whatever you want.  Here's a solution that multiplexes and satisfies your first wish by protecting the color change timer:

integer giTouched;
integer giLapsed;

default
{
    touch_start(integer detected)
    {
        if (!giTouched)
        {
            giTouched = TRUE;
            giLapsed = 0;
            llSetTimerEvent(1.0); //Fast timer for multiplexing
            llSetLinkColor(LINK_SET, <0.180, 0.800, 0.251>, ALL_SIDES); // green
        }
        else
        {
            llRegionSayTo(llDetectedKey(0),0,"Lapsed time = " + (string)giLapsed);
        }
    }
    
    timer()
    {
        ++giLapsed;     //Update tpdate the lapsed time with every clock tick
        if (giLapsed == 60)     // One minute after the initial touch
        {
            giTouched = FALSE;  // Frees the touch_start event for a new color change request
            llSetTimerEvent(0.0);
            llSetLinkColor(LINK_SET, <0.694, 0.051, 0.788>, ALL_SIDES); // purple
        }
    }
}

The total time for the color change is set by the value you give in the if toggle that makes the color change and shuts off the timer.  I set it to 60 seconds in this example, but you'd set yours to 300.  Note that this approach follows the lapsed time in integral clock ticks.  If you wanted a floating clock, you really would have to use a separate timer instead of multiplexing.  In that case, I'd probably use llGetTime() and keep a running tally each time you triggered touch_start.

  • Like 1
Link to comment
Share on other sites

  • 3 weeks later...

Another method if you want a slightly simpler solution is to use the get and reset time functions.  They count the seconds since the script was started or since the last time the timer was reset.  It's a useful set of commands. 

I like the timer multiplex solution listed above and use it and variants of it frequently but for some thing it's jut a bit much when when one is looking for small and simple.  So to adapt your script in another way would be like this:

integer touchActive = FALSE;

default
{
    touch_start(integer detected)
    {
		if (!touchActive)
		{
			llWhisper(0,"Timer started.");
			touchActive = TRUE;
			llSetTimerEvent(300);  //5min
			llSetLinkColor(LINK_SET, <0.180, 0.800, 0.251>, ALL_SIDES); // green
			llResetTime();
		} else {
			integer elapsedTime = (integer)llGetTime();
			llWhisper(0, "Timer active, " + (string)(300 - elapsedTime) + " seconds remaining.");
		}
    }
    timer()
    {
            llWhisper(0, "Timer ended.");
            llSetTimerEvent(0);
			touchActive = FALSE;
            llSetLinkColor(LINK_SET, <0.694, 0.051, 0.788>, ALL_SIDES); // purple
    }
}

 You could of course put in some code to manipulate the out put so it shows minutes + seconds remaining, or any number of things.  But the llResetTime(), llGetTime(), and llGetAndResetTime() commands are handing  for getting quick elapsed counts that don't require a whole lot of overhead for smaller and simpler scripts where a multiplex approach is not really justified.

 

 

Edited by Anna Salyx
Link to comment
Share on other sites

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