Trailer Attached/Detached -
Mattaffix - 14.10.2016
I was wondering...which is the best way to detect when a player has detached or attached to a trailer?
Of course while a player is driving a truck.
Re: Trailer Attached/Detached -
TheDrx - 14.10.2016
if(IsTrailerAttachedToVehicle(vehicleid))
{
printf("Vehicle %i has a trailer!", vehicleid);
}
vehicleid = Truck ID
More informations:
https://sampwiki.blast.hk/wiki/IsTrailerAttachedToVehicle
Re: Trailer Attached/Detached -
Mencent - 14.10.2016
Hello.
So that you can get the ID of the trailer you can use GetVehicleTrailer().
https://sampwiki.blast.hk/wiki/GetVehicleTrailer
With TheDrx's code you can check wheather the vehicle has a trailer or not.
Re: Trailer Attached/Detached -
Mattaffix - 14.10.2016
Alright so I assume I have to use a timer in order to detect if (IsPlayerAttachedToVehicle(vehicleid)) right? But how exactly?
Re: Trailer Attached/Detached -
Mencent - 14.10.2016
It depends on what you want to do.
Can you explain us what do you want to do, please? Then we can tell you what it's the best solution.
Re: Trailer Attached/Detached -
Mattaffix - 14.10.2016
Sure.
I made a trucking delivery system. It works well,you deliver stuff from one point to another.
What I need to know is how should I detect when the trailer that the player is using to transport goods gets detached from the truck and also when the player manages to re-attach the trailer. Just like single-player when you get start the trucking missions and get notified when you lose your trailer or leave the vehicle.
Re: Trailer Attached/Detached -
Mencent - 14.10.2016
Ah, you want to check if the trailer is always detect when the player must be delivering something with this trailer?
Then you should use a timer which you start when the player start the delivering and stop when the mission is finish.
Notice: Use SetTimerEx because you should use the playerid.
Re: Trailer Attached/Detached -
Mattaffix - 14.10.2016
I don't know I was thinking of using a timer pretty much like OnPlayerUpdate and check if the player has the trailer attached to his truck. If the trailer is detached there should be like a function that prints the seconds the player has left to get the trailer back,hmmm? If someone can show me like an example,that would be really great and much appreciated!
Re: Trailer Attached/Detached -
Mencent - 14.10.2016
I advise you to use OnPlayerUpdate.
Try this:
PHP код:
//under the includes:
new playerToTrailer[MAX_PLAYERS],missionTimer[MAX_PLAYERS];
//When you start the mission (when you attached the trailer):
missionTimer[playerid] = SetTimerEx("OnPlayerDoMission",1000,1,"i",playerid);
playerToTrailer[playerid] = 10;//10 seconds to attach the trailer again
//the callback:
forward OnPlayerDoMission(playerid);
public OnPlayerDoMission(playerid)
{
new veh = GetPlayerVehicleID(playerid);
if(!IsTrailerAttachedToVehicle(veh))
{
playerToTrailer[playerid] --;
if(playerToTrailer[playerid] == 0)
{
KillTimer(missionTimer[playerid]);
SendClientMessage(playerid,-1,"Your mission is deleted! You lose the trailer");
return 1;
}
new string[145];
format(string,sizeof string,"You have only %d seconds to attach the trailer again!",playerToTrailer[playerid]);
SendClientMessage(playerid,-1,string);
return 1;
}
else
{
playerToTrailer[playerid] = 10;
}
return 1;
}
//when you finish the Mission correct:
KillTimer(missionTimer[playerid]);
Re: Trailer Attached/Detached -
Mattaffix - 14.10.2016
Your code seems okay,I've tested it and after few tweaks it seems that it's working exactly how I wanted.
Thanks for helping out!