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.

About the Unreal Tutorials

The code presented in this tutorial is simplified to give you an introduction to the game flow. An actual game would have a more sophisticated design, and the player experience may differ significantly.

The functions in this tutorial are built as UFunctions with the Exec and BlueprintCallable specifiers, meaning they can be executed by the in-game console and in a Blueprint or Level Blueprint graph. The Exec specifier is useful for quickly testing your functions.

The example tests in each section are meant to ensure your C++ code is working properly and are unlikely to represent a completed game design. Adapt the organization and naming to suit your project’s needs.

Note that you may need to update your #include statements as you progress through the tutorial.

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 #

  1. In MyPlayerController.h, declare SetPresenceToOnline() and SetPresenceToAway():

    public:
    	UFUNCTION(Exec, BlueprintCallable, Category="Pragma")
    	void SetPresenceToOnline();
    
    	UFUNCTION(Exec, BlueprintCallable, Category="Pragma")
    	void SetPresenceToAway();
    
  2. In MyPlayerController.cpp, define SetPresenceToOnline() and SetPresenceToAway():

    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 #

  1. In MyPlayerController.h, declare SetInShop(). 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);
    
  2. In MyPlayerController.cpp, define SetInShop():

    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 #

  1. In MyPlayerController.h, declare HandleOnPresenceChanged():

    private:
    	void HandleOnPresenceChanged(const FPragmaPresence& Presence);
    
  2. In MyPlayerController.cpp, define HandleOnPresenceChanged():

    void AMyPlayerController::HandleOnPresenceChanged(const FPragmaPresence& Presence)
    {
    	UE_LOG(LogTemp, Display, TEXT("MyGameClient -- Presence status changed."));
    }
    
  3. In the MyPlayerController.cpp BeginPlay() method, register our HandleOnPresenceChanged() 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.