Respawn All Vehicles -
xRadical3 - 15.04.2019
pawn Code:
function RespawnAllVehicles()
{
for( new i = i; i <= MAX_VEHICLES; i ++ )
{
if(i != INVALID_VEHICLE_ID)
{
if(IsVehicleEmpty(i))
{
SetVehicleToRespawn(i);
}
}
}
return 1;
}
When i used `RespawnAllVehicles` function in my scripts
Vehicles late too respawn and server lagged for 10second also players..
How to fix??
Re: Respawn All Vehicles -
AdamsLT - 15.04.2019
You need to use foreach for this is. That way you won't need to loop over unused vehicleids so this loop will become a lot quicker.
But even then, lagging for 10 seconds seems unusually long. Are you sure this is your entire code? Because your loop seems weird: new i = i; should be new i = 0;
I'm pretty sure it shouldn't even compile with your current code.
Re: Respawn All Vehicles -
BsLOUAY - 15.04.2019
use this Code
PHP Code:
CMD:respawncars(playerid, params[])
{
//check if the player is a admin
LevelCheck(playerid, 4);
for(new cars; cars < MAX_VEHICLES; cars++)
{
LOOP_PLAYERS(i)
{
PlayerPlaySound(i, 1057, 0.0, 0.0, 0.0);
if(GetPlayerVehicleID(i) == cars)
{
if(GetPlayerState(i) == PLAYER_STATE_DRIVER)
{
if(GetPlayerGAdminLevel(playerid) < GetPlayerGAdminLevel(i))
{
SetVehicleToRespawn(cars);
}
}
else SetVehicleToRespawn(cars);
}
else SetVehicleToRespawn(cars);
}
}
GameTextForAll("~b~~h~~h~~h~Vehicles respawned", 5000, 3);
new string[144];
format(string, sizeof(string), "You respawned all vehicles.", ReturnPlayerName(playerid), playerid);
SendClientMessageToAll(COLOR_HOT_PINK, string);
return 1;
}
Re: Respawn All Vehicles -
AdamsLT - 16.04.2019
Normally I ignore those code suggestions but I can't.
DON'T use that code, wtf. It's awful.
Quote:
Originally Posted by BsLOUAY
use this Code
PHP Code:
CMD:respawncars(playerid, params[])
{
//check if the player is a admin
LevelCheck(playerid, 4);
for(new cars; cars < MAX_VEHICLES; cars++)
{
LOOP_PLAYERS(i)
{
PlayerPlaySound(i, 1057, 0.0, 0.0, 0.0);
if(GetPlayerVehicleID(i) == cars)
{
if(GetPlayerState(i) == PLAYER_STATE_DRIVER)
{
if(GetPlayerGAdminLevel(playerid) < GetPlayerGAdminLevel(i))
{
SetVehicleToRespawn(cars);
}
}
else SetVehicleToRespawn(cars);
}
else SetVehicleToRespawn(cars);
}
}
GameTextForAll("~b~~h~~h~~h~Vehicles respawned", 5000, 3);
new string[144];
format(string, sizeof(string), "You respawned all vehicles.", ReturnPlayerName(playerid), playerid);
SendClientMessageToAll(COLOR_HOT_PINK, string);
return 1;
}
|
It depends on functions: LOOP_PLAYERS(), LevelCheck(), GetPlayerGAdminLevel(), ReturnPlayerName() which you probably don't have defined anywhere.
It doesn't use foreach so it loops through all possible vehicleids and, thus, is very inefficient.
Every single vehicleid iteration it loops through all players with LOOP_PLAYERS. If it's written in a similar manner, then I bet it doesn't use foreach as well, meaning you loop through way too many playerids.
Every single time you loop through a vehicleid you will play a sound which will just end up spamming players with useless noise for every vehicle.
Every time a player isn't in the vehicle that is currently being iterated - that vehicle will be respawned. Meaning that you will respawn the same vehicle multiple (possibly hundreds of) times before a single loop is even finished.
Then you will also respawn the current vehicle every time a player isn't a driver but is inside the vehicle for whatever reason.
And you will also respawn the vehicle if the player is inside of it, is the driver and is a higher admin level than the person that wrote the command? That just doesn't make sense at all.
You seriously need to rethink your code, BsLOUAY.
Vizi10, just redo your function with foreach (from YSI or whatever). It will work fine.
PHP Code:
RespawnAllVehicles()
{
foreach(new vehicleid : Vehicle)
{
if(IsVehicleEmpty(vehicleid))
{
SetVehicleToRespawn(vehicleid);
}
}
return 1;
}
Re: Respawn All Vehicles -
xRadical3 - 16.04.2019
Thank you AdamsLT, fixed with foreach.