Skip to content

Commit 2a1f230

Browse files
authored
Merge pull request #1082 from Vince0789/PutPlayerInVehicle-example
PutPlayerInVehicle, updated example and warnings
2 parents d74397b + 1b162d3 commit 2a1f230

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

docs/scripting/functions/PutPlayerInVehicle.md

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,48 @@ Puts a player in a vehicle.
1818

1919
**true** - The function was executed successfully.
2020

21-
**false** - The function failed to execute. The player or vehicle don't exist.
21+
**false** - The function failed to execute. The player or vehicle doesn't exist.
2222

2323
## Examples
2424

2525
```c
26-
public OnPlayerEnterVehicle(playerid, vehicleid, ispassanger)
26+
// Global array to track which vehicle belongs to each player.
27+
// INVALID_VEHICLE_ID is used as a placeholder for players without a vehicle.
28+
static s_PlayerVehicle[MAX_PLAYERS] = { INVALID_VEHICLE_ID, ... };
29+
30+
public OnPlayerSpawn(playerid)
2731
{
28-
PutPlayerInVehicle(playerid, vehicleid, 0);
32+
// Check if the player already has a valid vehicle.
33+
if (!IsValidVehicle(s_PlayerVehicle[playerid]))
34+
{
35+
// If not, create a new vehicle for the player and store its ID.
36+
s_PlayerVehicle[playerid] = CreateVehicle(411, 0.0, 0.0, 3.5, 0.0, -1, -1, -1);
37+
}
38+
39+
// Mark that the player should be placed in their vehicle once it is fully loaded.
40+
// This avoids issues where the vehicle might not yet be loaded on the client's side.
41+
SetPVarInt(playerid, "PutPlayerInVehicle", 1);
42+
2943
return 1;
3044
}
45+
46+
public OnVehicleStreamIn(vehicleid, forplayerid)
47+
{
48+
// This callback is triggered when a vehicle streams in for the player (i.e. when it is loaded into memory).
49+
// Check if the streamed-in vehicle is the player's and if they need to be placed in it.
50+
if (vehicleid == s_PlayerVehicle[forplayerid] && GetPVarInt(forplayerid, "PutPlayerInVehicle"))
51+
{
52+
// Put the player into the vehicle.
53+
PutPlayerInVehicle(forplayerid, vehicleid, 0);
54+
55+
// Clear the marker to prevent repeatedly putting the player into the vehicle
56+
// (e.g., if the player leaves the vehicle and it streams in again later).
57+
DeletePVar(forplayerid, "PutPlayerInVehicle");
58+
}
59+
60+
return 1;
61+
}
62+
3163
```
3264
3365
| ID | Seat |
@@ -48,7 +80,9 @@ You can use [GetPlayerVehicleSeat](GetPlayerVehicleSeat) in a loop to check if a
4880
4981
:::warning
5082
51-
If the seat is invalid or is taken, will cause a crash when they EXIT the vehicle.
83+
* If the seat is invalid or already taken, the client will crash when they EXIT the vehicle.
84+
* Putting a player into a vehicle that is not streamed in can be unreliable. This is due to a potential client-side issue where the vehicle may not have fully loaded into memory yet.
85+
* This also applies when attempting to put a player into a vehicle that was just created.
5286
5387
:::
5488

0 commit comments

Comments
 (0)