[OPU question] Which one is better? -
newbienoob - 06.04.2013
pawn Код:
public OnPlayerUpdate(playerid)
{
for(new i = 0; i < MAX_PLAYERS; i++)
{
if(IsPlayerInRangeOfPoint(i, 10, 7.6219, 1522.2793, 21.0712))
{
//Something
}
}
return 1;
}
public OnPlayerUpdate(playerid)
{
if(IsPlayerInRangeOfPoint(playerid, 10, 7.6219, 1522.2793, 21.0712))
{
//Something
}
return 1;
}
With or without loop?
Re: [OPU question] Which one is better? -
iJumbo - 06.04.2013
why you have to do with a loop if OnplayerUopdate callback return already playerid?
Re: [OPU question] Which one is better? -
MP2 - 06.04.2013
He probably meant to say is it better to use a timer with a loop or OPU. It totally depends what you're doing!
In any case, you should use foreach.
Re: [OPU question] Which one is better? -
newbienoob - 06.04.2013
Quote:
Originally Posted by jumbo
why you have to do with a loop if OnplayerUopdate callback return already playerid?
|
Quote:
Originally Posted by mp5
He probably meant to say is it better to use a timer with a loop or OPU. It totally depends what you're doing!
In any case, you should use foreach.
|
Why foreach? What's the difference between for(....) and foreach?
Re: [OPU question] Which one is better? -
Threshold - 06.04.2013
Well foreach basically does the following for you:
is the same as:
pawn Код:
for(new i = 0; i < MAX_PLAYERS; i++)
{
if(IsPlayerConnected(i))
{
It's quicker to use foreach, and much more efficient, hence its wide usage throughout sa-mp servers. There's a full thread explaining its uses. Also, I would have to say that creating a loop within OnPlayerUpdate for EVERY player connected, would have to cause much more usage when compared to using the simple 'playerid'. That's general knowledge I am speaking of, although I have no sufficient evidence to actually back up my claim such as how usage is generated etc.
Re: [OPU question] Which one is better? -
newbienoob - 06.04.2013
Quote:
Originally Posted by BenzoAMG
Also, I would have to say that creating a loop within OnPlayerUpdate for EVERY player connected, would have to cause much more usage when compared to using the simple 'playerid'.
|
That kind of answer I would like to see. But I need someone to confirm it.
Is it true, using a loop inside of OPU would cause more ram usage?
Re: [OPU question] Which one is better? -
SuperViper - 06.04.2013
It causes more general resource usage, not RAM specifically. Think about this:
OnPlayerUpdate is called approximately 33 times per second. You loop through 500 players and perform an action for each one. If the code inside of your loop takes even 1 milasecond, it will equal 500 milaseconds total and your server will freeze because it's performing 500 milasecond code every 30 milaseconds
PER PLAYER.
Re: [OPU question] Which one is better? -
newbienoob - 06.04.2013
Ahh.. Thank you. So, the second one is better than the first one.
Re: [OPU question] Which one is better? -
MP2 - 06.04.2013
Quote:
Originally Posted by SuperViper
OnPlayerUpdate is called approximately 33 times per second.
|
Actually.. I decided to test this, as I was curious.
I ran this, alone on my local server:
pawn Код:
#include a_samp
#define DEBUG true
#include debug
new time;
new calls;
public OnPlayerUpdate(playerid)
{
if(time != gettime())
{
time = gettime();
Debug_SCMFORMAT("OPU calls this second: %i", calls); // This is a custom macro/function I made which acts like printf for SCMs
calls = 0;
}
calls++;
return 1;
}
Couldn't get more than 25 updates per second (while running around shooting Tec9's). Got max 23 while flying around in a hydra shooting rockets and shit. I managed to get to 35 by spamming a load of keys, but I doubt everyone runs around doing that.
I never realised it was called this much. I thought perhaps 10 or so. It's only ~2 when you're on foot and stationary.
I'd say it's 22 on average.
P.S. It's millisecond, not milasecond.