Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Count down from 30 mins

Wolfie Waves
Registered User
Join date: 23 Jun 2005
Posts: 11
09-13-2006 19:51
Ok, I'm not too good with this sort of thing.

I'm trying to write a script that counts down 30 minutes, displaying the time remaining in mins and sec using llSetText.
Can anyone tell me why this isn't working please?

CODE

integer expires;
integer MakeUNIXTime( integer minute, integer second)
{
integer time = ( minute - 30 );

return time;
}

default
{
state_entry()
{
expires = MakeUNIXTime( 30, 0);
llSetTimerEvent( 1.0 );
}

timer()
{
integer seconds = expires - llGetUnixTime();
if( seconds > 0 )
{
integer minutes = ( seconds ) / 60;
seconds = ( seconds - ( minutes * 60 ) );

string s1 = " minutes, and ";
if ( minutes == 1 )
{
s1 = " minute, and ";
}

string s2 = " seconds left";
if ( seconds == 1 )
{
s2 = " second left";
}

llSetText( (string)minutes + s1 + (string)seconds + s2, <1.0,1.0,1.0>, 1.0 );
}

else
{
state expired;
}
}
}

state expired
{
state_entry()
{
llSay(0, "Expired");
llResetScript();
}
}


Thanks.
_____________________
Kala Bijoux
Material Squirrel
Join date: 16 Nov 2004
Posts: 112
09-13-2006 21:47
Well, let's see. You start out by calling MakeUNIXTime with 30, 0. That function calculates (minute - 30), so that will be 30 - 30, which is 0. So the function will return 0. So in state_entry(), you're effectively saying "expires = 0".

Then you start a timer. The timer fires, and you do seconds = expires - llGetUnixTime(). expires is 0, because that's what it was set to, so seconds will be 0 - something, which is a negative number. That will kick you straight into state expired.

Is that what's happening? The script starts and expires immediately?

There are several ways to fix this. First would be to fix MakeUNIXTime... I think what you want that function to do is to return the UNIX time for the minute/second value passed in. Right now, it doesn't do that, because if you pass in (30, 0), it returns 0. If you want to get the Unix time for 30 minutes later, that function needs to do something like:

CODE

integer MakeUNIXTime(integer minute, integer second)
{
integer nowInUnixTime = llGetUnixTime();
integer laterInUnixTime = nowInUnixTime + (minute * 60) + second;

return laterInUnixTime;
}


That ought to fix it. But even this is overkill. If all you want to do is count down, you don't really need the unix time, all you need to do is count down. So...

CODE

integer timeLeft;

integer convertToSeconds(integer minutes, integer seconds)
{
return ( (minutes * 60) + seconds );
}

default
{
state_entry()
{
// Calculate how many seconds there are in 30 minutes
timeLeft = convertToSeconds(30, 0);

// You could also do this, since 30 minutes equals 1800 seconds...
timeLeft = 1800;

llSetTimerEvent(1.0);
}

timer()
{
// One second has passed, so the time left is one second less than it was before
timeLeft --;

if (timeLeft == 0)
{
state expired;
}
else
{
// ... Then do all the printing stuff
}
}

// state expired, etc.
_____________________
http://materialsquirrel.blogspot.com/