SA-MP Forums

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

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Old 19/08/2019, 11:32 PM   #1
FacuScript
Little Clucker
 
Join Date: Feb 2019
Posts: 6
Reputation: 0
Question How can I optimize this code?

Let nothing forgive my level of English.

Hi, I have a problem, the problem is that my CPU collapses completely, the CPU where I host my server, after I implement a code ..

I summarize briefly, add 17 NCP, create a couple of variables to activate an event, when the event is activated those players who are close to the position X, Y, Z get them the positions, then the 17 NCPs point and shoot with the updated position of the player, every 1 second ..

When I go alone to the place, the server is washed but it does not collapse, but when 2 or 3 players go in the same place, it is like the code, takes the positions of those 2 or 3 players and applies it to the 17 NCP , if we take accounts would be 17 x 3 = 51 queries per second ..


My server has 1GB RAM, I know very little, but I am also sure that the code can be optimized so that this does not happen.

I show you briefly

PHP Code:
if(eventobarco == 0// TIME FOR START OF EVENT, LOCATED WITHIN CALLBACK
    
{
    
Update3DTextLabelText(barcoevento,COLOR_WHITE,"{00CC00}Estado: ACTIVO\n{FFFFFF}Faltan 10 MIN para finalizar");
    
barcoeventoss 1;
    
tiemporestante 10;
    
SetTimer("tiempodeevento",60000,false); //TIMER COMMAND FOR THE UPDATE OF TIME
    
SetTimer("Shoot",1000,false); //HERE IS THE IMPORTANT, CALL THE CALLBACK SHOOT IN 1 SECOND

    
}
    return 
1
In The Calback Shoot

PHP Code:
public Shoot(playerid)
{
new 
Float:X,Float:Y,Float:Z;
for(new 
0MAX_PLAYERSi++) //ALL PLAYERS CONNECTED
{
if(
IsPlayerConnected(i) && !IsPlayerNPC(i)) //IF IT IS CONNECTED AND IT IS NOT A BOOT
{
if(
IsPlayerInRangeOfPoint(i100.0, -1456.6010,1500.7106,6.9688)) // AND IT'S ON THIS RADIO
{
if(
barcoeventoss == 1//AND THE EVENT IS ACTIVATED
{
GetPlayerPos(i,X,Y,Z);  // OBTAINS THE POSITIONS OF ALL WHO ARE WITHIN THE RADIO
//APPLY THIS AT 17 NCP
FCNPC_AimAtPlayer(barco1playeridtrue, -1true0.00.00.5);
FCNPC_AimAtPlayer(barco2playeridtrue, -1true0.00.00.5);
FCNPC_AimAtPlayer(barco3playeridtrue, -1true0.00.00.5);
FCNPC_AimAtPlayer(barco4playeridtrue, -1true0.00.00.5);
FCNPC_AimAtPlayer(barco5playeridtrue, -1true0.00.00.5);
FCNPC_AimAtPlayer(barco6playeridtrue, -1true0.00.00.5);
FCNPC_AimAtPlayer(barco7playeridtrue, -1true0.00.00.5);
FCNPC_AimAtPlayer(barco8playeridtrue, -1true0.00.00.5);
FCNPC_AimAtPlayer(barco9playeridtrue, -1true0.00.00.5);
FCNPC_AimAtPlayer(barco10,playeridtrue, -1true0.00.00.5);
FCNPC_AimAtPlayer(barco11,playeridtrue, -1true0.00.00.5);
FCNPC_AimAtPlayer(barco12,playeridtrue, -1true0.00.00.5);
FCNPC_AimAtPlayer(barco13,playeridtrue, -1true0.00.00.5);
FCNPC_AimAtPlayer(barco14,playeridtrue, -1true0.00.00.5);
FCNPC_AimAtPlayer(barco15,playeridtrue, -1true0.00.00.5);
FCNPC_AimAtPlayer(barco16,playeridtrue, -1true0.00.00.5);
FCNPC_AimAtPlayer(barco17,playeridtrue, -1true0.00.00.5);
}
SetTimer("Shoot",1500,false); // SEND A TIMER OF 1 SECOND AND THE FUNCTION IS REPEATED 
My question is the following, how can I optimize that code? For example, that instead of obtaining the positions of all the players within the radius, it takes only one at random, of all those inside, and that the CPU usage is reduced, how can I optimize this so that the VPS don't collapse me

Include that I am using

PHP Code:
#include <FCNPC> 

could you help me? thank you
FacuScript is offline   Reply With Quote
 

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
optimize code DeadNudock Scripting Help 4 25/06/2015 11:57 PM
optimize code JoshNudock Scripting Help 4 22/06/2015 07:38 PM
Optimize code Face9000 Scripting Help 4 05/01/2014 10:43 PM
OPTIMIZE CODE!!!!!!!! Nadim Scripting Help 2 01/11/2013 03:43 PM


All times are GMT. The time now is 09:25 PM.


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