Skip to main content

Updating lobbies and managing lobby members

Lobbies have two types of data:

  • Lobby data, which is stored against the lobby itself
  • Lobby member data, which is stored for a particular member of a lobby

The owner of a lobby can update the lobby data and kick members.

Members of a lobby can update the lobby member data of themselves. The lobby owner can't update the member data of another member (even though they're the owner).

Any member of the lobby, whether they are the host or not, can see lobby data, view the list of members in the lobby, and see the current lobby member data of a member.

When the owner of a lobby deletes the lobby, all members of the lobby are kicked and removed from the lobby. If you don't want this behaviour, you should use create a party instead, as parties support promoting another member as a new owner, whereas the lobby interface does not.

Update a lobby's settings, or change lobby data

You can update a lobby's settings (such as whether it is public and it's capacity), and update the lobby data of a lobby in a single update lobby call.

To update a lobby, first get the online lobby interface:

#include "OnlineSubsystem.h"
#include "OnlineSubsystemUtils.h"
// If your project can't find this header, make sure you have installed the headers from here:
// https://src.redpoint.games/redpointgames/online-interfaces/
#include "OnlineLobbyInterface.h"

// ...

IOnlineSubsystem* Subsystem = Online::GetSubsystem(this->GetWorld());
TSharedPtr<IOnlineLobby, ESPMode::ThreadSafe> Lobby = Online::GetLobbyInterface(Subsystem);

Then, make a lobby transaction for updating the lobby, using the LobbyId of a lobby you own:

TSharedPtr<const FUniqueNetId> LocalUserId = Identity->GetUniquePlayerId(0);
TSharedPtr<FOnlineLobbyTransaction> Txn = Lobby->MakeUpdateLobbyTransaction(*LobbyIdFromPreviousCreate, *LocalUserId.Get());

// To change the visibility of the lobby, set the Public value. If you
// don't set a value for this, it leaves the setting unchanged.
Txn->Public = true;

// To change the capacity of the lobby, set the Capacity value. If you
// don't set a value for this, it leaves the setting unchanged.
Txn->Capacity = 4;

// To change whether players can join the lobby, set the Locked value.
// If you don't set a value for this, it leaves the setting unchanged.
Txn->Locked = false;

// To set or change existing lobby data, change the SetMetadata
// map on the transaction.
Txn->SetMetadata.Add(TEXT("MySetting"), FVariantData(TEXT("MyValue")));

// To delete existing lobby data, add an item to the DeleteMetadata
// array on the transaction.
Txn->DeleteMetadata.Add(TEXT("MySettingToDelete"));

Then, update the lobby:

if (!Lobby->UpdateLobby(
*LocalUserId.Get(),
*LobbyId,
*Txn,
FOnLobbyOperationComplete::CreateLambda([](
const FOnlineError & Error,
const FUniqueNetId & UserId)
{
if (Error.WasSuccessful())
{
// The lobby was updated successfully.
}
else
{
// Lobby could not be updated.
}
})))
{
// Call failed to start.
}

Update lobby member data

When you're in a lobby, you can update your own member data, but no-one else's, even if you're the lobby owner.

caution

UpdateMemberSelf can only be called for yourself. You can't update the member data of a different member in the lobby.

First, create a Online Lobby Member Transaction for modifying the lobby member's data:

TSharedPtr<const FUniqueNetId> LocalUserId = Identity->GetUniquePlayerId(0);
TSharedPtr<FOnlineLobbyMemberTransaction> Txn = Lobby->MakeUpdateLobbyMemberTransaction(*LocalUserId.Get(), *LobbyIdFromPreviousConnectOrCreate, *LocalUserId.Get());

// Used to set Metadata, call SetMetadata using the variable name you want, followed by the value.
Txn->SetMetadata.Add(TEXT("ExampleAttributeName"), FVariantData(TEXT("ExampleAttributeValue")));

// Used to delete Metadata, using an Attribute name.
Txn->DeleteMetadata(TEXT("ExampleAttributeToDelete"));

Then, update your own lobby member data, using the LobbyId of a lobby you are currently connected to:

if (!Lobby->UpdateMemberSelf(
*LocalUserId.Get(),
*LobbyId,
*Txn,
FOnLobbyOperationComplete::CreateLambda([](
const FOnlineError & Error,
const FUniqueNetId & UserId)
{
if (Error.WasSuccessful())
{
// Your lobby member data was updated successfully.
}
else
{
// Your lobby member data could not be updated.
}
})))
{
// Call failed to start.
}

Getting lobby members

Anyone in the lobby can get the list of currently connected lobby members.

To get each member's UniqueNetId, get the lobby member count and then iterate through that count by calling GetMemberUserId.

TArray<TSharedRef<const FUniqueNetId>> MemberIds;
const int32 MemberCount = Lobby->GetMemberCount(
*LocalUserId.Get(),
*LobbyId)

for (int32 i = 0; i < MemberCount; i++)
{
TSharedPtr<const FUniqueNetId> MemberId;
Lobby->GetMemberUserId(*LobbyId, Index, NewId);
if (MemberId.IsValid())
{
MemberIds.AddUnique(NewId.ToSharedRef());
}
}

// MemberIds now contains all of the member user IDs.

Checking who is the owner of a lobby

The owner of the lobby is returned in the result when creating a lobby or joining a lobby. The owner of a lobby can never change, so there is no API to get the owner after a lobby is created or joined.

If you need to know who the owner of a lobby is, store the owner ID from the create or join operation and then reference it later.

Convert a lobby ID to a string

Lobby IDs are not string types by default (either in C++ or blueprints). If you want to send a lobby ID to another player or store it in the attribute of a lobby, party or session, you'll need to convert it to a string first.

To convert a LobbyId into a string, call ToString() on a previously created LobbyId.

FString SerializedLobbyIdFromPreviousCreate = LobbyIdFromPreviousCreate.ToString();

Parsing a string as a lobby ID

If you're receiving a lobby ID as a string, you'll need to convert it back to the lobby ID type before you can use it with the lobby API.

To convert a string back into a LobbyId in C++, call ParseSerializedLobbyId with the string value as the first argument.

TSharedPtr<FOnlineLobbyId> LobbyId = Lobby->ParseSerializedLobbyId(SerializedLobbyIdFromPreviousCreate);
if (LobbyId.IsValid())
{
// LobbyId holds a lobby ID with a valid syntax (though
// it doesn't necessarily mean the lobby exists or that
// you can join it).
}
else
{
// The string value wasn't a lobby ID.
}

Kicking a lobby member

The owner of the lobby can remove ("kick") any member of the lobby.

To kick a member, call KickMember as the owner of the lobby with the target member's user ID:

if (!Lobby->KickMember(
*LocalUserId.Get(),
*LobbyIdFromPreviousCreate,
*MemberUserIdToKick,
FOnLobbyOperationComplete::CreateLambda([](
const FOnlineError & Error,
const FUniqueNetId & UserId)
{
if (Error.WasSuccessful())
{
// The member was kicked successfully.
}
else
{
// Member could not be kicked.
}
})))
{
// Call failed to kick.
}