メインコンテンツまでスキップ

Add party logic

Last updated on October 23, 2024

This article walks you though how to add party functionalities to your game.

Add basic party functionalities

In the PartyHandler.cs, you can add these party functionalities for your players to use.

Create a party

This function will return a result from the class type PartyInfo which contains Party data such as partyID, the leader's leaderID, a list of the party members' userIDs, a list of the invitees' userIDs, and the party invitation's invitationToken.

```csharp
public void CreateParty()
{
ResultCallback<PartyInfo> createPartyCallback = result =>
{
// If there is an error, display the error information
if (result.IsError)
{
Debug.Log($"Failed to create party: error code: {result.Error.Code} message: {result.Error.Message}");
}
else
{
Debug.Log("Successfully create a party");
}
};
AccelByteSDK.GetClientRegistry().GetApi().GetLobby().CreateParty(createPartyCallback);
}
```

If you want to save the `PartyInfo` of the created party, you can set the value based on `result.Value`.

```csharp
if (result.IsError)
...
else
{
...
partyInfo = result.Value;
}
```

Invite friend to party

This function requires the UserID of the player you want to invite in order to send an invitation, so add inviteeUserId as the function's parameter.

    public void InviteToParty(string inviteeUserId)
{
ResultCallback inviteToPartyCallback = result =>
{
// If there is an error, display the error information
if (result.IsError)
{
Debug.Log($"Failed to invite user to party: error code: {result.Error.Code} message: {result.Error.Message}");
}
else
{
Debug.Log("Successfully invite an invitee");
}
};
AccelByteSDK.GetClientRegistry().GetApi().GetLobby().InviteToParty(inviteeUserId, inviteToPartyCallback);
}

Kick friend from party

This function requires the UserID of the party member you want to kick from the party, so add memberUserId as the function's parameter.

```csharp
public void KickParty(string memberUserId)
{
ResultCallback kickPartyMemberCallback = result =>
{
// If there is an error, display the error information
if (result.IsError)
{
Debug.Log($"Failed to kick user from party: error code: {result.Error.Code} message: {result.Error.Message}");
}
else
{
Debug.Log($"Successfully kick member {memberUserId} from party");
}
};
AccelByteSDK.GetClientRegistry().GetApi().GetLobby().KickPartyMember(memberUserId, kickPartyMemberCallback);
}
```

Leave party

Use this function to leave a party.

```csharp
public void LeaveParty()
{
ResultCallback leavePartyCallback = result =>
{
// If there is an error, display the error information
if (result.IsError)
{
Debug.Log($"Failed to leave party: error code: {result.Error.Code} message: {result.Error.Message}");
}
else
{
Debug.Log("Successfully leave from party");
}
};
AccelByteSDK.GetClientRegistry().GetApi().GetLobby().LeaveParty(leavePartyCallback);
}
```

Promote friend to party leader

This function requires the UserID of the party member you want to promote to party leader, so add memberUserId as the function's parameter.

```csharp
public void PromotePartyLeader(string memberUserId)
{
ResultCallback<PartyPromoteLeaderResponse> promotePartyLeaderCallback = result =>
{
// If there is an error, display the error information
if (result.IsError)
{
Debug.Log($"Failed to promote member to be a party leader: error code: {result.Error.Code} message: {result.Error.Message}");
}
else
{
Debug.Log("Successfully promote member to be a party leader");
}
};
AccelByteSDK.GetClientRegistry().GetApi().GetLobby().PromotePartyLeader(memberUserId, promotePartyLeaderCallback);
}
```

Manage party invitation events

Most of the party invitation's UI will be a spawnable prefab. Create a new script called PartyInvitationPanel.cs and add the following to the top:

```csharp
using UnityEngine;
using AccelByte.Core;
using AccelByte.Models;
```

This script will hold functions that handle the Party Invitation's related events. You can attach this in your **Party Invitation** prefab later on.

Add some functions to handle party invitations in PartyInvitationPanel.cs:

Accept party invitation (Join party)

This function requires the UserID of the player you want to invite in order to send an invitation to that player, so add inviteeUserId as the function's parameter.

    public void JoinParty(PartyInvitation partyInvitation)
{
ResultCallback<PartyInfo> joinPartyCallback = result =>
{
// If there is an error, display the error information
if (result.IsError)
{
Debug.Log($"Failed to join party: error code: {result.Error.Code} message: {result.Error.Message}");
}
else
{
Debug.Log("Successfully join the party");
}
};
AccelByteSDK.GetClientRegistry().GetApi().GetLobby().JoinParty(partyInvitation.partyID, partyInvitation.invitationToken, joinPartyCallback);
}

Reject party invitation

This function requires the UserID of the party member you want to kick from the party, so add memberUserId as the function's parameter.

```csharp
public void RejectPartyInvitation(PartyInvitation partyInvitation)
{
ResultCallback<PartyRejectResponse> rejectPartyCallback = result =>
{
// If there is an error, display the error information
if (result.IsError)
{
Debug.Log($"Failed to reject party invitation: error code: {result.Error.Code} message: {result.Error.Message}");
}
else
{
Debug.Log("Successfully reject the party invitation");
}
};
AccelByteSDK.GetClientRegistry().GetApi().GetLobby().RejectPartyInvitation(partyInvitation.partyID, partyInvitation.invitationToken, rejectPartyCallback);
}
```

You can use the result value from the `AccelByteSDK.GetClientRegistry().GetApi().GetLobby().InvitedToParty` event for this **PartyInvitation** value.

Handle party notifications

To send a notification to a player on any activity related to the current party, find LobbyHandler.cs and add this under the ConnectToLobby() function. For now, we will just add a Debug.Log for each event.

```csharp
lobby.InvitedToParty += result => { Debug.Log($"Invited by: {result.Value.from}"); };
lobby.JoinedParty += result => { Debug.Log("Invitee join a party"); };
lobby.KickedFromParty += result => { Debug.Log("You're kicked from party"); };
lobby.LeaveFromParty += result => { Debug.Log($"{result.Value.userID} leave the party"); };
lobby.RejectedPartyInvitation += result => { Debug.Log("Invitee rejected a party invitation"); };
```

Retrieve party data

You can retrieve party data in the following methods:

  • Retrieve party data every time the data is updated, use this event:

    AccelByteSDK.GetClientRegistry().GetApi().GetLobby().PartyDataUpdateNotif += result => {}

    This event will return a result with the class type PartyDataUpdateNotif which contains Party data such as PartyInfo. PartyDataUpdateNotif does not have an invitationToken, but does have updatedAt which indicates when the Party data is updated, and custom_attribute which is a Dictionary that you can use with any custom info.

    As with the last step, you can find this event in LobbyHandler.cs under the ConnectToLobby() function. In this case, since we only need to display Party data, just add a Debug.Log to notify the party member's UserID via the Console output.

    lobby.PartyDataUpdateNotif += result =>{ Debug.Log($"Current Party Members: {result.Value.members}");}
  • Retrieve Party data using the GetPartyInfo() function. The PartyInfo class contains Party data such as the partyID, leaderID, members (UserID), invitees (UserID), and an invitationToken. To get this PartyInfo, use the following function:

    ResultCallback<PartyInfo> getPartyInfoCallback = result =>
    {
    // If there is an error, display the error information
    if (result.IsError)
    {
    Debug.Log($"Failed to leave party: error code: {result.Error.Code} message: {result.Error.Message}");
    }
    else
    {
    Debug.Log("Successfully left party");
    }
    };
    AccelByteSDK.GetClientRegistry().GetApi().GetLobby().GetPartyInfo(getPartyInfoCallback);

    This function returns a result with the class type PartyInfo, and you can access its values with result.Value.<the object you need>. For example, if you want to retrieve the partyID, use result.Value.partyId.

    :::tip You can only retrieve UserID from PartyDataUpdateNotif and PartyInfo, so you will need to use the GetUser() function to retrieve user data. :::

    AccelByteSDK.GetClientRegistry().GetApi().GetUser().BulkGetUserInfo(usersIdList, result => {
    // Loop the result's Users Data, return in BaseUserInfo
    foreach (BaseUserInfo user in result.Value.data)
    {
    Debug.Log($"User data: {user.displayName}");
    }
    });

    AccelByteSDK.GetClientRegistry().GetApi().GetUser().GetUserByUserId(userId, result => {
    Debug.Log($"User data: {result.Value}");
    });

    Depending on the situation, when displaying Party data, you may want to use BulkGetUserInfo() which also contains avatarUrl.

After adding the party logic into your game, you can proceed to implementing the party UI. Follow the steps in the Implement the party UI article.