Unity: Parties #

This tutorial uses Unity 2021.3 with Pragma Engine 0.4.0 to demonstrate integrating Pragma Engine party functionality with a third-party game engine. This guide assumes you are proficient with Unity.

After creating a Party Plugin in Pragma Engine, you can build the corresponding frontend in Unity with help from the Pragma Party API.

In this section, we’ll expand the PragmaManager.cs file we created in the Create Unity Project Files Unity tutorial to implement party functionality. By the end of the tutorial, our game client will be able to create, join, and leave parties, as well as make game mode and character selections.

There are many ways to apply the Pragma logic to Unity UI elements. This tutorial provides a quick way to link UI elements such as buttons and input fields with the Pragma SDK and backend.

To test the functionality in this topic, you’ll need to build your Unity project and launch at least two executables.

Update SDK #

Because there were changes to the ext protos, re-run the update script command from your project’s root directory:

update-pragma-sdk.sh

Create the party #

Goal #

Allow a player to create a party from a Unity project.

Upon party creation, parties are automatically assigned a unique party ID and invite code. In our example, we have the CreateParty() function return the party ID and invite code for easy access.

Steps #

  1. In PragmaManager.cs, add a CreateParty() method:

    public void CreateParty()
    {
        Player.PartyApi.CreateParty(new ExtCreateRequest(), new ExtPlayerJoinRequest(), 
            result =>
            {
                if (result.IsFailure)
                {
                    Debug.Log("Pragma -- Create party failed");
                }
                else 
                {
                    Debug.Log("Pragma -- Party created");
                    Debug.Log("Pragma -- Party ID: " 
                        + Player.PartyApi.GetPartyCache().GetParty().Id);
                    Debug.Log("Pragma -- Party invite code: " 
                        + Player.PartyApi.GetPartyCache().GetParty().InviteCode);
                }
            }
        );   
    }
    

    Our CreateParty() method calls the PartyApi’s CreateParty() SDK method, which handles creating a party on the backend. Our example does not accept any extra data for party creation, but you can add ExtCreateRequest and ExtPlayerJoinRequest parameters to allow for party customization during the time of creation.

    Our function prints out the party’s invite code, which is set on the party and can be distributed to allow others to join the party (see Join party with invite code).

  2. In the Unity editor, create a UI button called Create Party and hook it up to the PragmaManager CreateParty() method when clicked.

Test #

To test this functionality in the Unity editor, log in using test01 and click the Create Party button.

If successful, you’ll see “Pragma – Party created”, as well as the party ID and invite code, in the Unity console.

Join party with invite code #

Goal #

Allow a player to join a party using the party’s public invite code.

Upon party creation, parties are automatically assigned a unique invite code. To access this value, you can have the CreateParty() function return the party invite code and store it locally, or access the code with a call to Player.PartyApi.GetPartyCache().GetParty().InviteCode.

Steps #

  1. In PragmaManager.cs, above the Awake() method, declare an InputInviteCode input field. This is where users will enter the invite code for the party they want to join. We’ll connect this to a Unity UI element in a later step.

    public InputField InputInviteCode;
    
  2. In PragmaManager.cs, define the JoinPartyWithInviteCode() method using the Pragma SDK JoinPartyWithInviteCode() method and text from the InputInviteCode field:

    public void JoinPartyWithInviteCode()
    {
       string partyInviteCode = InputInviteCode.text;
       ExtPlayerJoinRequest playerJoinRequest = new ExtPlayerJoinRequest ();
    
       Player.PartyApi.JoinPartyWithInviteCode(playerJoinRequest, partyInviteCode, result =>
        {
            if (result.IsSuccessful)
            {
                Debug.Log("Pragma -- Party joined with invite code: " + partyInviteCode);
            }
            else 
            {
                Debug.Log("Pragma -- Could not join party");
            }
        });
    }
    
  3. In yor Unity editor:

    a. Create an input field where players can enter the invite code for the party they want to join, and connect it to the Pragma Manager InputInviteCode:

    Connect InputInviteCode in Unity

    b. Create a Join by invite code button and connect it to the Pragma Manager JoinPartyWithInviteCode() when clicked:

    Connect JoinParty method in Unity

Send party invite #

Goal #

Allow a player to send a party invite to another player using the invitee’s Pragma ID.

Steps #

  1. In PragmaManager.cs, above the Awake() method, declare a PartyInviteeIdInput input field. This is where users will enter the invitee’s Pragma ID. We’ll connect this to a Unity UI element in a later step.

    public InputField PartyInviteeIdInput;
    
  2. In PragmaManager.cs, define the SendPartyInvite() method using the Pragma SDK SendPartyInvite() method and the PartyInviteeIdInput input:

    public void SendPartyInvite()
    {
       PragmaId inviteePragmaId = new PragmaId(PartyInviteeIdInput.text);
    
        Player.PartyApi.SendPartyInvite(inviteePragmaId, result =>
        {
            if (result.IsSuccessful)
            {
                Debug.Log("Invite sent");
            }
            else
            {
                Debug.Log("Could not send invite");
            }
        });
    
    }
    
  3. In yor Unity editor:

    a. Create an input field where players can enter an invitee’s Pragma ID, and connect it with the Pragma Manager PartyInviteeIdInput.

    b. Create a Send party invite button and connect it to the Pragma Manager SendPartyInvite() method when clicked.

Accept or decline a party invite #

Goal #

Allow players to accept or decline a received party invite.

Party invites are identified by the inviter’s PragmaId. Invitees respond to party invites by providing the inviter’s Pragma Id to the AcceptPartyInvite() and DeclinePartyInvite() SDK methods.

Steps #

  1. In PragmaManager.cs, above the Awake() method, declare a PartyInviteId input field. This is where the player will enter the invite ID they want to respond to. We’ll connect this to a Unity UI element in a later step.

    public InputField PartyInviteId;
    
  2. In PragmaManager.cs, define an AcceptPartyInvite() and DeclinePartyInvite() method using the Pragma SDK AcceptPartyInvite() and DeclinePartyInvite() methods with the PartyInviteId input:

    public void AcceptPartyInvite()
    {
        PragmaId pragmaInviteId = new PragmaId(partyInviteId.text); 
        ExtPlayerJoinRequest playerJoinRequest = new ExtPlayerJoinRequest ();
    
        Player.PartyApi.AcceptPartyInvite(pragmaInviteId, playerJoinRequest, result =>
        {
            if (result.IsSuccessful)
            {
                Debug.Log("Invite accepted");
            }
            else
            {
                Debug.Log("Could not be accepted");
            }
        });
    }
    
    public void DeclinePartyInvite()
    {
        PragmaId pragmaInviteId = new PragmaId(partyInviteId.text); 
    
        Player.PartyApi.DeclinePartyInvite(pragmaInviteId, result =>
        {
            if (result.IsSuccessful)
            {
                Debug.Log("Invite declined");
            }
            else
            {
                Debug.Log("Could not be declined");
            }
        });
    }
    
  3. In yor Unity editor:

    a. Create an input field called PartyInviteId and link it to the Pragma Manager PartyInviteId.

    b. Create an Accept invite button and connect it to the Pragma Manager AcceptInvite() method when clicked.

    c. Create a Decline invite button and connect it to the Pragma Manager DeclineInvite() method when clicked.

Make game mode selections #

Goal #

Allow party leaders to select the mode for their game. Game mode options are based on the values defined in the Set Up Parties with Pragma Engine tutorial.

Steps #

  1. In PragmaManager.cs, define a SetGameModeCasual() and SetGameModeRanked() method:

    public void SetGameModeCasual()
    {
        ExtUpdatePartyRequest updatePartyRequest = new ExtUpdatePartyRequest
        {
            NewGameMode = GameMode.Casual
        };
    
        Player.PartyApi.UpdateParty(updatePartyRequest, result =>
        {
            if (result.IsSuccessful)
            {
                Debug.Log("Pragma -- Game mode set" + updatePartyRequest);
            }
            else
            {
                Debug.Log("Pragma -- Could not set game mode");
            }
        });
    }
    
    public void SetGameModeRanked()
    {
        ExtUpdatePartyRequest updatePartyRequest = new ExtUpdatePartyRequest
        {
            NewGameMode = GameMode.Ranked
        };
    
        Player.PartyApi.UpdateParty(updatePartyRequest, result =>
        {
            if (result.IsSuccessful)
            {
                Debug.Log("Pragma -- Game mode set" + updatePartyRequest);
            }
            else
            {
                Debug.Log("Pragma -- Could not set game mode");
            }
        });
    }
    

    Method details

    Our SetGameMode() methods update the ExtUpdatePartyRequest proto with a new game mode selection based on the button clicked. We then use the UpdateParty() SDK method with the updated ExtUpdatePartyRequest to update the party.

  2. In your Unity editor, create buttons for each game mode option (CASUAL or RANKED), and link them to the appropriate Pragma Manager method.

Make character selections #

Goal #

Allow players to select a character. Character options are based on the values defined in the Set Up Parties with Pragma Engine tutorial.

Steps #

  1. In PragmaManager.cs, define a SetCharacterKnight() and SetCharacterMage() and SetCharacterRouge() method:

    public void SetCharacterKnight()
    {
        ExtUpdatePartyPlayerRequest updatePartyPlayerRequest = new ExtUpdatePartyPlayerRequest
        {
            NewCharacter = Character.Knight
        };
    
        Player.PartyApi.UpdatePartyPlayer(updatePartyPlayerRequest, result =>
        {
            if (result.IsSuccessful)
            {
                Debug.Log("Pragma -- Character set" + updatePartyPlayerRequest);
            }
            else
            {
                Debug.Log("Pragma -- Could not set character");
            }
        });
    }
    
    public void SetCharacterMage()
    {
        ExtUpdatePartyPlayerRequest updatePartyPlayerRequest = new ExtUpdatePartyPlayerRequest
        {
            NewCharacter = Character.Mage
        };
    
        Player.PartyApi.UpdatePartyPlayer(updatePartyPlayerRequest, result =>
        {
            if (result.IsSuccessful)
            {
                Debug.Log("Pragma -- Character set" + updatePartyPlayerRequest);
            }
            else
            {
                Debug.Log("Pragma -- Could not set character");
            }
        });
    }
    
    public void SetCharacterRogue()
    {
        ExtUpdatePartyPlayerRequest updatePartyPlayerRequest = new ExtUpdatePartyPlayerRequest
        {
            NewCharacter = Character.Rogue
        };
    
        Player.PartyApi.UpdatePartyPlayer(updatePartyPlayerRequest, result =>
        {
            if (result.IsSuccessful)
            {
                Debug.Log("Pragma -- Character set" + updatePartyPlayerRequest);
            }
            else
            {
                Debug.Log("Pragma -- Could not set character");
            }
        });
    }
    

    Method details

    Our SetCharacter() methods update the ExtUpdatePartyPlayerRequest proto with a new character selection based on the button clicked. We then use the UpdatePartyPlayer() SDK method with the updated ExtUpdatePartyPlayerRequest to update the party player.

  2. In your Unity editor, create buttons for each character options (KNIGHT, MAGE, or ROGUE), and link them to the appropriate Pragma Manager method.

Leave party #

Goal #

Allow players to leave a party.

Steps #

  1. In PragmaManager.cs, define a LeaveParty() that uses the LeaveParty() SDK method:

    public void LeaveParty()
    {
        Player.PartyApi.LeaveParty(response =>
        {
            if (response.IsSuccessful)
            {
                Debug.Log("Pragma -- Left party");
            }
            else
            {
                Debug.Log("Pragma -- Could not leave party ");
            }
        });
    }
    
  2. In the Unity editor, create a Leave party button and hook it up to the PragmaManager LeaveParty() method when clicked.