SA-MP Forums Archive
Respawn all free trailers? - 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: Respawn all free trailers? (/showthread.php?tid=663142)



Respawn all free trailers? - rOps - 23.01.2019

Hello guys, I was searching for the answer of this question in older threads, but didn't found it.

My question is how to respawn all FREE trailers, I mean all trailers, which didn't attached to the vehicle.


Re: Respawn all free trailers? - rOps - 23.01.2019

HTML Code:
new trailerid = sizeof(TruckTrailer);

for(new vehicleid; vehicleid <= GetVehiclePoolSize(); vehicleid ++) 
{
     if(!IsTrailerAttachedToVehicle(vehicleid))
     {
          SetVehicleToRespawn(trailerid);
     }
}
Is this code correct? I don't think so.


Re: Respawn all free trailers? - rOps - 23.01.2019

But how to get a list of used trailers?


Re: Respawn all free trailers? - JaKe Elite - 23.01.2019

PHP Code:
for(new 0sizeof(TruckTrailer); i++)
{
    if(!
IsTrailerAttachedToVehicle(i))
    {
        
SetVehicleToRespawn(i);
    }

Not tested but I am 99% sure this would work


Re: Respawn all free trailers? - MEGADETHS - 24.01.2019

PHP Code:

new bool:TrailerIsFree[MAX_VEHICLES] = true;
public 
OnPlayerCommandText(playeridcmdtext[])
{
if(!
strcmp(cmdtext"/respawnfreetrailers"true))
{
new 
TrailerID = -1;
for(new 
i=0i<MAX_VEHICLES;i=i+1)
{
if(
IsTrailerAttachedToVehicle(i) == 1)
{
TrailerID GetVehicleTrailer(i);
TrailerIsFree[TrailerID] = false;
}
}
for(new 
i=0i<MAX_VEHICLES;i=i+1)
{
if(
TrailerIsFree[i] == true)
{
SetVehicleToRespawn(i);
}
}
}
return 
0;




Re: Respawn all free trailers? - Kaliber - 24.01.2019

Write it like this:

PHP Code:
new bool:trailer[(sizeof(TruckTrailer)) char];
for(new 
GetVehiclePoolSize(),iv!=0v--)
{
    if(
IsTrailerAttachedToVehicle(v) && (i=GetTrailerIndex(v)) != -1)
    {
        
trailer{i} = true;
    }
}
for(new 
ii<sizeof(TruckTrailer); i++)
{
    if(!
trailer{i}) SetVehicleToRespawn(TruckTrailer[i]);
}
stock GetTrailerIndex(v)
{
    for(new 
ii<sizeof(TruckTrailer); i++)
    {
        if(
TruckTrailer[i] == v) return i;
    }
    return -
1;




Re: Respawn all free trailers? - Calisthenics - 24.01.2019

Quote:
Originally Posted by Kaliber
View Post
PHP Code:
for(new ii<sizeof(TruckTrailer); i++) 
If TruckTrailer has size of 50 and there are 100 vehicles, half of them will be ignored.

https://sampwiki.blast.hk/wiki/OnTrailerUpdate

You can track when a trailer is attached/detached in this callback and store in in an array of MAX_VEHICLES. When you want to respawn trailers, you loop from 0 to sizeof(TruckTrailer)-1 and you check if the value of the array for the vehicleid is not attached and you respawn it.


Re: Respawn all free trailers? - Kaliber - 24.01.2019

Quote:
Originally Posted by Calisthenics
View Post
If TruckTrailer has size of 50 and there are 100 vehicles, half of them will be ignored.
Yes, you don't say.

But in another post he used that, so it seemed that all Trailer he wanna respawn are TruckTrailer.

Otherwise it would be complete non-sense from him, he used that variable.


Re: Respawn all free trailers? - MEGADETHS - 24.01.2019

Oh yeah, I forgot to check for the vehicle model, This one should definitely work but it would be better if you keept track of trailers towed with a vehicle variable that changes when a vehicle starts towing/untowing.

PHP Code:
new bool:TrailerIsFree[MAX_VEHICLES] = true;
public 
OnPlayerCommandText(playeridcmdtext[])
{
if(!
strcmp(cmdtext"/respawnfreetrailers"true))
{
new 
TrailerID = -1;
for(new 
i=0i<MAX_VEHICLES;i=i+1)
{
if(
IsTrailerAttachedToVehicle(i) == 1)
{
TrailerID GetVehicleTrailer(i);
TrailerIsFree[TrailerID] = false;
}
}
for(new 
i=0i<MAX_VEHICLES;i=i+1)
{
if(
TrailerIsFree[i] == true)
{
if(
GetVehicleModel(i) == 435 || GetVehicleModel(i) == 450 || GetVehicleModel(i) == 584 || GetVehicleModel(i) == 591)
{
SetVehicleToRespawn(i);
}
}
}
}
return 
0;