Unreal: Presence #
This tutorial uses Unreal Engine 5.3 with Pragma Engine 0.4.0. to demonstrate integrating Pragma Engine presence functionality with a third-party game engine. This guide assumes you are proficient with Unreal Editor.
In this tutorial, we’ll expand MyPlayerController
to implement functionality related to the Pragma Engine Presence service. Specifically we will:
- Allow a player to appear “online” or “offline” (basic presence)
- Allow a player to appear “in shop” (rich presence)
- Display a player’s current presence values
- Handle backend presence changes
By the end of the tutorial, our player client and backend will be able to make and update basic and rich presence selections.
Update SDK #
Because there were changes to the ext protos, re-run the update script command from your Unreal Plugins folder:
update-pragma-sdk.sh
Set player presence to online or away #
Goal: Allow player to set their basic presence to “online” or “away”
Steps #
In
MyPlayerController.h
, declareSetPresenceToOnline()
andSetPresenceToAway()
:public: UFUNCTION(Exec, BlueprintCallable, Category="Pragma") void SetPresenceToOnline(); UFUNCTION(Exec, BlueprintCallable, Category="Pragma") void SetPresenceToAway();
In
MyPlayerController.cpp
, defineSetPresenceToOnline()
andSetPresenceToAway()
:void AMyPlayerController::SetPresenceToOnline() { Player->PresenceApi().SetAsOnline( UPragmaPresenceApi::FOnCompleteDelegate::CreateWeakLambda( this, [this](const TPragmaResult<>& Result) { if (Result.IsSuccessful()) { UE_LOG(LogTemp, Display, TEXT("MyGameClient -- Set presence to online succeeded.")); } else { UE_LOG(LogTemp, Display, TEXT("MyGameClient -- Set presence to online failed.")); } })); } void AMyPlayerController::SetPresenceToAway() { Player->PresenceApi().SetAsAway( UPragmaPresenceApi::FOnCompleteDelegate::CreateWeakLambda( this, [this](const TPragmaResult<>& Result) { if (Result.IsSuccessful()) { UE_LOG(LogTemp, Display, TEXT("MyGameClient -- Set presence to away succeeded.")); } else { UE_LOG(LogTemp, Display, TEXT("MyGameClient -- Set presence to away failed.")); } })); }
Our SetPresenceToOnline()
calls the PresenceApi’s SetAsOnline()
function, and our SetPresenceToAway()
function calls the PresenceApi’s SetAsAway()
function. The PresenceApi function facilitates setting the player’s basic presence to “Online” or “Away”.
Test #
To test this functionality using the Unreal in-game console, as a logged in player with social features initialized, issue SetAsOnline
or SetAsAway
.
If successful, you should see “Set presence to online succeeded.” or “Set presence to away succeeded.” in the Unreal output log.
To test this functionality using Unreal Blueprints, create a “Online” button that calls SetPresenceToOnline
, and a “Away” button that calls SetPresenceToAway
. As a logged in player with social features initialized, click each button.
If successful, you should see “Set presence to online succeeded.” or “Set presence to away succeeded.” in the Unreal output log along with any on-screen functionality you defined in the Blueprints (such as populating a “Basic presence” text box).
Set player presence to “in shop” #
Goal: Allow a player to set their rich presence to appear “in shop”.
Steps #
In
MyPlayerController.h
, declareSetInShop()
. Have it accept a boolean value that represents whether the player is currently in the shop:public: UFUNCTION(Exec, BlueprintCallable, Category="Pragma") void SetInShop(const bool IsInShop);
In
MyPlayerController.cpp
, defineSetInShop()
:void AMyPlayerController::SetInShop(const bool IsInShop) { FPragma_Presence_ExtRichPresencePlayerRequest Request; Request.Update.SetInShop(IsInShop); Player->PresenceApi().SetRichPresence( Request, UPragmaPresenceApi::FOnCompleteDelegate::CreateWeakLambda( this, [this](const TPragmaResult<> Result) { if (Result.IsSuccessful()) { UE_LOG(LogTemp, Display, TEXT("MyGameClient -- Set in shop success")); } else { UE_LOG(LogTemp, Error, TEXT("MyGameClient -- Unable to set rich presence: %s"), *Result.GetErrorAsString()); } })); }
Our SetInShop()
calls the PresenceApi’s SetRichPresence()
function, providing the ExtRichPresencePlayerRequest
value.
Test #
To test this functionality using the Unreal in-game console, as a logged in player with social features initialized, issue SetInShop true
.
If successful, you should see “Set in shop to true.” in the Unreal output log.
To test this functionality using Unreal Blueprints, create a “Enter shop” button that calls SetInShop
with the true
value, and a “Leave shop” button that calls SetInShop
with the false
value. As a logged in player with social features initialized, click each button.
If successful, you should see “Set in shop to true.” in the Unreal output log along with any on-screen functionality you defined in the Blueprints (such as populating a “Rich presence” text box).
Handle presence changes #
Goal: Alert player that their backend presence has changed.
Steps #
In
MyPlayerController.h
, declareHandleOnPresenceChanged()
:private: void HandleOnPresenceChanged(const FPragmaPresence& Presence);
In
MyPlayerController.cpp
, defineHandleOnPresenceChanged()
:void AMyPlayerController::HandleOnPresenceChanged(const FPragmaPresence& Presence) { UE_LOG(LogTemp, Display, TEXT("MyGameClient -- Presence status changed.")); }
In the
MyPlayerController.cpp
BeginPlay()
method, register ourHandleOnPresenceChanged()
method with the appropriate event handler:Player->PresenceApi().OnPresenceChanged.AddUObject( this, &AMyPlayerController::HandleOnPresenceChanged);
Test backend presence changes #
Because we modified the OnAddPlayer()
and OnRemovePlayer()
methods in Automatically set player to “in party” to update a player’s rich presence based on party state, we don’t need to do any additional work in the Unreal files.
To test the functionality defined in the plugin using Unreal, as a logged in player with social features initialized, create, join, or leave a party. You should see the message defined in your HandleOnPresenceChanged()
method in the Unreal output log.
Next steps #
Congratulations! You now have a custom Unreal implementation of the Pragma Engine Presence service. Continue to the Unreal: Friends tutorial to see how to apply the Friends services in Unreal.