Set Presence #

A basic presence allows a player to appear “online” or “away” to their friends.

When a user first logs on, no presence status exists. Upon logout, crash, or disconnect, Connect will automatically remove the presence status.

Set status as online #

To set a user’s presence status to “online”, use the Presence API SetAsOnline method:

This guide is based on an example Unreal project using the Third Person Unreal C++ project template. The example project is named ‘Narwhal’, update references accordingly.

Source\Narwhal\NarwhalPlayerController.h


// ...
class NARWHAL_API ANarwhalPlayerController : public APlayerController
{
    GENERATED_BODY()

public:
  // ...

	UFUNCTION(Exec)
	void SetOnline();

  // ...
};

Source\Narwhal\NarwhalPlayerController.cpp

void ANarwhalPlayerController::SetOnline() {
  Player->PresenceApi().SetAsOnline(FOnCompleteDelegate::CreateUObject(
      this, &ANarwhalPlayerController::OnFriendUpdate));
}

Set status as away #

To set a user’s presence to “away”, use the Presence API SetAsAway method:

Source\Narwhal\NarwhalPlayerController.h


// ...
class NARWHAL_API ANarwhalPlayerController : public APlayerController
{
    GENERATED_BODY()

public:
  // ...

	UFUNCTION(Exec)
	void SetAway();

  // ...
};

Source\Narwhal\NarwhalPlayerController.cpp

void ANarwhalPlayerController::SetAway() {
  Player->PresenceApi().SetAsAway(FOnCompleteDelegate::CreateUObject(
      this, &ANarwhalPlayerController::OnFriendUpdate));
}

View friends and their presence #

Use the client side cache to display friends and their current online status.

Source\Narwhal\NarwhalPlayerController.h


// ...
class NARWHAL_API ANarwhalPlayerController : public APlayerController
{
    GENERATED_BODY()

public:
  // ...

	UFUNCTION(Exec)
	void ViewFriends();

  // ...
};

Source\Narwhal\NarwhalPlayerController.cpp

void ANarwhalPlayerController::ViewFriends() {
  // use the friends cache to see friends current status
  if (const auto FriendsPtr = Player->FriendApi().GetFriends();
      FriendsPtr.IsValid()) {
    for (const TPair<FString, FPragmaFriend>& Friend : FriendsPtr.Get()->Array()) {
      const auto Presence = Friend.Value.Presence();
      FString Status = TEXT("Unknown");
      if (const UEnum *EnumPtr = StaticEnum<EBasicPresence>();
          EnumPtr && Presence.IsSet()) {
        Status = EnumPtr->GetNameStringByValue(
            static_cast<int64>(Presence->GetBasicPresence()));
      }

      UE_LOG(LogTemp, Display, TEXT("Friend %s is `%s`"),
             *Friend.Value.DisplayName().DisplayName, *Status);
    }
  }
}