Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

NEED: Formatted Timer from Countdown - L$300

ElectronicSuper Sonic
Registered User
Join date: 13 Jun 2006
Posts: 8
06-13-2006 05:36
Hey all. I'm new to SL, and loving this LSL stuff to death. I spent a couple hours last night working up a complicated little multifunction timer, it parses commands in seconds, minutes, and hours, but as a long integer. I even have it rigged to an XYtext display so I can get a peek at long I have to wait (or run).

But... I run into a stumper that I decided I'd just do later, and this morning I look at it, not having time before heading off to work, to convert '999999' into 'hhmmss' like a proper counter.

So.... here's what I need:
Take an integer called 'countdown', which can be up to 999999 long (i don't know why, someone might want a really long timer), which is counted off in seconds. I need it formatted into "hour + min + sec". so I can feed it to the XYdisplay.

If I tried to work it out this early, or when I get home, my head would asplode. I'm guessing that someone else already has this figured out way ahead of me. :D

Thanks ahead of time,
ESS.
Ordinal Malaprop
really very ordinary
Join date: 9 Sep 2005
Posts: 4,607
06-13-2006 05:51
Off the top of my head... use the format_time() function in this script to get a formatted time string from an integer number of seconds.

CODE
string zero_pad(integer number)
{
if (number < 10) return "0" + (string)number;
else return (string)number;
}

string format_time(integer seconds)
{
integer hours = llFloor(seconds / 3600);
seconds -= hours * 3600;
integer minutes = llFloor(seconds / 60);
seconds -= minutes * 60;
return (string)hours + ":" + zero_pad(minutes) + ":" + zero_pad(seconds);
}

integer gCountdown = 999999;

default
{
state_entry()
{
llSetTimerEvent(1.0);
}

timer()
{
llSetText(format_time(--gCountdown), <1.0, 1.0, 1.0>, 1.0);
if (gCountdown == 0) {
llShout(0, "BOOOOOM!!!");
llDie();
}
}
}

That should work.
Sheila Plunkett
On The Prowl!
Join date: 25 Dec 2005
Posts: 67
06-13-2006 05:59
CODE


integer starth=5; //Put in the number of countdown hours
integer startm=10; // Countdown minutes
integer starts=0;// countdown seconds

integer total;

default
{
touch_start(integer num_detected)
{
if (llDetectedKey(0) == llGetOwner())
{
total = (starth * 60 * 60) + (startm * 60) + starts;
llSetTimerEvent(1.0);
}
}
timer()
{
total--;
if (total < 0)
{
total = 0;
llSetTimerEvent(0.0);
}
integer hours = total / 3600;
integer minutes = (total % 3600) / 60;
integer seconds = total % 60; }
string shours = (string)hours;
string sminutes = (string)minutes;
string sseconds = (string)seconds;}
if(llStringLength(shours) == 1)
{
sseconds = "0" + shours;
}
if(llStringLength(sminutes) == 1)
{
sminutes = "0" + sminutes;
}
if(llStringLength(sseconds) == 1)
{
sseconds = "0" + sseconds;
}
llSay(0,shours + sminutes + sseconds);
}
}


Am at work, so cannot check against compile errors.. but it should do the trick.

Just try it ^.^

*meow*
Sheila!
ed44 Gupte
Explorer (Retired)
Join date: 7 Oct 2005
Posts: 638
06-13-2006 06:44
I could not help myself!

CODE




string zero_pad(integer number)
{
if (number < 10) return "0" + (string)number;
else return (string)number;
}

string format_time(integer seconds)
{
integer secs = seconds % 60;
seconds /= 60;
integer mins = seconds % 60;
integer hrs = seconds / 60;
return (string)hrs + ":" + zero_pad(mins) + ":" + zero_pad(secs);
}

integer gCountdown = 999999;

default
{
state_entry()
{
llListen( 0, "", NULL_KEY, "" );
llSetTimerEvent(1.0);
}

timer()
{
llSetText(format_time(--gCountdown), <1.0, 1.0, 1.0>, 1.0);
if (gCountdown == 0) {
llShout(0, "BOOOOOM!!!");
llDie();
}
}

listen( integer channel, string name, key id, string message )
{
llOwnerSay ("1 " + (string) gCountdown);
integer i = (integer) message;
if (i > 0)
gCountdown = i;

llOwnerSay ("2 " + (string) gCountdown);
}

}
ElectronicSuper Sonic
Registered User
Join date: 13 Jun 2006
Posts: 8
06-13-2006 07:07
O.O OMG. Ya'll even padded it *faints*

I'll take a better look at them later. And... here's the code I have (minus some recent edits) before I add the formatting. ... Of COURSE it's for counter-strike scenarios. :D

CODE
list run_message = ["Run.","Start running now.","This is the part where you run.","Get Moving!","Haul Ass!","You probably don't want to be around for this.","That's how you have to  live if you're still in this region.","Stick around if you like.","Time to start making demands.","Note: This will be a rather large explosion."];
integer armed=FALSE;
integer countdown;

rez_bomb()
{
//llWhisper(0,"Insert Boom Here"); // used for timer testing
llSetTimerEvent(0); //stop timer
llMessageLinked(LINK_SET, 1122,"KABOOM", "");
//Rez Thing That Makes Boom Here
}

default {
state_entry()
{
llListen(2666,"","",""); //"boom" - open listen for NEST team.
llMessageLinked(LINK_SET, 1122,"STNDBY", ""); //set display text
}

on_rez(integer start)
{llResetScript();}

timer()
{if(armed)
{
countdown--;
llSetObjectDesc("Armed: " +(string)countdown + " seconds."); //Idunno why I bothered...
llMessageLinked(LINK_SET, 1121,(string)countdown, "");//seperate channel for display-side integer padding. to be changed when formatting added.
if(countdown<60)
{llPlaySound("2370e863-c240-8e76-f23d-fe1f185902a2", 0.3);}
if(countdown<=0){rez_bomb();} // FINAL PROCESS
}else{llSetTimerEvent(0);llSetObjectDesc("Drop. Set. Run Like Hell.");
llMessageLinked(LINK_SET, 1122,"DISARM", "");
}
}

touch_start(integer total_number)//Gives owner dialog commands.
{
key av = llDetectedKey(0);
if(av == llGetOwner()) {
llDialog(llGetOwner(),"xxxxxxx Briefcase Nuke xxxxxxx\nPress 'Arm Nuke' to select detonation options. !DISARM! to stop the countdown, and 'Help' for a notecard. Note: I'm not responsible for your dumb ass. Use at you're own risk, etc, etc. Have a nice day",["Arm Nuke>>","!DISARM!", "Help"], 2666);
} else {llSay(0,"Unauthorized Access Attempt ::" +llKey2Name(av));}
}


listen(integer channel, string name, key id, string message)
{
message = llToLower(message);//it's just easier
list command = llParseString2List(message, ["|"], []); // parse timer commands
if (llGetOwnerKey(id) != llGetOwner()) {} else {
// Primary Functions & Dialog Options
llPlaySound("2370e863-c240-8e76-f23d-fe1f185902a2", 0.3);
if (message == "arm nuke>>")
{
llDialog(id,"When Ready To Detinate, select a preset time from menu. Remember to run like hell.",["10|sec","30|sec","60|sec","5|mins","10|mins","30|mins","90|mins","1|Hour","2|hour","5|hour","10|hour","12|hour"], 2666);
return;
}
if (message == "!disarm!" || message == "disarm" || message == "defuse")
{
llWhisper(0,"/me Disarmed");
armed=FALSE;
return;
}
if (message == "help")
{
llGiveInventory(llGetOwner(),"xxxxx Breifcase Nuke xxxxx");
return;
}

if(llList2String(command, 1) == "sec")
{
countdown= llList2Integer(command, 0);
llSetTimerEvent(1);
integer rand = llRound(llFrand(llGetListLength(run_message)));
llWhisper(0,"/me will detinate in " + (string)countdown + " seconds. " + llList2String(run_message, rand));
armed=TRUE;
}
if(llList2String(command, 1) == "mins")
{
countdown= 60*llList2Integer(command, 0);
llSetTimerEvent(1);
integer rand = llRound(llFrand(llGetListLength(run_message)));
llWhisper(0,"/me will detinate in " + (string)countdown + " minutes. " + llList2String(run_message, rand));
armed=TRUE;
}
if(llList2String(command, 1) == "hour")
{
countdown= (60*60)*llList2Integer(command, 0);
llSetTimerEvent(1);
integer rand = llRound(llFrand(llGetListLength(run_message)));
llWhisper(0,"/me will detinate in " + (string)countdown + " hours. " + llList2String(run_message, rand));
armed=TRUE;
}
}}
}
Sheila Plunkett
On The Prowl!
Join date: 25 Dec 2005
Posts: 67
06-13-2006 07:21
Well, wasn't that part of the description? ^.^

*mew*
Sheila!
ElectronicSuper Sonic
Registered User
Join date: 13 Jun 2006
Posts: 8
06-13-2006 07:28
I suppose it was. :D I posted the code I'm using now. :D
Sheila Plunkett
On The Prowl!
Join date: 25 Dec 2005
Posts: 67
06-13-2006 07:49
Well, then all you really needed is the
integer hours = total / 3600;
integer minutes = (total % 3600) / 60;
integer seconds = total % 60;
bit.. and the padding, which is easy, tho.

*mew*
Sheila!
ElectronicSuper Sonic
Registered User
Join date: 13 Jun 2006
Posts: 8
06-13-2006 08:37
I'll have to play with it an find out. Man, I wish I could run SL from work. :(