One global timer vs. multiple timers
#1

Hey everyone! I have one thing to ask. I'm not running any short-interval timers in my script. I've done so far this:
pawn Code:
SetTimerEx("OnTazeEnd", 5000, false, "d", playerb);
So when player gets tazed, he will be controllable again in 5 seconds. I've done the same thing with setting the player in the prison:
pawn Code:
SetTimerEx("SendToJail", 5000, false, "d", playerb);
// Under SendToJail
SetTimerEx("SendOutOfJail", 60000, false, "d", playerid);
So my question is this: Should I create one timer globally with interval of a second and check for everything there (if the time has passes to send player out of jail), or should I keep everything this way so I'll have, let's say, maybe ~10 timers running at the same time so I can check for only specific players instead of running foreach loop.
If you could spare few minutes and write down your opinion on this, I would be very grateful.
Thanks in advance. Kindly regards!
Reply
#2

I had this same doubt when started my npcs, I recommend you to use TimerEx, it will be smoother synced and better to control.
Reply
#3

Quote:
Originally Posted by dominik523
View Post
Should I create one timer globally with interval of a second and check for everything there
Don't! I remember ****** saying it's better to have more timers with less code in them than having less timers with way too heavy code (as you describe it: checking everything).

In the example you gave, those timers will be called only once - still better than repeated timers.
Reply
#4

Thank you guys for your answers. I'll stick to multiple timers instead of one.
Reply
#5

Quote:
Originally Posted by ipsLeon
View Post
I had this same doubt when started my npcs, I recommend you to use TimerEx, it will be smoother synced and better to control.
I asked myself the same question with npcs too. I wonder how 'many' timers would be too much.
it would be good to know some kind of 'safe zone' on the amount of timers untill it causes trouble.
Reply
#6

Quote:
Originally Posted by Konstantinos
View Post
Don't! I remember ****** saying it's better to have more timers with less code in them than having less timers with way too heavy code (as you describe it: checking everything).

In the example you gave, those timers will be called only once - still better than repeated timers.
Well I if we are talking about ******, i remember another thing from his timer thread. Timers with the same interval are bad.
Reply
#7

Quote:
Originally Posted by dusk
View Post
Well I if we are talking about ******, i remember another thing from his timer thread. Timers with the same interval are bad.
Because that means they are processed in the exact time, which is the same as one global timer for multiple checks.
Reply
#8

Quote:
Originally Posted by dusk
View Post
Well I if we are talking about ******, i remember another thing from his timer thread. Timers with the same interval are bad.
First, DaniceMcHarley is correct. Second, this is why he made y_timers. y_timers balances all intervals internally.

I use y_timers for ALL of my timers.
Reply
#9

Check the odds. What are the odds of having 10 people tazed at the same time? Exactly, wage your possible outcomes.
Reply
#10

If you use separate timers, their logic will be executed only when its needed. By throwing everything inside one timer, it would do unnecessary checks each second.
And more importantly - its always a nice idea to keep things modular, even if it comes with a cost of a slightly slower performance (it will be too minimal to even notice).
In this case to preserve readability, you can just throw a bigger server at the problem. Servers are cheap, developer time is not.
Reply
#11

The only issue with a global timer (that I find) is that it 'checks' for things, and the problem people have with that, is that they only want things to be done if true so they use individual timers but honestly it all depends on what your doing. I have my global timer and I do have individual timers, it's all up to you.
Reply
#12

You should use global timers if the matter isn't relatively dependable on a player or if you need to do something to a certain group of players (not a close-near repeatable timer, but needed to do something to the group in an interval). Otherwise, if a the timer focuses on a player and is repeatable, consider using per-player timers. However, there might be cases where the above doesn't apply.

The less code to execute in the interval, the better.
Reply
#13

Currently I use variables, set value to it and I use 1 global repeating timer of 1 second, and basically I lower the variable by -1 each second until it reaches 0, so would it be better to do it like this or use SetTimerEx?
Reply
#14

You might as well use OnPlayerUpdate if you want stuff to be checked for a player after an indeterminate amount of time. Say 300 updates. Because the amount of updates varies depending on what the player is doing (shooting and driving require more updates than idling) you can never be quite sure when it will be executed, only that it will at some point.

This is also a great method if you have some kind of functionality that gives XP or another collectable over time. Players that are active will receive more, players that are idling will receive less and players that have paused the game and are AFK will receive nothing at all because OnPlayerUpdate is not called while paused.
Reply
#15

Well this is basically one example how it does:

pawn Code:
if(PlayerInfo[i][Jail] > 0) PlayerInfo[i][Jail]--;
else if(PlayerInfo[i][Jail] == 0)
{
      unjail
}
So you think it'd be better if it was OnPlayerUpdate?
Performance-wise, wouldn't make any lag and so on?
Reply
#16

Quote:
Originally Posted by LocMax
View Post
Well this is basically one example how it does:

pawn Code:
if(PlayerInfo[i][Jail] > 0) PlayerInfo[i][Jail]--;
else if(PlayerInfo[i][Jail] == 0)
{
      unjail
}
So you think it'd be better if it was OnPlayerUpdate?
Performance-wise, wouldn't make any lag and so on?
This example should DEFINITLY be done in OnPlayerUpdate. However, you need to use time checking instead of constantly reducing. You need to use timestamps.
Reply
#17

Quote:
Originally Posted by LocMax
View Post
Well this is basically one example how it does:

pawn Code:
if(PlayerInfo[i][Jail] > 0) PlayerInfo[i][Jail]--;
else if(PlayerInfo[i][Jail] == 0)
{
      unjail
}
So you think it'd be better if it was OnPlayerUpdate?
Performance-wise, wouldn't make any lag and so on?
Your code won't work properly (if you were to use that in OPU) if the player goes AFK since the updates are not sent when a player is paused.Or you could consider it to be a new feature where the countdown doesn't decrease if the player goes AFK :P

You can optimize it:

pawn Code:
if(GetTickCount() > PlayerInfo[i][JailReleaseTick) { unjail }
where JailReleaseTick is when the player must be unjailed.If the player isn't jailed then set JailReleaseTick to FLOAT_INFINITY.

Though there is a function call in the above code, it hardly makes any difference.You can make a local variable and call GetTickCount just once and use the same tick for other checks.

At end of the thread, cessil uses OPU for making a SYNC System and for many anti-cheat checks.
https://sampforum.blast.hk/showthread.php?tid=220089

Quote:

wouldn't make any lag and so on

It is a myth that "adding code to OPU will slow down the server".

I use OPU for most of the simple anti-cheat checks and it does no harm!

I am guessing that using many timers will internally slow down the server because it needs to check every timer in every server tick!
Reply
#18

Quote:
Originally Posted by Yashas
View Post
Or you could consider it to be a new feature where the countdown doesn't decrease if the player goes AFK :P
Exactly. A jail system should not allow players to use AFK to pass time in jail.
Reply
#19

Quote:
Originally Posted by Crayder
View Post
Exactly. A jail system should not allow players to use AFK to pass time in jail.
That's totally right, if you wanted some players to never visit your server again. It's boring waiting in jail for a long time (3-5 minutes), I would most likely ALT-TAB and do something productive.

What's the point in validating that statement 51 times per second, when you can just validate it when it's precisely necessary to do so (1 time per second)?

OPU doesn't slow down the server; however, it uses up a lot more bandwidth.
Reply
#20

Quote:
Originally Posted by SickAttack
View Post
That's totally right, if you wanted some players to never visit your server again. It's boring waiting in jail for a long time (3-5 minutes), I would most likely ALT-TAB and do something productive.
Ok, then I'd have to say it depends on the type of server. If it's a CNR, use a timer. If it's another type of server and the jail is for an admin's jail command, use OPU. If it's for an admin command the player most likely deserves the time they received and they should spend the time crying about what they've done. It's supposed to be like real jail, you can't just take a break. It's supposed to be punishment and discipline. However, a CNR server would be different because it's fun to go to jail.
Quote:
Originally Posted by SickAttack
View Post
What's the point in validating that statement 51 times per second, when you can just validate it when it's precisely necessary to do so (1 time per second)?

OPU doesn't slow down the server; however, it uses up a lot more bandwidth.
If that's your argument against OPU, I'd still use an AFK check inside the timer. I'd also continue there jail time when they reconnect if they tried disconnecting to bypass jail.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)