SA-MP Forums Archive
Vehicle use - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Vehicle use (/showthread.php?tid=594475)



Vehicle use - SpriTe - 18.11.2015

Hello I have a question, how to optimize code to stop execute so many loops.
Код:
for(new i = 1; i <= Total_Veh_Created; i++)
{
	if(vInfo[i][vSpawned] != 0 && vInfo[i][vDespawnTime] > 0 && VehicleUse(vInfo[i][vSpawned])) vInfo[i][vDespawnTime]--;
	else if(vInfo[i][vSpawned] != 0 && vInfo[i][vDespawnTime] <= 0 && VehicleUse(vInfo[i][vSpawned]))
	{
		new vid = vInfo[i][vSpawned];
    	vUpdate(i, vKmx);
		OwnedVeh(vid) = 0;
		DestroyVehicle(vid);
    	vInfo[i][vSpawned] = 0;
		vUpdate(i, vSpawnedx);
	}
} - This code is executed every minute and make lag.

function VehicleUse(vehicleid)
{
	new HighestPlayerId = GetPlayerPoolSize();
	for(new i=0; i <= HighestPlayerId; i++)
	{
		if(IsPlayerInVehicle(i, vehicleid)) return 0;
	}
	return 1;
}



Re: Vehicle use - Mencent - 18.11.2015

Hello!

Try this:
PHP код:
for(new i=1;i<=Total_Veh_Created;i++)
{
    if(
vInfo[i][vSpawned] == 0)continue;
    if(
vInfo[i][vDespawnTime] > && VehicleUse(vInfo[i][vSpawned]))
    {
        
vInfo[i][vDespawnTime] --;
    }
    else
    {
        new 
vid vInfo[i][vSpawned];
        
vUpdate(i,vKmx);
        
OwnedVeh(vid) = 0;
        
DestroyVehicle(vid);
        
vInfo[i][vSpawned] = 0;
        
vUpdate(i,vSpawnedx);
    }




Re: Vehicle use - SpriTe - 18.11.2015

Thanks, I eliminated lag.


Re: Vehicle use - Macluawn - 18.11.2015

Some notes:
In the loop you have Total_Veh_Created. What happens when you create, say, 100 vehicles and delete the 50th vehicle.. total vehicles will be 99, but ID 100 will still be in use and the loop won`t reach that vehicle.

A similar problem with VehicleUse function. Just because the maximum player ID in use is 50, doesnt mean that all ID's from 0 to 50 are taken.