Skip to main content

Player Module

Session::Player() gives you the local player and, through events, every remote player your client knows about.

Getting players

std::optional< Player > GetLocalPlayer() const;
std::optional< Player > GetPlayerByID( Int32 id ) const;
std::optional< std::vector< Player > > GetAllPlayers() const;
  • GetLocalPlayer() returns the local player. Absent while the client is not connected to a server.
  • GetPlayerByID() resolves an entity ID back to a player. Absent while no such player is streamed in.
  • GetAllPlayers() returns every player currently streamed in to the client. Treat it as a one-time snapshot, for example, to seed your state right after your plugin loads. Do not poll it every frame; membership changes arrive through the streaming events below, and single players resolve through GetPlayerByID().

The Player handle

A TruckersMP::Player handle answers questions about one player. It stays meaningful while that player is streamed in for your client; afterwards every getter returns empty. See Data and Handles for the general rules.

Identity

GetterReturnsNotes
GetPlayerID()Int32Session-scoped entity ID.
GetAccountID()Uint64TruckersMP account ID. Stable across sessions.
GetSteamID()Uint64Steam account ID. Stable across sessions.
GetUsername()std::stringUTF-8 display name.

Presentation

GetterReturnsNotes
GetTagText()std::stringThe tag shown in front of the name.
GetTagColor()ColorThe tag's color.
IsPatron()BoolHas a Patreon tier that qualifies for in-game rewards.
IsGameModerator()BoolGame moderator.
IsTeamMember()BoolTruckersMP team member.
IsManager()BoolPart of TruckersMP management.

State

GetterReturnsNotes
GetNetworkLatency()Uint16Milliseconds between this player and the game server.
GetVehicle()VehicleThe vehicle the player is driving.
GetTrailer()TrailerThe trailer the player is towing.
GetDistanceFromLocalPlayer()FloatDistance between this player's vehicle and the local player's vehicle, in meters.
GetDistanceToCamera()FloatDistance between this player's vehicle and the client's active camera, in meters.
CanCollideWith( player )BoolWhether the collisions between the given player's and local player's vehicles are enabled
CanStreamFromCamera()BoolWhether this player's camera may drive streaming.
StreamsFromCamera()BoolWhether it currently does.

Camera streaming is a special right: normally the world streams in around a player's vehicle, so a free camera flying away sees an empty world. A player with this right may anchors streaming to the camera itself.

Events

OnStreamIn

A player entered the client's streaming range. This fires after the client has received a full update for them; the player is visible in the world and every getter works.

g_session->Player().OnStreamIn.Register( []( TruckersMP::PlayerStreamInEvent &e )
{
if( const std::optional< TruckersMP::Int32 > id = e.GetPlayer().GetPlayerID() )
{
AddToMyRadar( *id, player.GetUsername().value_or( "unknown" ) );
}
} );

OnStreamOut

The player left the client's streaming range. The plugin stops receiving updates for them and their handle goes stale. Use the event to clean up any states.

OnUpdate

The client received a network update for a player. This event fires at network rate, many times per second across all streamed-in players. Treat the handler as a data tap:

  • Read the values you need and store them in your own structures.
  • Return immediately.
  • Do not call back into the SDK from this handler, and keep logic out of it. Act on the collected data in your own update path.
static std::map< TruckersMP::Int32, Clock > g_lastSeen;

g_session->Player().OnUpdate.Register( []( TruckersMP::PlayerUpdateEvent &e )
{
if( const std::optional< TruckersMP::Int32 > id = e.GetPlayer().GetPlayerID() )
{
g_lastSeen[ *id ] = Clock::now(); // bookkeeping only
}
} );