Towing vehicle help
#1

Can someone tell me why this tow system doesn't work properly? I mean, when i press "Y" the vehicle is attached, but when i press it again (it should de-attach it), doesn't work, the vehicle is still attached.

pawn Код:
if(newkeys == KEY_YES)
            {
            if  (IsTrailerAttachedToVehicle(GetPlayerVehicleID(playerid))) SendClientMessage(playerid, COLOR_GREY, "You are already towing a vehicle.");
            new Float:PPPX,Float:PPPY,Float:PPPZ;
            GetPlayerPos(playerid,PPPX,PPPY,PPPZ);
            new Float:VVVX,Float:VVVY,Float:VVVZ;
            new Found=0;
            new vid=0;
            while((vid<MAX_VEHICLES)&&(!Found))
            {
                vid++;
                GetVehiclePos(vid,VVVX,VVVY,VVVZ);
                if  ((floatabs(PPPX-VVVX)<7.0)&&(floatabs(PPPY-VVVY)<7.0)&&(floatabs(PPPZ-VVVZ)<7.0)&&(vid!=GetPlayerVehicleID(playerid)))
                {
                    Found=1;
                    AttachTrailerToVehicle(vid,GetPlayerVehicleID(playerid));
                    GameTextForPlayer(playerid, "~w~You are towing this vehicle!", 5000, 3);
                    TowingVehicle[playerid] = 1;
                }
            }
            if  (TowingVehicle[playerid] == 1)
            {
                    DetachTrailerFromVehicle(vid);
                    GameTextForPlayer(playerid, "~w~You stopped towing this vehicle!", 5000, 3);
                    TowingVehicle[playerid] = 0;
            }
            }
Reply
#2

Код:
if(newkeys == KEY_YES)
            {
            if  (IsTrailerAttachedToVehicle(GetPlayerVehicleID(playerid))) SendClientMessage(playerid, COLOR_GREY, "You are already towing a vehicle.");
            new Float:PPPX,Float:PPPY,Float:PPPZ;
            GetPlayerPos(playerid,PPPX,PPPY,PPPZ);
            new Float:VVVX,Float:VVVY,Float:VVVZ;
            new Found=0;
            new vid=0;
            while((vid<MAX_VEHICLES)&&(!Found))
            {
                vid++;
                GetVehiclePos(vid,VVVX,VVVY,VVVZ);
                if  ((floatabs(PPPX-VVVX)<7.0)&&(floatabs(PPPY-VVVY)<7.0)&&(floatabs(PPPZ-VVVZ)<7.0)&&(vid!=GetPlayerVehicleID(playerid)))
                {
                    Found=1;
                    AttachTrailerToVehicle(vid,GetPlayerVehicleID(playerid));
                    GameTextForPlayer(playerid, "~w~You are towing this vehicle!", 5000, 3);
                    TowingVehicle[playerid] = 1;
                }
            }
           else if  (TowingVehicle[playerid] == 1)
            {
                    DetachTrailerFromVehicle(vid);
                    GameTextForPlayer(playerid, "~w~You stopped towing this vehicle!", 5000, 3);
                    TowingVehicle[playerid] = 0;
            }
            }
I think you need else if. Try that if it dont work let me know.
Reply
#3

Your code gives me errors, so i tried in this way:

pawn Код:
if(newkeys == KEY_YES)
            {
            if  (IsTrailerAttachedToVehicle(GetPlayerVehicleID(playerid))) SendClientMessage(playerid, COLOR_GREY, "You are already towing a vehicle.");
            new Float:PPPX,Float:PPPY,Float:PPPZ;
            GetPlayerPos(playerid,PPPX,PPPY,PPPZ);
            new Float:VVVX,Float:VVVY,Float:VVVZ;
            new Found=0;
            new vid=0;
            while((vid<MAX_VEHICLES)&&(!Found))
            {
                vid++;
                GetVehiclePos(vid,VVVX,VVVY,VVVZ);
                if  ((floatabs(PPPX-VVVX)<7.0)&&(floatabs(PPPY-VVVY)<7.0)&&(floatabs(PPPZ-VVVZ)<7.0)&&(vid!=GetPlayerVehicleID(playerid)))
                {
                    Found=1;
                    AttachTrailerToVehicle(vid,GetPlayerVehicleID(playerid));
                    GameTextForPlayer(playerid, "~w~You are towing this vehicle!", 5000, 3);
                    TowingVehicle[playerid] = 1;
                }
                else if  (TowingVehicle[playerid] == 1)
            {
                    DetachTrailerFromVehicle(vid);
                    GameTextForPlayer(playerid, "~w~You stopped towing this vehicle!", 5000, 3);
                    TowingVehicle[playerid] = 0;
            }
            }
            }
I attach a vehicle for the first time, all working, i press "Y" again and i get "you are already towing this vehicle" and the vehicle doesn't detach.
Reply
#4

Detect if the old key is also Y.

pawn Код:
else if(newkeys & KEY_YES && oldkeys & KEY_YES && TowingVehicle[playerid] == 1)
{
        DetachTrailerFromVehicle(vid);
        GameTextForPlayer(playerid, "~w~You stopped towing this vehicle!", 5000, 3);
        TowingVehicle[playerid] = 0;
}
Reply
#5

Same problem.
Reply
#6

Quote:
Originally Posted by iZN
Посмотреть сообщение
Detect if the old key is also Y.

pawn Код:
else if(newkeys & KEY_YES && oldkeys & KEY_YES && TowingVehicle[playerid] == 1)
That will never work because newkeys is never the same as oldkeys. Pressing a key twice in succession produces four different calls to OnPlayerKeyStateChange;
Код:
oldkeys = 0, newkeys = KEY_YES // Pressed KEY_YES
oldkeys = KEY_YES, newkeys = 0 // Released KEY_YES
oldkeys = 0, newkeys = KEY_YES // Pressed KEY_YES again
oldkeys = KEY_YES, newkeys = 0 // Released KEY_YES again
The real problem is that the attaching part of the code is unconditional; meaning that a trailer will get attached regardless. Basically, you want to detach an attached trailer before even checking anything else.

pawn Код:
if(newkeys & KEY_YES)
{
    new vehicleid = GetPlayerVehicleID(playerid);
    if(vehicleid && IsTrailerAttachedToVehicle(vehicleid))
    {
        DetachTrailerFromVehicle(vehicleid);
        return 1; // Quit here!
    }

    // Attaching part goes here
    return 1;
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)