OnPlayerUpdate Lag
#1

__________________________
Reply
#2

Don't run it on OnPlayerUpdate.
Loop it through a timer.
OnPlayerUpdate updates so quickly that if you have a decent playerbase it'll cause sufficient lag throughout the server.
Reply
#3

__________________________
Reply
#4

OnPlayerUpdate, in terms of modern hardware, isn't something that's TOO frequent, I suppose (a lot of operations go a lot deeper into the timing). But what the source of problems in this code is that in way too often cases you update the textdraw string. It does not show visually to the player since it is a textdraw, but if you would have it as a client message, you would see the hell it causes in your chatbox.

There is a wide range of stuff you should not do so frequently and when communicating with the player through an API of some kind, it is good to remember that you should interact with it as little as possible.

I'm not sure about the speed of the natives causing any trouble here, but the actual formatting and the internal commands that these natives issue. So only update when necessary, and don't do it so often!

// EDIT:
wrong with the loop is that you don't have a playerid to operate on. For this, you must loop through all players.
pawn Код:
for(int playerid = 0; playerid != MAX_PLAYERS; playerid++)
{
    if(GetPlayerWantedLevel(playerid) > 0)
    {
        // code
    }
}
(Note that I don't run an IsPlayerConnected check because I can be sure that GetPlayerWantedLevel returns 0 for disconnected players)
Reply
#5

__________________________
Reply
#6

Код:
new updatetimer[MAX_PLAYERS];


OnGameModeInit:
----
updatetimer[playerid] = SetTimerEx("update", 1000, true, "d", playerid);
----


forward update(playerid);
public update(playerid)
{
	for(int playerid = 0; playerid != MAX_PLAYERS; playerid++)
	{
	    if(GetPlayerWantedLevel(playerid) >= 1)
		{
		new string[124];
		format(string, sizeof(string), "~b~~h~Tickets~h~(%d/10): ~g~~h~$%d000~n~~r~/ticket to pay", GetPlayerWantedLevel(playerid), GetPlayerWantedLevel(playerid));
		TextDrawSetString(Ticket, string);
		Ticket = TextDrawCreate(497.000000, 129.000000, " ");
		return 1;
		}
	}
	return 1;
}
Reply
#7

__________________________
Reply
#8

Код:
new updatetimer[MAX_PLAYERS];


OnGameModeInit:
----
updatetimer[playerid] = SetTimerEx("update", 1000, true, "d", playerid);
----


forward update(playerid);
public update(playerid)
{
	for(new playerid = 0; playerid < MAX_PLAYERS; playerid++)
	{
	    if(GetPlayerWantedLevel(playerid) >= 1)
		{
		new string[124];
		format(string, sizeof(string), "~b~~h~Tickets~h~(%d/10): ~g~~h~$%d000~n~~r~/ticket to pay", GetPlayerWantedLevel(playerid), GetPlayerWantedLevel(playerid));
		TextDrawSetString(Ticket, string);
		Ticket = TextDrawCreate(497.000000, 129.000000, " ");
		return 1;
		}
	}
	return 1;
}
Reply
#9

__________________________
Reply
#10

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


OnGameModeInit:
----
updatetimer[playerid] = SetTimerEx("update", 1000, true, "d", playerid);
----


forward update(playerid);
public update(playerid)
{
	for(new playerid = 0; playerid < MAX_PLAYERS; playerid++)
	{
	    if(GetPlayerWantedLevel(playerid) >= 1)
		{
		new string[124];
		format(string, sizeof(string), "~b~~h~Tickets~h~(%d/10): ~g~~h~$%d000~n~~r~/ticket to pay", GetPlayerWantedLevel(playerid), GetPlayerWantedLevel(playerid));
		TextDrawSetString(Ticket, string);
		Ticket = TextDrawCreate(497.000000, 129.000000, " ");
		return 1;
		}
	}
	return 1;
}
Thats bad, you have already defined 'playerid' as parameter, should use it in a loop again.

Fix:

pawn Код:
forward UpdateWanted();
public UpdateWanted()
{
    for(new i, z = GetMaxPlayers(); i < z; i++)
    {
        if(!IsPlayerConnected(i) || GetPlayerWantedLevel(playerid) == 0)
            continue;
        new string[128];
        format(string, sizeof string, "~b~~h~Tickets~h~(%d/10): ~g~~h~$%d000~n~~r~/ticket to pay", GetPlayerWantedLevel(playerid), GetPlayerWantedLevel(playerid));
        return TextDrawSetString(Ticket, string);
    }
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)