How to make this condition?
#1

I've got something like this:
pawn Код:
SetTimer("OneSecondTimer", 1000, true);
pawn Код:
public OneSecondTimer(playerid)
{
  Server[Seconds]++;
  if(Server[Seconds] == 60)
  {
    Server[Minutes]++;
  }
  bla...
  bla...
  bla...
  return 1;
}
What I want to do is every 10 minutes something happens, so I tried this:
pawn Код:
public OneSecondTimer(playerid)
{
  Server[Seconds]++;
  if(Server[Seconds] == 60)
  {
    Server[Minutes]++;
  }
  bla...
  bla...
  bla...
  else if(Server[Minutes] == 0 || Server[Minutes] == 10 || Server[Minutes] == 20 || Server[Minutes] == 30 || Server[Minutes] == 40 || Server[Minutes] == 50)
  {
    // do something
  }
  return 1;
}
But by doing that, "do something" would be repeated 60 times in a minute, and I want it to be done just once every 10 minutes. How can I do it?
(I decided to use this in order to not use too many timers).

Thanks!
Reply
#2

try this:
Код:
public OneSecondTimer(playerid)
{
	Server[Seconds]++;
	if(Server[Seconds]>59)
	{
		Server[Seconds]-=60;
		Server[Minutes]++;
		if(Server[Minutes]%10==0)
		{
			// do something
		}
	}
	return 1;
}
Reply
#3

So you are saying you want a timer to repeat every 10 minutes, I'm a but confused.

pawn Код:
SetTimer("TenMinuteTimer", 6000000 /*10mins*/, true);

public TenMinuteTimer(playerid)
{
 
    Server[Minutes] = Server[Minutes] + 10;
  bla...
  bla...
  bla...
  else if(Server[Minutes] == 0 || Server[Minutes] == 10 || Server[Minutes] == 20 || Server[Minutes] == 30 || Server[Minutes] == 40 || Server[Minutes] == 50)
  {
    // do something
  }
  return 1;
}
Reply
#4

Quote:
Originally Posted by Babul
try this:
pawn Код:
public OneSecondTimer(playerid)
{
    Server[Seconds]++;
    if(Server[Seconds]>59)
    {
        Server[Seconds]-=60;
        Server[Minutes]++;
        if(Server[Minutes]%10==0) // This line
        {
            // do something
        }
    }
    return 1;
}
What is this "%" for?

Quote:
Originally Posted by [HiC
TheKiller ]
So you are saying you want a timer to repeat every 10 minutes, I'm a but confused.
Yes but, I don't want a timer, I want the OneSecondTimer to check and do something every 10 mins...
Reply
#5

the % is the "modulo" operator, the remaining amount of the division: 5/2 = 2 remaining 1 (the mod value 5%2=1).
so each 10 minutes (or minutes dividable thru 10 w/o any remaining value, gets 0: 10/10=1 remain 0, 20/10=2 remain 0, etc) - the result (dividable /10) is true.
if you set the seconds back to 0 (-=60), after reaching >59 (=60 and more hehe), the condition also is true, coz 0/10=0 remain 0.
the reason i script it this way has one simple reason:
if you start syncing the seconds with a real clock, then the seconds skip 1 sometimes. so there is a 1.67% chance of skipping one vital second for calling a minutely "payday", and you maybe would "lag" 1 whole more minute in hope to hit the 60th second next time...
if you check for >59 seconds, the 61st second will fit the condition check too, and will subtract 60 from the seconds-timer to second 1 instead of 0, AND will call your minute-loop, adding minutes, until the minutes %10 ==0.
1 more advantage: you get your 1 lagged second back for free

Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)