Vehicle use
#1

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;
}
Reply
#2

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);
    }

Reply
#3

Thanks, I eliminated lag.
Reply
#4

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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)