AttachTrailerToVehicle problem
#1

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?
Reply
#2

OnVehicleStreamIn is one option.
Reply
#3

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.
Reply
#4

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.
Reply
#5

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).
Reply
#6

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.
Reply
#7

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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)