Skip to main content

Vehicles and Trailers

Session::Vehicle() and Session::Trailer() cover the physical entities in the world. They share one design: spawn and despawn events on the module, physics getters on the handle.

Getting handles

Vehicle and trailer handles come from three places:

  • A player: player.GetVehicle() and player.GetTrailer().
  • A vehicle: vehicle.GetTrailer() returns the attached trailer.
  • The spawn events below.
const TruckersMP::Player player = g_session->Player().GetLocalPlayer().value_or( {} );
const TruckersMP::Vehicle vehicle = player.GetVehicle().value_or( {} );

// An invalid handle answers every getter with an empty optional,
// so the chain needs no checks; the defaults fall through.
const TruckersMP::Placement placement = vehicle.GetPlacement().value_or( {} );
const TruckersMP::Float3 speed = vehicle.GetLinearVelocity().value_or( {} );

The Vehicle handle

GetterReturnsNotes
GetPlacement()PlacementPosition (Double3) and rotation (Quaternion) in world space.
GetLinearVelocity()Float3Meters per second, world space.
GetAngularVelocity()Float3Radians per second, world space.
GetBoundingBox()BoundsModel bounding box in vehicle space, meters.
GetTrailer()TrailerThe attached trailer, if any.

The Trailer handle

GetterReturnsNotes
Exists()BoolWhether the trailer is currently present in the world.
GetPlacement()PlacementPosition and rotation in world space.
GetLinearVelocity()Float3Meters per second, world space.
GetAngularVelocity()Float3Radians per second, world space.
GetBoundingBox()BoundsModel bounding box in trailer space, meters.

Handles stay meaningful while the entity is present in the game world; after despawn, every getter returns empty.

warning

The trailer handle is always present, even if the player is not towing any. Use the Exists() getter to check if the trailer actually exists.

Events

Both modules raise the same pair of lifecycle events, each carrying the owning player and the entity:

g_session->Vehicle().OnSpawned.Register( []( TruckersMP::VehicleSpawnedEvent &e )
{
TruckersMP::Player owner = e.GetPlayer();
TruckersMP::Vehicle vehicle = e.GetVehicle();
// Track it, tag it, measure it.
} );

g_session->Vehicle().OnDespawned.Register( []( TruckersMP::VehicleDespawnedEvent &e )
{
// Clean up anything keyed on this vehicle.
} );

Trailer() offers OnSpawned and OnDespawned with the same shape, carrying the player and the trailer.