onplayerupdate laggging (profile)
# support-forum
c
The plugin profile told me that my
OnPlayerUpdate
is using a lot of resources but i'm only using it to gettime
Copy code
c++
public OnPlayerUpdate(playerid)
{
    AFKTime[playerid] = gettime();
}
so i can detect if player pauses in other function
Copy code
c++
        // check paused
        if(paused[i] == false) {
            if(gettime() - AFKTime[i] > 3) { OnPlayerPause(i); } // 3 secs no answer
        }
        if(paused[i] == true) {
            if(gettime() - AFKTime[i] < 3) { OnPlayerUnpause(i); }
        }
is anybody knows a better way to do it?
w
use Tick count instead
b
OnPlayerUpdate is called ± 30 times per second, we hope it be used a lot
but is u really getting lag?
i saw OnPlayerUpdate public's with gigant codes working fine
g
OnPlayerUpdate does not get that much lag, i don't know why u keep sticking to it in several posts. As victor said, many gamemodes use giant blocks of code, and yet is normal, probably it can be another include / plugin you are using that is causing the lag.
c
i'll try it
but there is no way to slow down the onplayerupdate? its the only callback i can use to check afk, but it calls 30x per sec
yes, that print is from profile, a plugin to check cpu usage
looks like gettime there is eating my cpu out
i have no libs in my server, the list of profile shows
OnPlayerUpdate
first, `gettime`second with almost same calls (a lot, different from the others) it have to be it
g
either way it'll be the most called even if it has nothing on it
c
It is called 30 times per second in the worst case (when player, for example, driving a vehicle), but nothing like that when he is just stand and move his camera or moving onfoot. Moreover it is called 0 times per second when he's in afk as mentioned. Taking 30 times per sec as a constant rule for this behaviour is wrong, it's an edge case.
As for "slowing it down" you can edit rates in server config file (namely onfoot_rate and incar_rate for samp server), but it will affect the quality of synchronization.
c
thank you very much @creamy-river-65943
do u have any idea why my onplayerupdate is that heavy to CPU? https://media.discordapp.net/attachments/1089736888056815647/1089742481316393011/Screenshot_from_2023-03-26_23-46-20.png?width=1020&height=134 having a lot of actors in game could call this callback? (i got more then 3000 all time) or its just gettime that is heavy to be there?
do u know any other callback or way to detect players paused?
g
OnPlayerPause
c
no, actors don't call this callback
but where is the "heavy to CPU"?
what is posted on the screen is just the percentage of load from the total, which gives the server as a whole
did you checked the real CPU load at that time?
c
its using 84% now with 50 players on
but those are the functions that most uses cpu, right? 17.52% of the whole functions
c
they do most load comparing to any other calls, yeah
oh, that's pretty bad indeed, try to find some filterscript or include if it uses this callback anyhow, besides the code in your main gm
also some information about server tickrate will be helpful, you can make something like "/serverstats" command where such values will be displayed in game by the request
you can use GetServerTickRate (or GetNetworkStats, to print more detailed information) for that and check it from time to time
what's your hardware btw?
c
i had, there isnt, i think it is that gettime stuff
but i have an idea to make it lighter:
Copy code
c++
public OnPlayerUpdate(playerid)
{
    if(isLogged[playerid] == true)
    {
        if(updatingAFKTime[playerid] == false) {
            AFKTime[playerid] = GetTickCount();
            updatingAFKTime[playerid] = true;
            SetTimerEx("ResetUpdatingAFKTime", 250, false, "i", playerid);
        }
    }
}
public ResetUpdatingAFKTime(playerid) {
    updatingAFKTime[playerid] = false;
}
what do u think?
c
Find a real cause instead of making such changes to the code that already doesn't have performance problems itself
s
sometimes, vps provider itself is sucks instead of the code. try it on local or different vps provider. check the performance difference with 5-10 players. use clean gta + samp addon to multiply join your server in the same device
your OnPlayerUpdate code looks fine btw, I guess it takes 17% because the amount of it being called continuously
g
what
why u did that?
isnt better to just get the tickcount when the player is afk?
when he types /afk