SA-MP Forums Archive
Timer Efficency - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Server (https://sampforum.blast.hk/forumdisplay.php?fid=6)
+--- Forum: Server Support (https://sampforum.blast.hk/forumdisplay.php?fid=19)
+--- Thread: Timer Efficency (/showthread.php?tid=171707)



Timer Efficency - BP13 - 27.08.2010

Looking for a efficient way for timers. I am using this and is it more efficient or less than normal timers?

pawn Код:
public OnGameModeInit()
{
    MainTimer = SetTimer("MasterTimer", 1000, 1);
}

public MasterTimer()
{
    Second ++;
    Second2 ++;
    Second3 ++;

    if(Second == 1) // 1 Second
    {
        for(new i = 0; i < MAX_PLAYERS; i++)
        {
            if(IsPlayerConnected(i))
            {
                // Gate3 (Cage) U
                if(IsPlayerInRangeOfPoint(i, 5.0, -423.6993, 2201.7231, 40.9262) && OpenGateCage[i] == false) // Close --> Open
                {
                    OpenGateCage[i] = true;
                    MoveDynamicObject(Gate3, -423.692413, 2201.705811, 50.923096, 10.0);
                    MoveDynamicObject(Gate4, -423.686127, 2206.743164, 45.926369, 10.0);
                    MoveDynamicObject(Gate5, -418.655365, 2201.730713, 45.926369, 10.0);
                    MoveDynamicObject(Gate6, -423.685608, 2196.700928, 45.913692, 10.0);
                    MoveDynamicObject(Gate7, -428.733124, 2201.718506, 45.926338, 10.0);
                }
                else if(!IsPlayerInRangeOfPoint(i, 5.0, -423.699341, 2201.723145, 40.926292) && OpenGateCage[i] == true) // Open --> Close
                {
                    OpenGateCage[i] = false;
                    MoveDynamicObject(Gate3, -423.692413, 2201.705811, 59.923141, 1.5);
                    MoveDynamicObject(Gate4, -423.686127, 2215.716309, 45.926369, 1.5);
                    MoveDynamicObject(Gate5, -409.680603, 2201.730713, 45.926369, 1.5);
                    MoveDynamicObject(Gate6, -423.685608, 2187.702881, 45.913692, 1.5);
                    MoveDynamicObject(Gate7, -437.683075, 2201.718506, 45.926338, 1.5);
                }
            }
        }
        Second = 0;
    }
    if(Second2 == 180)
    {
        SendClientMessageToAll(COLOR_ORANGE, RandMessages[random(sizeof(RandMessages))]);
        Second2 = 0;
    }
    if(Second3 == 138)
    {
        DestroyDynamicObject(PirateObject2);                                                                          
        PirateObject1 = CreateDynamicObject(8493, 3751.8791503906, 1508.7846679688, 14.907380104065, 0.000000, 0.000000, 180,1,0,-1,200.0); //
        MoveDynamicObject(PirateObject1, 3752.7321777344, 1203.7044677734, 14.907380104065, 2.00 );
    }
    if(Second3 == 276)
    {
        DestroyDynamicObject(PirateObject1);
        PirateObject2 = CreateDynamicObject(8493, 3752.7321777344, 1203.7044677734, 14.907380104065, 0.000000, 0.000000, 0.000000,1,0,-1,200.0); //
        MoveDynamicObject(PirateObject2, 3751.8791503906, 1508.7846679688, 14.907380104065, 2.00);
        Second3 = 0;
    }
    return 1;
}



Re: Timer Efficency - RoBo - 08.09.2010

pawn Код:
new tick[MAX_PLAYERS];

OnPlayerUpdate(playerid)
{
    if((GetTickCount() - tick[playerid]) > 1000)
    {
        tick[playerid] = GetTickCount();
        //do stuff
    }
    return 1;
}
No messing around with silly timers and loops.


Re: Timer Efficency - BP13 - 08.09.2010

Quote:
Originally Posted by RoBo
Посмотреть сообщение
pawn Код:
new tick[MAX_PLAYERS];

OnPlayerUpdate(playerid)
{
    if((GetTickCount() - tick[playerid]) > 1000)
    {
        tick[playerid] = GetTickCount();
        //do stuff
    }
    return 1;
}
No messing around with silly timers and loops.
So that stuff about OnPlayerUpdate using more CPU is a bunch of BS?


Re: Timer Efficency - RoBo - 08.09.2010

Quote:
Originally Posted by BP13
Посмотреть сообщение
So that stuff about OnPlayerUpdate using more CPU is a bunch of BS?
Pretty much, yes.


Re: Timer Efficency - nemesis- - 08.09.2010

Quote:
Originally Posted by BP13
Посмотреть сообщение
So that stuff about OnPlayerUpdate using more CPU is a bunch of BS?
If you apply what RoBo mentioned (nothing new, simple math) and leave out the array of MAX_PLAYERS loops that some people seem to use so frequently, yeah, then OPU isn't that bad.


Re: Timer Efficency - Jay_ - 08.09.2010

Do not use OnPlayerUpdate. OnPlayerUpdate gets called 10x a second, per player. Use a timer, it's much more efficient. OnPlayerUpdate shouldn't contain ANY code or VERY little.


Re: Timer Efficency - nemesis- - 08.09.2010

Quote:
Originally Posted by Jay_
Посмотреть сообщение
Do not use OnPlayerUpdate. OnPlayerUpdate gets called 10x a second, per player. Use a timer, it's much more efficient. OnPlayerUpdate shouldn't contain ANY code or VERY little.
WRONG but thanks for playing.


Re: Timer Efficency - Westie - 08.09.2010

So, you're saying that a timer that blocks the execution for a long time in comparison is more efficient than a tick based system?


Re: Timer Efficency - nemesis- - 08.09.2010

Quote:
Originally Posted by Westie
Посмотреть сообщение
So, you're saying that a timer that blocks the execution for a long time in comparison is more efficient than a tick based system?
A tick-based system distributes execution based on a player's relative timeframe. The timer executes all code against all players at once. I like the distributed method.


Re: Timer Efficency - cyber_punk - 08.09.2010

Quote:
Originally Posted by Jay_
Посмотреть сообщение
Do not use OnPlayerUpdate. OnPlayerUpdate gets called 10x a second, per player. Use a timer, it's much more efficient. OnPlayerUpdate shouldn't contain ANY code or VERY little.
Actually it gets called way more than 10x a second what do you think the send rate is for? From my tests an accurate way of finding out how often its called is like this....take your *_rate (onfoot, incar, weapon) from server config and divide that by 1000, for example default rate is 40 so on average its called every 25 milliseconds the only time this isn't true is when a player is standing still the rate lowers. (something I wish was never changed....in 0.2x it was called standing still just as often as running/moving) Also if you use it correctly its much more efficient.