SA-MP Forums Archive
AttachTrailerToVehicle problem - 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: AttachTrailerToVehicle problem (/showthread.php?tid=623729)



AttachTrailerToVehicle problem - AmigaBlizzard - 04.12.2016

Hi,

I don't know if some of you have tried this already but I found a problem when creating a trailer and immediately after it, attach it to my vehicle.

I've created a graphical menu (using selectable textdraws) to select a vehicle.
It also puts me inside the vehicle.
That's working fine.

Now, when I'm inside a vehicle that can pull a trailer, the menu shows me the trailer I can attach to my vehicle.
It spawns the trailer and attaches it to my vehicle, but it isn't attached.
I had to resort to a timer to attach the trailer to my vehicle after some time.

Instead of this (this fails to attach the trailer to my vehicle):
PHP код:
new trailerid CreateVehicle(modelxyzarandom(126), random(126), 300);
AttachTrailerToVehicle(traileridGetPlayerVehicleID(playerid)); 
I need to do this:
PHP код:
// Inside a function
new trailerid CreateVehicle(modelxyzarandom(126), random(126), 300);
SetTimerEx("Vehicle_AttachTimer"100false"ii"playeridtrailerid);
forward Vehicle_AttachTimer(playeridtrailerid);
public 
Vehicle_AttachTimer(playeridtrailerid)
{
    new 
vehicleid GetPlayerVehicleID(playerid);
    
SendClientMessage(playerid0xFFFFFFFF"Running attach timer");
    
// If the trailer has streamed in, attach it to the player's vehicle
    
if (IsVehicleStreamedIn(traileridplayerid))
        
AttachTrailerToVehicle(traileridvehicleid);
    else 
// The trailer hasn't streamed in yet, so run the timer again
        
SetTimerEx("Vehicle_AttachTimer"100false"ii"playeridtrailerid);

I find no other way to deal with this.

I guess using AttachTrailerToVehicle immediately after creating the trailer fails because the trailer hasn't streamed in yet and thus, it cannot be attached yet.

Is this the only way?


Re: AttachTrailerToVehicle problem - Threshold - 04.12.2016

OnVehicleStreamIn is one option.


Re: AttachTrailerToVehicle problem - AmigaBlizzard - 04.12.2016

I'd need to store the vehicleid somewhere for that callback to work, otherwise it won't know to which vehicle it needs to attach itself to.
But that could be considered to be a work-around as well.
And probably less stress for the server (less timers running all over the place).

Thank you.


Re: AttachTrailerToVehicle problem - Threshold - 04.12.2016

Well that's easily. You have a tying element for the callback and the value. 'playerid' and 'vehicleid'.

PHP код:
// Create new variable at top of script.
new TrailerStream[MAX_PLAYERS] = {INVALID_VEHICLE_ID, ...};
// Creating the trailer
TrailerStream[playerid] = CreateVehicle(modelxyzarandom(126), random(126), 300); 
PHP код:
public OnVehicleStreamIn(vehicleidforplayerid)
{
    if(
vehicleid == TrailerStream[forplayerid])
    {
        
// Check if player is in a valid vehicle?
        
AttachTrailerToVehicle(vehicleidGetPlayerVehicleID(forplayerid));
        
TrailerStream[forplayerid] = INVALID_VEHICLE_ID;
    }
    return 
1;

EDIT: You'll also need to account for the TrailerStream variable when the trailer is destroyed. Just in case.


Re: AttachTrailerToVehicle problem - AmigaBlizzard - 04.12.2016

I've already modified the system to use OnVehicleStreamIn and deleted the timer.

The vehicleid (to which the trailer must attach itself) is saved in a variable and when OnVehicleStreamIn kicks in, the variable is checked.
If it's 0, nothing happens (this means another vehicle has streamed in).
When it holds a vehicleid, the player is also checked (saved as well).
It might happen that a trailer is streamed in faster for another player near your location than for yourself.
So this is an additional check to be certain the vehicle streams in for you, not for another player.

When both are correct, the trailer is attached to the player's vehicle and both variables are cleared, preventing the same trailer to re-attach itself later on.

So both variables are only valid for a few hundred milliseconds (the time it takes between creating the trailer and actually streaming it in for the player).


Re: AttachTrailerToVehicle problem - Threshold - 04.12.2016

Well I wouldn't be worried about other players, because the other players wouldn't have the trailer ID assigned to their player ID in the first place. You don't need to over-complicate things, my example is probably as hard as it needs to be.


Re: AttachTrailerToVehicle problem - AmigaBlizzard - 04.12.2016

You were correct, again

I saved the vehicleid and the playerid to which the trailer should be attached.
I modified it to only save the playerid in the vehicle's enum.
The player's vehicleid, to which the trailer must be attached, can simply be retrieved using GetPlayerVehicleID in the callback.

The system now works perfectly without any timers.