SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 27/12/2012, 05:10 AM   #1
The__
Gangsta
 
Join Date: Nov 2011
Posts: 519
Reputation: 53
Default Heavy CPU usage...

I keep having heavy CPU usage when players are logged in, using DOF2 saving system, my OnPlayerUpdate isn't so heavy...timers neither...if you'd like any part of the code, let me know.
The__ is offline   Reply With Quote
Old 27/12/2012, 03:04 PM   #2
The__
Gangsta
 
Join Date: Nov 2011
Posts: 519
Reputation: 53
Default Re: Heavy CPU usage...

shameless self bump :/.
The__ is offline   Reply With Quote
Old 27/12/2012, 03:08 PM   #3
Gh05t_
Huge Clucker
 
Gh05t_'s Avatar
 
Join Date: Dec 2011
Posts: 196
Reputation: 140
Default Re: Heavy CPU usage...

How about some info that may actually be of use to us.
Gh05t_ is offline   Reply With Quote
Old 27/12/2012, 03:12 PM   #4
Hiddos
High-roller
 
Hiddos's Avatar
 
Join Date: Sep 2008
Posts: 4,844
Reputation: 2179
Default Re: Heavy CPU usage...

Well you could show us your OnPlayerUpdate to start with
__________________
Quote:
Originally Posted by DeWolf View Post
For fuck's sake people, this whole thread is fucked up now.
Hiddos is offline   Reply With Quote
Old 27/12/2012, 03:12 PM   #5
The__
Gangsta
 
Join Date: Nov 2011
Posts: 519
Reputation: 53
Default Re: Heavy CPU usage...

Quote:
Originally Posted by The__ View Post
I keep having heavy CPU usage when players are logged in, using DOF2 saving system, my OnPlayerUpdate isn't so heavy...timers neither...if you'd like any part of the code, let me know.
What else would you want to know ?
EDIT:
Copying my onplayerupdate, hold on.
EDIT2:
Code:
public OnPlayerUpdate(playerid)
{
    if(!BoomboxStream[playerid])
    {
        for(new i=0; i<MAX_PLAYERS; i++)
        {
            if(IsPlayerConnected(i))
            {
                if(Boombox[i])
                {
                    if(IsPlayerInRangeOfPoint(playerid, 30, bpos[i][0], bpos[i][1], bpos[i][2]))
                    {
                        PlayAudioStreamForPlayer(playerid, BoomboxURL[i], bpos[i][0], bpos[i][1], bpos[i][2], 30, 1);
                        BoomboxPlayer[playerid] = i;
                        BoomboxStream[playerid] = 1;
                        SendClientMessage(playerid, COLOR_GREY, " You are listening to music coming out of a nearby boombox.");
                    }
                }
            }
        }
    }
    else
    {
        new i = BoomboxPlayer[playerid];
        if(!IsPlayerInRangeOfPoint(playerid, 30, bpos[i][0], bpos[i][1], bpos[i][2]))
        {
            BoomboxStream[playerid] = 0;
            BoomboxPlayer[playerid] = -1;
            StopAudioStreamForPlayer(playerid);
            SendClientMessage(playerid, COLOR_GREY, " You have went far away from the boombox.");
        }
    }
	new Float:hp;
	GetPlayerHealth(playerid,hp);
	if(hp <= 30) SetPlayerDrunkLevel(playerid,1000);
	if(hp == 100) SetPlayerDrunkLevel(playerid,0);
	if(IsPlayerInAnyVehicle(playerid))
	{
	    GetVehicleHealth(GetPlayerVehicleID(playerid), vhp);
		if(vhp < vhealth[playerid] || vhp > vhealth[playerid])
		{
			if(vhp < vhealth[playerid]) OnVehicleLoseHealth(playerid,GetPlayerVehicleID(playerid),floatround(vhealth[playerid]-vhp));
			vhealth[playerid] = vhp;
		}
		if(vhp == vhealth[playerid]) GetVehicleVelocity(GetPlayerVehicleID(playerid),velX[playerid],velY[playerid],velZ[playerid]);
	}
	if(SeatBelt[playerid] != 1)
	{
		if(GetPVarInt(playerid,"FallFromVeh") == 1)
		{
			new Float:vX, Float:vY, Float:vZ;
			GetPlayerVelocity(playerid,vX,vY,vZ);
			if(vZ == 0)
			{
			    new Float:php;
			    ClearAnimations(playerid);
				GetPlayerHealth(playerid, php);
				SetPlayerHealth(playerid, php-30);
				if(php-30 <= 0 || php <= 0)
				{
				    SetPlayerDrunkLevel(playerid,0);
				    SetPVarInt(playerid,"FallFromVeh",0);
				    return 1;
				}
				SetPlayerDrunkLevel(playerid,10000);
				SetTimerEx("Refix",5000,0,"i",playerid);
				SetTimerEx("Animation",500,0,"i",playerid);
				TogglePlayerControllable(playerid,0);
				ApplyAnimation(playerid, "PARACHUTE", "FALL_skyDive_DIE",4,0,0,0,1,0);
				SetPVarInt(playerid,"FallFromVeh",0);
			}
		}
	}
    if(GetPlayerMoney(playerid) != PCash[playerid]) {
        SetPlayerMoney(playerid,PCash[playerid]);
    }
    if(GetPlayerState(playerid) == PLAYER_STATE_DRIVER && Speedlimit[playerid])
	{
 		new a, b, c;
		GetPlayerKeys(playerid, a, b ,c);
  		if(a == 8 && GetVehicleSpeed(GetPlayerVehicleID(playerid), 0) > Speedlimit[playerid])
    	{
     		new newspeed = GetVehicleSpeed(GetPlayerVehicleID(playerid), 0) - Speedlimit[playerid];
    		ModifyVehicleSpeed(GetPlayerVehicleID(playerid), -newspeed);
    	}
	}
    if(PayDayAuth[playerid] == 1)
	{
	    PayDayAuth[playerid] = 0;
	    PayDay(playerid);
	}

    return 1;
}
The__ is offline   Reply With Quote
Old 27/12/2012, 04:11 PM   #6
BlackBank
High-roller
 
BlackBank's Avatar
 
Join Date: Dec 2010
Location: The Netherlands
Posts: 1,176
Reputation: 435
Default Re: Heavy CPU usage...

Quote:
Originally Posted by The__ View Post
What else would you want to know ?
EDIT:
Copying my onplayerupdate, hold on.
EDIT2:
Code:
public OnPlayerUpdate(playerid)
{
    if(!BoomboxStream[playerid])
    {
        for(new i=0; i<MAX_PLAYERS; i++)
        {
            if(IsPlayerConnected(i))
            {
                if(Boombox[i])
                {
                    if(IsPlayerInRangeOfPoint(playerid, 30, bpos[i][0], bpos[i][1], bpos[i][2]))
                    {
                        PlayAudioStreamForPlayer(playerid, BoomboxURL[i], bpos[i][0], bpos[i][1], bpos[i][2], 30, 1);
                        BoomboxPlayer[playerid] = i;
                        BoomboxStream[playerid] = 1;
                        SendClientMessage(playerid, COLOR_GREY, " You are listening to music coming out of a nearby boombox.");
                    }
                }
            }
        }
    }
    else
    {
        new i = BoomboxPlayer[playerid];
        if(!IsPlayerInRangeOfPoint(playerid, 30, bpos[i][0], bpos[i][1], bpos[i][2]))
        {
            BoomboxStream[playerid] = 0;
            BoomboxPlayer[playerid] = -1;
            StopAudioStreamForPlayer(playerid);
            SendClientMessage(playerid, COLOR_GREY, " You have went far away from the boombox.");
        }
    }
	new Float:hp;
	GetPlayerHealth(playerid,hp);
	if(hp <= 30) SetPlayerDrunkLevel(playerid,1000);
	if(hp == 100) SetPlayerDrunkLevel(playerid,0);
	if(IsPlayerInAnyVehicle(playerid))
	{
	    GetVehicleHealth(GetPlayerVehicleID(playerid), vhp);
		if(vhp < vhealth[playerid] || vhp > vhealth[playerid])
		{
			if(vhp < vhealth[playerid]) OnVehicleLoseHealth(playerid,GetPlayerVehicleID(playerid),floatround(vhealth[playerid]-vhp));
			vhealth[playerid] = vhp;
		}
		if(vhp == vhealth[playerid]) GetVehicleVelocity(GetPlayerVehicleID(playerid),velX[playerid],velY[playerid],velZ[playerid]);
	}
	if(SeatBelt[playerid] != 1)
	{
		if(GetPVarInt(playerid,"FallFromVeh") == 1)
		{
			new Float:vX, Float:vY, Float:vZ;
			GetPlayerVelocity(playerid,vX,vY,vZ);
			if(vZ == 0)
			{
			    new Float:php;
			    ClearAnimations(playerid);
				GetPlayerHealth(playerid, php);
				SetPlayerHealth(playerid, php-30);
				if(php-30 <= 0 || php <= 0)
				{
				    SetPlayerDrunkLevel(playerid,0);
				    SetPVarInt(playerid,"FallFromVeh",0);
				    return 1;
				}
				SetPlayerDrunkLevel(playerid,10000);
				SetTimerEx("Refix",5000,0,"i",playerid);
				SetTimerEx("Animation",500,0,"i",playerid);
				TogglePlayerControllable(playerid,0);
				ApplyAnimation(playerid, "PARACHUTE", "FALL_skyDive_DIE",4,0,0,0,1,0);
				SetPVarInt(playerid,"FallFromVeh",0);
			}
		}
	}
    if(GetPlayerMoney(playerid) != PCash[playerid]) {
        SetPlayerMoney(playerid,PCash[playerid]);
    }
    if(GetPlayerState(playerid) == PLAYER_STATE_DRIVER && Speedlimit[playerid])
	{
 		new a, b, c;
		GetPlayerKeys(playerid, a, b ,c);
  		if(a == 8 && GetVehicleSpeed(GetPlayerVehicleID(playerid), 0) > Speedlimit[playerid])
    	{
     		new newspeed = GetVehicleSpeed(GetPlayerVehicleID(playerid), 0) - Speedlimit[playerid];
    		ModifyVehicleSpeed(GetPlayerVehicleID(playerid), -newspeed);
    	}
	}
    if(PayDayAuth[playerid] == 1)
	{
	    PayDayAuth[playerid] = 0;
	    PayDay(playerid);
	}

    return 1;
}
Remove this out of your OnPlayerUpdate code±
PHP Code:
        for(new i=0i<MAX_PLAYERSi++)
        {
            if(
IsPlayerConnected(i))
            {
                if(
Boombox[i])
                {
                    if(
IsPlayerInRangeOfPoint(playerid30bpos[i][0], bpos[i][1], bpos[i][2]))
                    {
                        
PlayAudioStreamForPlayer(playeridBoomboxURL[i], bpos[i][0], bpos[i][1], bpos[i][2], 301);
                        
BoomboxPlayer[playerid] = i;
                        
BoomboxStream[playerid] = 1;
                        
SendClientMessage(playeridCOLOR_GREY" You are listening to music coming out of a nearby boombox.");
                    }
                }
            }
        } 
You are looping every time when OnPlayerUpdate is called on all the players...

If MAX_PLAYERS is 500 and there is one player online, it will loop everytime when OnPlayerUpdate is callled.

If there are more players, like 7 players, it will be like this: 7 * 500.
So the more the server needs to do.
BlackBank is offline   Reply With Quote
Old 27/12/2012, 04:39 PM   #7
The__
Gangsta
 
Join Date: Nov 2011
Posts: 519
Reputation: 53
Default Re: Heavy CPU usage...

Quote:
Originally Posted by BlackBank3 View Post
...
How about this loop ?
Code:
for(new i, j = GetMaxPlayers(); i < j; i++)
The__ is offline   Reply With Quote
Old 27/12/2012, 07:32 PM   #8
Y_Less
Spam Machine
 
Y_Less's Avatar
 
Join Date: Jun 2008
Location: 629 - git.io/Y
Posts: 14,897
Reputation: 3126
Default Re: Heavy CPU usage...

That loop is worse - "foreach" exists for a reason (and I have the debate about which one is better almost weekly, still none have managed to beat "foreach").
Y_Less is online now   Reply With Quote
Old 27/12/2012, 08:12 PM   #9
BlackBank
High-roller
 
BlackBank's Avatar
 
Join Date: Dec 2010
Location: The Netherlands
Posts: 1,176
Reputation: 435
Default Re: Heavy CPU usage...

Quote:
Originally Posted by The__ View Post
How about this loop ?
Code:
for(new i, j = GetMaxPlayers(); i < j; i++)
I looked back, and this whole code doesn't even works well.
PHP Code:
    if(!BoomboxStream[playerid])
    {
        for(new 
i=0i<MAX_PLAYERSi++)
        {
            if(
IsPlayerConnected(i))
            {
                if(
Boombox[i])
                {
                    if(
IsPlayerInRangeOfPoint(playerid30bpos[i][0], bpos[i][1], bpos[i][2]))
                    {
                        
PlayAudioStreamForPlayer(playeridBoomboxURL[i], bpos[i][0], bpos[i][1], bpos[i][2], 301);
                        
BoomboxPlayer[playerid] = i;
                        
BoomboxStream[playerid] = 1;
                        
SendClientMessage(playeridCOLOR_GREY" You are listening to music coming out of a nearby boombox.");
                    }
                }
            }
        }
    }
    else
    {
        new 
BoomboxPlayer[playerid];
        if(!
IsPlayerInRangeOfPoint(playerid30bpos[i][0], bpos[i][1], bpos[i][2]))
        {
            
BoomboxStream[playerid] = 0;
            
BoomboxPlayer[playerid] = -1;
            
StopAudioStreamForPlayer(playerid);
            
SendClientMessage(playeridCOLOR_GREY" You have went far away from the boombox.");
        }
    } 
BlackBank is offline   Reply With Quote
Old 27/12/2012, 08:15 PM   #10
The__
Gangsta
 
Join Date: Nov 2011
Posts: 519
Reputation: 53
Default Re: Heavy CPU usage...

So to replace my loop with foreach ?
EDIT:
changed it to this
Code:
if(!BoomboxStream[playerid])
    {
        foreach(new i : Player)
        {
	        if(Boombox[i])
	        {
		        if(IsPlayerInRangeOfPoint(playerid, 30, bpos[i][0], bpos[i][1], bpos[i][2]))
		        {
		        PlayAudioStreamForPlayer(playerid, BoomboxURL[i], bpos[i][0], bpos[i][1], bpos[i][2], 30, 1);
		        BoomboxPlayer[playerid] = i;
		        BoomboxStream[playerid] = 1;
		        SendClientMessage(playerid, COLOR_GREY, " You are listening to music coming out of a nearby boombox.");
		        }
	        }
        }
    }
    else
    {
        new i = BoomboxPlayer[playerid];
        if(!IsPlayerInRangeOfPoint(playerid, 30, bpos[i][0], bpos[i][1], bpos[i][2]))
        {
            BoomboxStream[playerid] = 0;
            BoomboxPlayer[playerid] = -1;
            StopAudioStreamForPlayer(playerid);
            SendClientMessage(playerid, COLOR_GREY, " You have went far away from the boombox.");
        }
    }
I'm going to sleep now..so dicuss and try to help my fucking CPU usage :/...good night.
The__ is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Tutorial] SA:MP Images usage, list and usage. Partner Tutorials 23 26/03/2016 10:00 PM
too much CPU usage!? Oscii Server Support 5 28/07/2012 03:02 PM
CPU Usage - ... Kyle Scripting Help 11 28/05/2012 09:43 PM
About CPU Usage Ricop522 Server Support 7 09/12/2011 12:29 PM
Too much CPU usage Rickzor14 Server Support 9 12/03/2011 06:52 PM


All times are GMT. The time now is 01:54 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.