February 24, 2018, 04:02:48 am

Author Topic: Dispelling the "TIMERS ARE BAD" myth  (Read 1454 times)

0 Members and 1 Guest are viewing this topic.

stormeus

  • Vice Underdog
  • Crazy Man
  • *
  • *
  • Posts: 1750
  • Country: us
  • VC:MP Developer
    • View Profile
    • GTA VICE CITY Respective owner
    • Awards
Dispelling the "TIMERS ARE BAD" myth
« on: July 05, 2013, 08:25:34 pm »
Over the past few weeks, I have had a large number of people asking me for help with scripts and one question that came up in particular was timers. For that, I'm making this thread for the sole purpose of linking people here when they ask if using timers is bad.



  • "How often should I be running timers?"
  • "Are timers bad?"
  • "I need to run this code but I don't want to use timers because I heard they are bad to use."

Timers are not bad unless they are used badly.
(And to know that, there are several things that you should know about scripted timers.)



What are timers?
Timers are functions in a script that are run at predetermined intervals for a certain number of repetitions (or ad infinitum). Timers can be used within scripts to perform actions at a later time, when they cannot be controlled by other events in the scripts.

For example, you may need to use a timer for a heal command if you want players to wait three seconds to heal. There are no events like onPlayerThreeSecondsHavePassed that you can use to perform such a task, so you would need to use a timer to control when players can heal.

However, you should not use timers for performing actions such as saving statistics in a database or file when that can be done in events such as onPlayerDeath, onPlayerKill, or onPlayerTeamKill.

But please do not ever do anything like that in onPlayerMove. EVER.

How do timers work?
The VC:MP server (both Pawn and Squirrel) rely on an internal loop when they run to process packets and other information, which is relayed to connected clients. In order to prevent draining the CPU, the servers wait 16ms after processing all the packets in its queue, before processing any queued packets again, repeating this process continuously until the server closes.

Timers are simply an extension of existing script functions that takes advantage of that 16ms period of sleep. An internal list of timers created by the scripts is maintained, and every time the server processes information, it also adds the amount of time it waited (16ms) to a counter maintained for each individual timer. Once that counter reaches the time interval (e.g. NewTimer( "function", 1000, 1 ) -- 1000 is the interval), the function is called, and the timer is either called infinitely or until the timer has run as many times as it asked to.

How many timers should I be using?
You can use as many timers as you want as long as you're not performing a ridiculous amount of instructions in them. For example, you can have a timer for a countdown running every 1000ms, and another timer for random messages every 15 seconds. Combining them and doing something like this:

Code: [Select]
RandomMessageTime <- 0;
Countdowns <- 0;
function Countdown()
{
    Countdowns = 3;
}

function MyTimer()
{
    if( ++randomMessageTime == 15 )
    {
        Message( "This timer sucks balls." );
        randomMessageTime = 0;
    }

    if( Countdowns > 0 )
    {
        Message( Countdowns-- );
    }
}

NewTimer( "MyTimer", 1000, 0 );

Does nothing but make your code messier and emulate the very behavior that the server already does to make your life more convenient.

I combined a few of my timers, so I have one timer running every second to save stats, keep track of a last man standing script, and print messages. Is this okay?
NO. Now you have so much action going on in one timer that is running so frequently (every second), performance will be even worse than if you had split up these timers.

But my friends recommended that trick to me!
Your friends are complete dumbasses.

What should I avoid in timers then?
Try to avoid saving to databases in timers that are running for very frequent amounts of time (less than 60 seconds). Also avoid writing to files, including INIs or saving hash tables, for the same reason.

Don't use timers to spawn other timers, especially when using intervals that are too low. This could cause an avalanche of timers that could lag your server to the point of being unusable, if it does not crash.

So why do people say timers are bad?
Originally, to keep noobs from using timers to do all of the bad things I told you about above. Now, because people actually believe that timers are bad.

I don't understand this thread.
You should not be using timers at all, then.



This rant has been paid for by the "Stop Scripting Stupidly" Foundation.
« Last Edit: July 05, 2013, 08:35:54 pm by stormeus »
Agree Agree x 3 Funny Funny x 1 Winner Winner x 1 Informative Informative x 1 (list)
Agree Disagree Funny Winner Pwnt Informative Friendly Useful Optimistic Artistic Late Brain Donor

<krystianoo> stormeus do good job
<krystianoo> with recent update
<krystianoo> if not agree; jeb yourself in head
<Avenger> yesterday you said death to stormeus
<karan> double standard krystianoo
<karan> he called him fake prophet too
<krystianoo> sure fake prophet
<krystianoo> but with recent updates real

Ryne

  • Vice Underdog
  • Crazy Man
  • *
  • *
  • *
  • Posts: 1762
  • Country: ca
  • Winning feels better when you take a little damage
    • View Profile
    • VUmania
    • Awards
Re: Dispelling the "TIMERS ARE BAD" myth
« Reply #1 on: July 05, 2013, 08:27:15 pm »
nick work please someone stick it
Agree Agree x 1 (list)
Agree Disagree Funny Winner Pwnt Informative Friendly Useful Optimistic Artistic Late Brain Donor

Currently: EC Manager, KTB Moderator, LG Admin
Clan History: KTM, CG, ULK, VU, MK, DZ and DnA
Past Experience: EA manager/Admin, Pure DM Manager/Admin, Sky-city Admin, XE, KTB Moderator and Elite Paradise Admin
Events: Developed EYE event on EA(EYE 1 and 3 Admin), XAD league referee, Vice War 3,4,6 and VCDC 3,5 Admin

Thijn

  • Forum Administrator
  • Crazy Man
  • *
  • *
  • *
  • Posts: 2801
  • Country: nl
    • View Profile
    • Awards
Re: Dispelling the "TIMERS ARE BAD" myth
« Reply #2 on: July 05, 2013, 08:27:21 pm »
Pinned. This is awesome.



This rant has been paid for by the "Stop Scripting Stupidly" Foundation.
rofl
« Last Edit: July 05, 2013, 08:30:15 pm by Thijn »
Agree Agree x 1 Funny Funny x 1 Useful Useful x 1 (list)
Agree Disagree Funny Winner Pwnt Informative Friendly Useful Optimistic Artistic Late Brain Donor

I'm not totally useless,
I can be used as an bad example ;)

"Never do Today, What you can do Tomorrow"



Charley

  • Vice Underdog
  • Crazy Man
  • *
  • *
  • *
  • Posts: 4065
  • Country: 00
    • View Profile
    • Awards
Re: Dispelling the "TIMERS ARE BAD" myth
« Reply #3 on: July 05, 2013, 11:24:09 pm »
Yep, nice thread.

Also, one little tip for people who are using timers to save player data:

When I first started scripting I noticed that many people were saving an entire server's playerdata every 30 seconds or minute, so as not to lose stats in the event of a crash. Saving playerdata on instances outside of leaving/joining is necessary, but doing it this way can seriously lag down a server.

In XE I scripted it so that every 3 seconds, one player's data is saved. There is a counter that goes through each of the slots in the server, checks to see whether a player is present, if present then it saves and waits 3 seconds for the next one, and if not present then moves on to the next one immediately.

Saving 1 player at a time is far less laggy than loading them all in on one go. There may be an even better way to do it, in fact there almost certainly is, but this is what I came up with a couple of years ago and it seems to have worked quite well.
Agree Agree x 2 (list)
Agree Disagree Funny Winner Pwnt Informative Friendly Useful Optimistic Artistic Late Brain Donor

Writer of excessively long posts


X_94

  • Regular
  • ***
  • Posts: 74
  • Country: mx
    • View Profile
    • Awards
Re: Dispelling the "TIMERS ARE BAD" myth
« Reply #4 on: July 11, 2013, 01:11:19 am »
Saving 1 player at a time is far less laggy than loading them all in on one go. There may be an even better way to do it, in fact there almost certainly is, but this is what I came up with a couple of years ago and it seems to have worked quite well.

saving data for a player each 3 seconds doesn't sound that lag free, i prefer to save all players data each 5 minutes by using TRANSACTION statements: http://www.sqlite.org/lang_transaction.html
« Last Edit: July 11, 2013, 01:15:03 am by X_94 »
Agree Agree x 1 (list)
Agree Disagree Funny Winner Pwnt Informative Friendly Useful Optimistic Artistic Late Brain Donor




<X_94> messi suckz!!
<~Baldachyn> yes x94 finally some sane words from you