Page 1 of 2

Confused (timers)

Posted: June 5th, 2010, 4:50 pm
by Rezil
I have made a simple timer which does what it's supposed to but the problem is that it's going too fast. The (relevant) code is this:

Code: Select all

make_stopwatch()
{

	self.stopwatch  = newclienthudelem(self);
	self.stopwatch .x = 630;
	self.stopwatch .y = 65;
	self.stopwatch .alignX = "right";
	self.stopwatch .alignY = "bottom";
	self.stopwatch .font = "default"; 
	self.stopwatch .fontScale = 1.3;	
	self.stopwatch .archived = false;
	self.stopwatch .color = (1, 1, 1);
	self.stopwatch  setvalue(self.time );
}
simple_stopwatch()

{

	t = getent("test5","targetname");
	
	while(1)
	
	{
	
		t waittill("trigger", user);
		if(!isdefined(user.time))
		{
			user.time = 60;
		}
		user make_stopwatch();
		for(i=60;i>0;i--)
		{
			user.time--;
			user.stopwatch setvalue(user.time);
			wait 1;
		}

	}
}
Found out about settimer but it's only accurate to a second(I want tenths of a second aswell - want to buid my own timer from scratch for practice). I thought wait 1; waits for 1 second but my timer and the one in the top left corner don't count down the same, mine is faster. :/

Any ideas how to make it work for exactly 1 second? (I might be totally wrong about wait 1; aswell)

Re: Confused (timers)

Posted: June 5th, 2010, 5:43 pm
by Drofder2004

Code: Select all

simple_stopwatch()
{
	t = getent("test5","targetname");
	
	while(1)
	{
            t waittill("trigger", user);
            if(!isdefined(user.time))
               user thread make_stopwatch(60);
	}
}

make_stopwatch(time)
{
   self.stopwatch = newclienthudelem(self);
   self.stopwatch.x = 630;
   self.stopwatch.y = 65;
   self.stopwatch.alignX = "right";
   self.stopwatch.alignY = "bottom";
   self.stopwatch.font = "default";
   self.stopwatch.fontScale = 1.3;   
   self.stopwatch.archived = false;
   self.stopwatch.color = (1, 1, 1);

   for(i=0;i<time;i+=0.1)
   {
      self.time = i;
      self.stopwatch setValue(self.time);
      wait 0.1;
   }

   self.time = undefined;
   self.stopwatch destroy();
}
try.

Re: Confused (timers)

Posted: June 5th, 2010, 5:58 pm
by Rezil
Works great but I lol'd a bit at the results:

Sometimes it's skips/approximates the time to x.9999 or x.0001, other times it stays fine for 20 or so seconds. I had a time of 32.9997 IIRC. The same problem persists however: it goes too fast. Either the ingame timer is slower or the wait command is not exactly 1 second(which I'm pretty sure it is).

Re: Confused (timers)

Posted: June 5th, 2010, 6:37 pm
by Soviet
to solve the decimal issue:

Code: Select all

temp = Int(self.time);
self.stopwatch setValue(temp);

Re: Confused (timers)

Posted: June 5th, 2010, 6:44 pm
by Rezil
Nope, it makes the number an integer, but I want it aproximated to 1/10.

Re: Confused (timers)

Posted: June 5th, 2010, 6:47 pm
by Soviet
Ah, well I know how you could handle that in Java, but I'm pretty sure Q3 scripting isn't complex enough. It would involve typecasting the int as a string then splitting it at the decimal point, keeping one digit after the decimal and merging it back together. Unfortunately Q3 scripting just doesn't have the methods required. I suppose you'll have to focus on the more fundamental issue and hopefully that will solve itself.

Re: Confused (timers)

Posted: June 5th, 2010, 6:50 pm
by Rezil
I figured as much, I can make it simulate a .1 accuracy by looping the value of a sperate HUD element from 9 to 0 and putting it next to the seconds one but it's a lame workaround and I want it to be exact and stored as a single variable.

Re: Confused (timers)

Posted: June 5th, 2010, 6:53 pm
by waywaaaard
a getSystemTimeInMilliseconds function would be great :P

Re: Confused (timers)

Posted: June 5th, 2010, 7:49 pm
by Drofder2004
CoD Script wait is EXACT, unless your game is set incorrectly...

If I do:

Code: Select all

for(i=0;i<10;i+=0.1)
{
   iprintln(i);
   wait 0.1;
}
I get perfect numbers...

The only thing you must abide to is a 1/20 time limit (0.05 seconds minimum wait time)

Re: Confused (timers)

Posted: June 5th, 2010, 7:52 pm
by Rezil
Well try using the script you posted, if you indeed get all perfect numbers then it might be something wrong with my game...

Re: Confused (timers)

Posted: June 5th, 2010, 8:45 pm
by Drofder2004
ok, you were right... simple fix.

Code: Select all

   time = time * 10;

   for(i=0;i<time;i++)
   {
      self.time = i / 10;

      self.stopwatch setValue(self.time);
      wait 0.1;
   }
USe that instead of the current for loop.

Re: Confused (timers)

Posted: June 5th, 2010, 10:05 pm
by Rezil

Code: Select all

simple_stopwatch()
{
   t = getent("test5","targetname");
   
   while(1)
   {
            t waittill("trigger", user);
            if((!isdefined(user.secondtime)) && (!isdefined(user.minutetime)))
               user thread make_secondswatch();
			   user thread make_minuteswatch();
			   user thread dots_inbetween();
   }
}

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++)
		{
		  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++)
    {
      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(;;)
	{
		self.dots.label = &":";
		wait 0.7;
		self.dots.label = &" ";
		wait 0.7;
	}
}
Image

Hooray, it works nicely. Thanks for your help Drof. :)

edit: One last thing, I've noticed that there will be an accumulation of 0.05 seconds error for every minute. Any way to fix it?(I want the seconds timer to loop from 0 to 59.9 indefinitely but I see no other way than with another for loop).

Re: Confused (timers)

Posted: June 5th, 2010, 10:17 pm
by Drofder2004

Code: Select all

for(i=minutes;i>=0;i++)
Thats an infinite loop...

If you dont want the for loop to end just do

Code: Select all

for(;;)

Re: Confused (timers)

Posted: June 5th, 2010, 10:22 pm
by Rezil
Right but a for loop requires a wait statement. Any other way to loop forever without waiting?

Re: Confused (timers)

Posted: June 5th, 2010, 11:12 pm
by Drofder2004
All loops must have a definite end or a wait statement. Otherwise, it is infinite...