how to respawn all cars
#1

Hi, I want to know what can I do so that all vehicles in my server can respawn to the position they were when the server started after 5 minutes but not if they are occupied. how can I do it, please help me.
Reply
#2

You mind like a /arespawncars?

I have the CMD in my GM.

I'll give you the CMD, not now because I'm on cellphone.
Reply
#3

Quote:
Originally Posted by PabloDiCostanzo
Посмотреть сообщение
You mind like a /arespawncars?

I have the CMD in my GM.

I'll give you the CMD, not now because I'm on cellphone.
Mmm yeah that will help too.

PD: I'm on my a cellphone too
Reply
#4

Quickly written, should do the job

pawn Код:
stock RespawnAllVehicles()
{
    for (new i=0;i<MAX_VEHICLES;i++)
    {
        if (CanVehicleBeRespawned(i)) // If this returns 1 (no factors preventing the vehicle from being respawned as stated in "CanVehicleBeRespawned"), the vehicle will respawn
        {
            SetVehicleToRespawn(i);
        }
    }
}

stock CanVehicleBeRespawned(vehicleid)
{
    new Float:X,Float:Y,Float:Z;
    GetVehiclePos(vehicleid, X,Y,Z);
    if (X == 0.0 && Y == 0.0 && Z == 0.0)
    {
        //Vehicle not created, returning 0
        return 0;
    }
   
    for (new i=0;i<MAX_PLAYERS;i++)
    {
        if (IsPlayerConnected(i))
        {
            if (GetPlayerVehicleID(i) == vehicleid) return 0; // Someone is inside the vehicle, returning 0
        }
    }
   
    return 1; // Vehicle is ready to be respawned, returning 1 :)
}
Now just use RespawnAllVehicles to respawn them to their original positions

EDIT: If you need a timer for it, here:

pawn Код:
forward RespawnVehicles();

public OnGameModeInit()
{
    SetTimer("RespawnVehicles", 300000, true);
}


public RespawnVehicles()
{
    RespawnAllVehicles();
    SendClientMessageToAll(-1, "All unoccupied vehicles successfully respawned");
}
Reply
#5

pawn Код:
stock IsVehicleOccupied(vehicleid)
{
    for(new i = 0; i < MAX_PLAYERS; i ++)
    {
        if(GetPlayerVehicleID(i) == vehicleid) return 1;
    }
    return 0;
}
pawn Код:
forward RespawnVehicles();
Under OnGameModeInit()
pawn Код:
SetTimer("RespawnVehicles",300000,1);
pawn Код:
public RespawnVehicles()
{
    for(new i = 0; i < MAX_VEHICLES; i++)
    {
        if(!IsVehicleOccupied(i))
        {
            SetVehicleToRespawn(i);
        }
    }
    SendClientMessageToAll(-1,"Vehicles respawned.");
    return 1;
}
Reply
#6

Your code will perform longer since you don't check the vehicle's existence and if the player is connected or not.
Reply
#7

Quote:
Originally Posted by tyler12
Посмотреть сообщение
pawn Код:
stock IsVehicleOccupied(vehicleid)
{
    for(new i = 0; i < MAX_PLAYERS; i ++)
    {
        if(GetPlayerVehicleID(i) == vehicleid) return 1;
    }
    return 0;
}
pawn Код:
forward RespawnVehicles();
Under OnGameModeInit()
pawn Код:
SetTimer("RespawnVehicles",300000,1);
pawn Код:
public RespawnVehicles()
{
    for(new i = 0; i < MAX_VEHICLES; i++)
    {
        if(!IsVehicleOccupied(i))
        {
            SetVehicleToRespawn(i);
        }
    }
    SendClientMessageToAll(-1,"Vehicles respawned.");
    return 1;
}
where do I add the 2 first codes and last one
Reply
#8

Quote:
Originally Posted by Jakku
Посмотреть сообщение
Quickly written, should do the job

pawn Код:
stock RespawnAllVehicles()
{
    for (new i=0;i<MAX_VEHICLES;i++)
    {
        if (CanVehicleBeRespawned(i)) // If this returns 1 (no factors preventing the vehicle from being respawned as stated in "CanVehicleBeRespawned"), the vehicle will respawn
        {
            SetVehicleToRespawn(i);
        }
    }
}

stock CanVehicleBeRespawned(vehicleid)
{
    new Float:X,Float:Y,Float:Z;
    GetVehiclePos(vehicleid, X,Y,Z);
    if (X == 0.0 && Y == 0.0 && Z == 0.0)
    {
        //Vehicle not created, returning 0
        return 0;
    }
   
    for (new i=0;i<MAX_PLAYERS;i++)
    {
        if (IsPlayerConnected(i))
        {
            if (GetPlayerVehicleID(i) == vehicleid) return 0; // Someone is inside the vehicle, returning 0
        }
    }
   
    return 1; // Vehicle is ready to be respawned, returning 1 :)
}
Now just use RespawnAllVehicles to respawn them to their original positions
its can be made much shorter:
pawn Код:
stock RespawnAllVehicles()
{
    for(new v = 1; v <= MAX_VEHICLES; v++)
        {
            for(new i = 0; i <= MAX_PLAYERS; i++;)  if(IsPlayerInVehicle(i, v)) continue;
            SetVehicleToRespawn(v);
        }
}
Reply
#9

Quote:
Originally Posted by Jakku
Посмотреть сообщение
Your code will perform longer since you don't check the vehicle's existence and if the player is connected or not.
It doesn't matter if it takes 2-3 seconds to do the job, what really matters is that it does it.
Reply
#10

Quote:
Originally Posted by Lauder
Посмотреть сообщение
It doesn't matter if it takes 2-3 seconds to do the job, what really matters is that it does it.
Are you claiming 2 or 3 seconds of wasted time doesn't matter?

EDIT: 30ms without checking the existence and 6ms with it which means it's 5x faster.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)