> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 创建大厅

> 创建客户端或服务器所拥有的 PlayFab 大厅实例,并为多人游戏会话配置最大玩家数、访问策略和初始成员属性。

本文介绍如何创建大厅。

## 大厅是如何创建的?

有多种方式可以创建大厅。

* **由你的玩家创建**:大厅可以由想要组队一起玩的玩家创建。这些是客户端所拥有的大厅。
* **由你的游戏服务器创建**:你的游戏服务器可以创建大厅并等待玩家加入。这些是服务器所拥有的大厅。
* **通过匹配创建**:一组玩家经过匹配后,创建一个大厅作为游戏开始之前的容纳场所。这些是客户端所拥有的大厅。

从技术角度看,所有大厅根据所有权分为两大类—服务器所拥有和客户端所拥有。若要了解更多,请参阅[所有者要求与特权](/services/playfab/multiplayer/lobby/owner-requirements-and-privileges)。

PlayFab 大厅的一般用法是暂时将一组玩家聚集在一起。有关 Lobby 的常用应用,请参阅 [PlayFab Lobby 概览](/services/playfab/multiplayer/lobby)。

### 支持的实体类型

在创建大厅之前,你的游戏必须以 PlayFab 实体身份登录。

* 代表玩家创建客户端所拥有的大厅时,请以 title\_player\_account 实体身份登录。有关更多信息,请参阅[登录基础与最佳实践](/services/playfab/identity/player-identity/login/login-basics-best-practices)。
* 代表游戏服务器创建服务器所拥有的大厅时,请以 game\_server 实体身份登录。有关更多信息,请参阅 [AuthenticateGameServerWithCustomId](https://learn.microsoft.com/en-us/rest/api/playfab/authentication/authentication/authenticate-game-server-with-custom-id)。

## 大厅的配置方式

大厅的几项重要设置是在创建时配置的:**maxMemberCount**、**accessPolicy**、**owner** 和 **ownerMigrationPolicy**。

* **maxMemberCount** 定义大厅可容纳的玩家数量
* **accessPolicy** 定义谁可以发现大厅的连接字符串
* **owner** 定义大厅所有者,该所有者拥有更新大厅数据的特殊权限。此值隐式为创建大厅的客户端或服务器实体。
* **ownerMigrationPolicy** 定义在当前所有者离开大厅或与大厅保持连接出现问题时,如何转移大厅所有权的策略。

此外,在创建期间,大厅创建者还可以定义自定义大厅属性和搜索属性,以便向大厅会话附加自定义数据。这些自定义属性也可以在大厅生命周期内由大厅所有者修改。

有关更多信息,请参阅[大厅属性](/services/playfab/multiplayer/lobby/lobby-properties)。

## 使用 Lobby 和 Matchmaking SDK 创建客户端所拥有大厅的示例

此示例使用一名已作为 [title\_player\_account](/services/playfab/live-service-management/game-configuration/entities/available-built-in-entity-types) 实体登录到 PlayFab 的本地玩家。

在此代码片段中,大厅属性以 **lobbyConfiguration** 传入,创建大厅的玩家通过 **creatorMemberConfiguration** 传入其初始成员属性。本地玩家成为大厅所有者,因此这是一个客户端所拥有的大厅。大厅成功创建后,可以使用 **PFLobbyHandle** 通过 [PFLobbySendInvite](/services/playfab/multiplayer/lobby/playfabmultiplayerreference-cpp/pflobby/functions/pflobbysendinvite) 邀请其他玩家。

```cpp theme={null}
    // Retrieved elsewhere from SDK's PFMultiplayerInitialize API
    PFMultiplayerHandle apiHandle = g_pfmHandle;

    // Retrieved elsewhere from one of PlayFab's title_player_account login APIs
    const PFEntityKey* clientOwner = m_localPlayerTitlePlayerAccounts[0];

    // Initialize the lobby configuration
    const char* gameModePropertyKey = "GameMode";
    const char* gameModePropertyValue = "GameMode_Foo";

    PFLobbyCreateConfiguration lobbyConfiguration{};
    lobbyConfiguration.maxMemberCount = 16;
    lobbyConfiguration.ownerMigrationPolicy = PFLobbyOwnerMigrationPolicy::Automatic;
    lobbyConfiguration.accessPolicy = PFLobbyAccessPolicy::Private;
    lobbyConfiguration.lobbyPropertyCount = 1;
    lobbyConfiguration.lobbyPropertyKeys = &gameModePropertyKey;
    lobbyConfiguration.lobbyPropertyValues = &gameModePropertyValue;
    
    // Initialize the creator's member properties
    const char* playerColorPropertyKey = "PlayerColor";
    const char* playerColorPropertyValue = "Red";

    PFLobbyJoinConfiguration creatorMemberConfiguration{};
    creatorMemberConfiguration.memberPropertyCount = 1;
    creatorMemberConfiguration.memberPropertyKeys = &playerColorPropertyKey;
    creatorMemberConfiguration.memberPropertyValues = &playerColorPropertyValue;

    // Create a lobby using our first player
    PFLobbyHandle lobby;
    HRESULT error = PFMultiplayerCreateAndJoinLobby(
        apiHandle,
        clientOwner,
        &lobbyConfiguration,
        &creatorMemberConfiguration,
        nullptr,
        &lobby);
```

创建大厅的调用完成时,[PFMultiplayerStartProcessingLobbyStateChanges](/services/playfab/multiplayer/lobby/playfabmultiplayerreference-cpp/pflobby/functions/pfmultiplayerstartprocessinglobbystatechanges) 将提供一个 [PFLobbyCreateAndJoinLobbyCompletedStateChange](/services/playfab/multiplayer/lobby/playfabmultiplayerreference-cpp/pflobby/structs/pflobbycreateandjoinlobbycompletedstatechange)。

## 使用 Lobby 和 Matchmaking SDK 创建服务器所拥有大厅的示例

此示例类似于[客户端所拥有大厅的示例](#example-creating-a-client-owned-lobby-using-the-lobby-and-matchmaking-sdk),不同之处在于它以 [game\_server](/services/playfab/live-service-management/game-configuration/entities/available-built-in-entity-types) 实体身份创建大厅,并调用 [PFMultiplayerCreateAndClaimServerLobby](/services/playfab/multiplayer/lobby/playfabmultiplayerreference-cpp/pflobby/functions/pfmultiplayercreateandclaimserverlobby) 而不是 [PFMultiplayerCreateAndJoinLobby](/services/playfab/multiplayer/lobby/playfabmultiplayerreference-cpp/pflobby/functions/pfmultiplayercreateandjoinlobby)。

```cpp theme={null}
    // Retrieved elsewhere from SDK's PFMultiplayerInitialize API
    PFMultiplayerHandle apiHandle = g_pfmHandle;

    // Retrieved elsewhere from one of PlayFab's game_server login APIs
    const PFEntityKey* serverOwner = m_gameServerEntityKey;

    const char* gameModePropertyKey = "GameMode";
    const char* gameModePropertyValue = "GameMode_Foo";
    
    // Initialize the lobby configuration
    PFLobbyCreateConfiguration lobbyConfiguration{};
    lobbyConfiguration.maxMemberCount = 16;
    lobbyConfiguration.ownerMigrationPolicy = PFLobbyOwnerMigrationPolicy::Automatic;
    lobbyConfiguration.accessPolicy = PFLobbyAccessPolicy::Public;
    lobbyConfiguration.lobbyPropertyCount = 1;
    lobbyConfiguration.lobbyPropertyKeys = &gameModePropertyKey;
    lobbyConfiguration.lobbyPropertyValues = &gameModePropertyValue;
    
    // Create a lobby using our first player
    PFLobbyHandle lobby;
    HRESULT error = PFMultiplayerCreateAndClaimServerLobby(
        apiHandle,
        serverOwner,
        &lobbyConfiguration,
        nullptr,
        &lobby);
```

创建大厅的调用完成时,[PFMultiplayerStartProcessingLobbyStateChanges](/services/playfab/multiplayer/lobby/playfabmultiplayerreference-cpp/pflobby/functions/pfmultiplayerstartprocessinglobbystatechanges) 将提供一个 [PFLobbyCreateAndClaimServerLobbyCompletedStateChange](/services/playfab/multiplayer/lobby/playfabmultiplayerreference-cpp/pflobby/structs/pflobbycreateandclaimserverlobbycompletedstatechange)。

有关客户端所拥有大厅与服务器所拥有大厅之间差异的更多信息,请参阅[游戏服务器与大厅](/services/playfab/multiplayer/lobby/lobby-server-overview)。

<Note>
  若要在 PlayFab Multiplayer 中启用游戏服务器 API,你必须在包含 PFLobby.h 之前定义 PFMULTIPLAYER\_INCLUDE\_SERVER\_APIS。有关更多信息,请参阅[游戏服务器与大厅](/services/playfab/multiplayer/lobby/lobby-server-overview)
</Note>

## 另请参阅

* [Lobby SDK 参考](/services/playfab/multiplayer/lobby/playfabmultiplayerreference-cpp/pflobby/pflobby_members)
* [创建可搜索的大厅](/services/playfab/multiplayer/lobby/define-search-keywords)
* [邀请玩家加入大厅](/services/playfab/multiplayer/lobby/lobby-invites)
* [大厅属性](/services/playfab/multiplayer/lobby/lobby-properties)
* [大厅与匹配](/services/playfab/multiplayer/lobby/lobby-and-matchmaking)


## Related topics

- [Lobby SDK 快速入门](/zh-CN/services/playfab/multiplayer/lobby/lobby-getting-started.md)
- [从 MPSD 迁移到 PlayFab Multiplayer 和 MPA](/zh-CN/services/xbox-services/multiplayer/mpsd/concepts/live-mpsd-to-mlp.md)
- [查找大厅](/zh-CN/services/playfab/multiplayer/lobby/find-lobbies.md)
- [加入大厅](/zh-CN/services/playfab/multiplayer/lobby/join-lobbies.md)
- [创建可搜索的大厅](/zh-CN/services/playfab/multiplayer/lobby/define-search-keywords.md)
