Vehicle Seats! -
Juvanii - 03.08.2014
is there any method to count how many players are in my current vehicle and check what seats id's are they in?!!
Re: Vehicle Seats! -
McBan - 03.08.2014
Something Like:
PHP код:
OnPlayerEnterVehicle
{
CountPlayersInVehicle(vehicleid, blabla
}
And make a stock for CountPlayersInVehicle like:
PHP код:
forward CountPlayersInVehicle(vehicleid);
public CountPlayersInVehicle(vehicleid)
{
PlayerInVehicle++;
}
Or something like that above, Not entirely sure how it'd work but i'm sure it can be done if there isn't a function already.
Good Luck With It.
Re: Vehicle Seats! -
Threshold - 03.08.2014
Here's a short example that I just created:
pawn Код:
new veh = GetPlayerVehicleID(playerid), cnt = 0, Seat[10][2], i;
for(i = 0; i < MAX_PLAYERS; i++) //Start a loop through all players. (Foreach is the recommended approach)
{
if(!IsPlayerConnected(i)) continue; //This skips ids that aren't connected. Remove if using Foreach
if(!IsPlayerInVehicle(i, veh)) continue; //This skips ids that are not inside your vehicle.
new vseat = GetPlayerVehicleSeat(i); //Get the player's vehicle seat ID
Seat[cnt][0] = i; //Save the playerid to the first element.
Seat[cnt][1] = (-1 < vseat < sizeof(Seat)) ? (vseat) : (cnt + 1); //Save the player's seat to the second element
++cnt; //Add '1' to the seat count.
}
for(i = 0; i < cnt; i++) printf("ID %d is in Seat ID %d", Seat[i][0], Seat[i][1]);
This will print the seats that each player inside your vehicle is in. I must give credit to Jeff somewhat for a few parts of this code as he taught me a few things quite a while back.
cnt - The number of players inside your vehicle.
Seat[#][0] - The id of the #'th player inside your vehicle.
Seat[#][1] - The seat id of the #'th player inside your vehicle.
Re: Vehicle Seats! -
[XST]O_x - 03.08.2014
pawn Код:
stock CountPlayersInVehicle(vehicleid)
{
new res = 0;
foreach(new i: Player)
if(GetPlayerVehicleID(i) == vehicleid) res++;
return res;
}
Re: Vehicle Seats! -
Juvanii - 03.08.2014
Thank you all, but another question: can i check which seat id is UnOccupied (empty) ?
Re: Vehicle Seats! -
[XST]O_x - 03.08.2014
Quote:
Originally Posted by Juvanii
Thank you all, but another question: can i check which seat id is UnOccupied (empty) ?
|
pawn Код:
stock GetFreeSeat(vehicleid)
{
foreach(new i: Player) {
if(IsPlayerInAnyVehicle(i) && GetPlayerVehicleID(i) == vehicleid) {
for(new j = 0; j < 9; j++) {
if(GetPlayerVehicleSeat(i) == j) continue;
else return j;
}
return -1;
}
}
return 1;
}
Will only work for occupied vehicles though
Re: Vehicle Seats! -
Juvanii - 07.08.2014
Quote:
Originally Posted by [XST]O_x
Will only work for occupied vehicles though
|
What if 2 seats are empty? is it gonna choose one of them?
Re: Vehicle Seats! -
[XST]O_x - 07.08.2014
Quote:
Originally Posted by Juvanii
What if 2 seats are empty? is it gonna choose one of them?
|
The first one.
0 - the driver seat
1 - the seat next to the driver
2 - the left rear seat
3- the right rear seat
And so on
If 1 and 3 are both empty, it will choose 1 and not 3. If 0, 1 and 3 are empty, it will choose 0.
AW: Re: Vehicle Seats! -
Nero_3D - 08.08.2014
Quote:
Originally Posted by [XST]O_x
pawn Код:
stock GetFreeSeat(vehicleid) { foreach(new i: Player) { if(IsPlayerInAnyVehicle(i) && GetPlayerVehicleID(playerid) == vehicleid) { for(new j = 0; j < 9; j++) { if(GetPlayerVehicleSeat(i) == j) continue; else return j; } return -1; } } return 1; }
Will only work for occupied vehicles though
|
That code wont work at all, first you loop through the players than if you found a player inside the vehicle you get his seatid and if it isn't j (start at 0 goes till

the function returns j
If the seatid of the first person found is 0 than the function will return 1 and if the seatid isn't 0 than it will return 0
Re: AW: Re: Vehicle Seats! -
[XST]O_x - 08.08.2014
Quote:
Originally Posted by Nero_3D
That code wont work at all, first you loop through the players than if you found a player inside the vehicle you get his seatid and if it isn't j (start at 0 goes till  the function returns j
If the seatid of the first person found is 0 than the function will return 1 and if the seatid isn't 0 than it will return 0
|
I don't get what's wrong?
pawn Код:
if(GetPlayerVehicleSeat(i) == j) continue;
If the player's seat is j (0 to 8 ), meaning the seat is taken, continue to the next j. (0 to 1, 1 to 2...)
If the player's seat is not taken( the 'else' statement), meaning that this seat is NOT taken, return j (the ID of the empty seat).
If it looped through 0 to 8 and none were free, return -1.
Have you tried the code?
EDIT: Checked the code myself, works fine, I don't know what you're talking about. You can test it.