Page 1 of 1

Timer and stopwatch

Posted: September 29th, 2010, 2:24 pm
by BatterY
I would like to create a stopwatch on side of the screen when trigger A is triggered and delete it when you hit trigger B or time is up (60 secs). Another script would be needed on measuring the time. When you hit trigger A, it starts measuring time, when you hit B, it stops the time and prints it, or while measuring, it shows it.

A picture is worth a thousand words:
trigger.png
I don't have any experience in creating hud elements and that's why I need help.

Re: Timer and stopwatch

Posted: September 29th, 2010, 2:53 pm
by Rezil

Code: Select all

main()
{
	precachestring(&":");
	precachestring(&" ");

	thread start();
	thread simple_stopwatch();
	thread stopwatch_end();
	
	level.time_interrupted = false;


}

start()
{	
	for(;;)
	{
		p = getentarray("player", "classname");
		for(i=0;i<p.size;i++) 
		{
			if ((!isdefined(p[i].reset)) && (isalive(p[i])))
			{
				p[i].reset = true;
				p[i].triggered_timer = false;
				p[i] thread watchUserTime();
			}
		}
		wait 0.25;
	}
}

simple_stopwatch()
{
	t = getent("START_TRIGGER","targetname"); //Change!

	while(1)
	{
		t waittill("trigger", user);
		level.time_interrupted = false;
		if(!user.triggered_timer)
		{
			level.time_interrupted = false;
			user.triggered_timer = true;
			if((!isdefined(user.secondwatch)) && (!isdefined(user.minutewatch)))
			{
				user thread make_secondswatch();
				user thread make_minuteswatch();
				user thread dots_inbetween();
			}
		}
		else user iprintlnbold("You already triggered the timer!"); wait 2;
	}
}

make_secondswatch()
{
	self.secondwatch = newclienthudelem(self);
	self.secondwatch.x = 322; //max 650
	self.secondwatch.y = 450; //max 475
	self.secondwatch.alignX = "left";
	self.secondwatch.alignY = "bottom";
	self.secondwatch.font = "default";
	self.secondwatch.fontScale = 2;   
	self.secondwatch.archived = false;
	self.secondwatch.color = (1, 1, 1);
	for(;;)
	{
		secondtime = 0;
		secondtime = secondtime * 10;
		seconds = 0;
		for(i=seconds;i<=599;i++)
		{
			if(level.time_interrupted) 
			{
				seconds = 0;
				break;
			}
			self.secondtime = i / 10;
			self.secondwatch setValue(self.secondtime);
			wait 0.1;
		}
		wait 0.05;
	}
}

make_minuteswatch()
{
	self.minutewatch = newclienthudelem(self);
	self.minutewatch.x = 311; //max 650
	self.minutewatch.y = 450; //max 475
	self.minutewatch.alignX = "right";
	self.minutewatch.alignY = "bottom";
	self.minutewatch.font = "default";
	self.minutewatch.fontScale = 2;   
	self.minutewatch.archived = false;
	self.minutewatch.color = (1, 1, 1);
	self.minutetime = 0;
	minutes = 0; //And no I don't know why I used this instead of just putting i=0;
	for(i=minutes;i>=0;i++)
	{
		if(level.time_interrupted) 
		{
			minutes = 0;
			break;
		}
		self.minutewatch setValue(self.minutetime);
		if(self.secondtime == 599/10) self.minutetime++;
		wait 0.1;
	}
}

dots_inbetween()
{
	self.dots = newclienthudelem(self);
	self.dots.x = 318; //max 650
	self.dots.y = 450; //max 475
	self.dots.alignX = "right";
	self.dots.alignY = "bottom";
	self.dots.font = "default";
	self.dots.fontScale = 2;   
	self.dots.archived = false;
	self.dots.color = (1, 1, 1);
	for(;;)
	{
		if(level.time_interrupted) break;
		self.dots.label = &":";
		wait 0.7;
		self.dots.label = &" ";
		wait 0.7;
	}
}

watchUserTime()
{
	for(;;)
	{
		if(self.triggered_timer) //if user has triggered timer he must have secondtime and minutetime values
		{
			if((self.minutetime==1) && (!level.time_interrupted)) //60 seconds are up!
			{
				self iprintlnbold("You weren't fast enough!");
				level.time_interrupted = true;
				self.triggered_timer = false;
				self.secondtime = 0;
				self.minutetime = 0;
				self.minutewatch destroy();
				self.secondwatch destroy();
				self.dots destroy();
				wait 0.05;
			}
		}
		wait 0.05;
	}
}

stopwatch_end()
{
	t = getent("END_TRIGGER","targetname"); //Change!

	while(1)
	{
		t waittill("trigger", user);
		if(user.triggered_timer)
		{
			user.triggered_timer = false;
			level.time_interrupted = true;
			user iprintlnbold("You finished in " +user.secondtime+ " seconds and " +user.minutetime, " minutes."); //minutes added if you want more minutes other than 1(see watchUserTime())
			user.secondtime = 0;
			user.minutetime = 0;
			user.minutewatch destroy();
			user.secondwatch destroy();
			user.dots destroy();
			wait 0.05;
		}
		else user iprintlnbold("You didn't trigger the timer so it can't end here!"); wait 2;
	}
}

Re: Timer and stopwatch

Posted: September 29th, 2010, 4:11 pm
by Rezil
See post above.

Re: Timer and stopwatch

Posted: September 29th, 2010, 4:35 pm
by Pedsdude
Rezil @ win.

Re: Timer and stopwatch

Posted: September 29th, 2010, 6:12 pm
by BatterY
128909531505186973.jpg

Re: Timer and stopwatch

Posted: September 29th, 2010, 7:34 pm
by Drofder2004

Code: Select all

    level.watch = newHudElem();
    level.watch.x = 0;
    level.watch.y = 0;
    level.watch.alignx = "center";
    level.watch.aligny = "middle";
    level.watch.horzAlign = "center";
    level.watch.vertAlign = "middle";
    level.watch setClock( 60, 60, "hudStopwatch", 64, 64 );
Easy clock.

Re: Timer and stopwatch

Posted: September 30th, 2010, 12:34 am
by MORGOTH
why can't you add a thing like this in cjmod for cod4 that makes the timer start by pressing a button and end by pressing it another time... would be cool for custom jumping maps ^^

Re: Timer and stopwatch

Posted: September 30th, 2010, 1:44 pm
by Hoogie
MORGOTH wrote:why can't you add a thing like this in cjmod for cod4 that makes the timer start by pressing a button and end by pressing it another time... would be cool for custom jumping maps ^^
Good idea!
Would be even more awesome to save records on the server but i guess you need a whole database to do that :P

But the timer to use personally would be nice :D

Re: Timer and stopwatch

Posted: September 30th, 2010, 2:59 pm
by Rezil
The script works. Just save it into your map .gsc, make a start trigger and an end trigger, rename the parts in the script accordingly and be sure to give credit('with help from codjumper' should do fine. :P).

Re: Timer and stopwatch

Posted: September 30th, 2010, 3:04 pm
by BatterY
Rezil wrote:The script works. Just save it into your map .gsc, make a start trigger and an end trigger, rename the parts in the script accordingly and be sure to give credit('with help from codjumper' should do fine. :P).
Will do.