SA-MP Forums Archive
How to check if there is a driver in the car or not? - 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: How to check if there is a driver in the car or not? (/showthread.php?tid=418143)



How to check if there is a driver in the car or not? - Tamer - 23.02.2013

pawn Код:
public OnPlayerDeath(playerid, killerid, reason)
{
    new playerState = GetPlayerState(killerid); // Get the killer's state
 
    if (playerState == PLAYER_STATE_PASSENGER ) // If the killer was in a vehicle,alright. What about and there were no drivers in it?
    {
// do the stuff.
    }
    return 1;
}
Read the if statement. How can I manage to do a thing like this?

So simply it will check if there is no players in the car of the killerid (a driver) if there is a driver it won't do anything.


Re: How to check if there is a driver in the car or not? - Misiur - 23.02.2013

1. Get the vehicle id
2. Loop through all players (except for killer), and check if they are inside this car, and their state is "teh driver"
3.
4. Profit


Re: How to check if there is a driver in the car or not? - Scenario - 23.02.2013

Something like this would work:

pawn Код:
stock DoesVehicleHaveDriver(vehicleid)
{
    #if defined foreach
    foreach(new i : Player)
    #else
    for(new i = 0; i < MAX_PLAYERS; i++)
    #endif 
    {
        if(GetPlayerVehicleID(i) == vehicleid && GetPlayerState(i) == PLAYER_STATE_DRIVER) return true;
    }
    return false;
}



Re: How to check if there is a driver in the car or not? - Tamer - 24.02.2013

pawn Код:
new playerState = GetPlayerState(killerid); // Get the killer's state
    if (playerState == PLAYER_STATE_PASSENGER ) // If the killer was in a vehicle,alright. What about and there were no drivers in it?
    {
    if(DoesVehicleHaveDriver(vehicleid)) return 0;
    }
pawn Код:
stock DoesVehicleHaveDriver(vehicleid)
{
    #if defined foreach
    foreach(new i : Player)
    #else
    for(new i = 0; i < MAX_PLAYERS; i++)
    #endif  
    {
        if(GetPlayerVehicleID(i) == vehicleid && GetPlayerState(i) == PLAYER_STATE_DRIVER) return true;
    }
    return false;
}
Код:
C:\Users\Tamer\Desktop\pwn\AVT scripting\gamemodes\GameMode.pwn(3588) : error 017: undefined symbol "vehicleid"
C:\Users\Tamer\Desktop\pwn\AVT scripting\gamemodes\GameMode.pwn(3617) : warning 217: loose indentation
C:\Users\Tamer\Desktop\pwn\AVT scripting\gamemodes\GameMode.pwn(3618) : warning 217: loose indentation
C:\Users\Tamer\Desktop\pwn\AVT scripting\gamemodes\GameMode.pwn(9245) : error 017: undefined symbol "foreach"
C:\Users\Tamer\Desktop\pwn\AVT scripting\gamemodes\GameMode.pwn(9245) : error 029: invalid expression, assumed zero
C:\Users\Tamer\Desktop\pwn\AVT scripting\gamemodes\GameMode.pwn(9245) : error 017: undefined symbol "i"
C:\Users\Tamer\Desktop\pwn\AVT scripting\gamemodes\GameMode.pwn(9245) : fatal error 107: too many error messages on one line

Compilation aborted.Pawn compiler 3.2.3664	 	 	Copyright © 1997-2006, ITB CompuPhase


5 Errors.



Re: How to check if there is a driver in the car or not? - Scenario - 24.02.2013

In your code, replace vehicleid with GetPlayerVehicleID(playerid)

In mine, remove the things with a # in front and then remove the foreach line.


Re: How to check if there is a driver in the car or not? - Tamer - 24.02.2013

Yes,I tried to do this on my own and worked,thanks for the help,no errors now. I need to test it thought.

Posting incase someone would like to learn how this works
pawn Код:
new playerState = GetPlayerState(killerid);
    if (playerState == PLAYER_STATE_PASSENGER )
    {
    new vehicleid = GetPlayerVehicleID(killerid);
    if(DoesVehicleHaveDriver(vehicleid)) return 0;
    }
pawn Код:
stock DoesVehicleHaveDriver(vehicleid)
{
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(GetPlayerVehicleID(i) == vehicleid && GetPlayerState(i) == PLAYER_STATE_DRIVER) return true;
    }
    return false;
}