> ## 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 Lobby 執行個體，並為多人遊戲工作階段設定最大玩家數、存取原則和初始成員屬性。

本文說明如何建立大廳。

## 大廳是如何建立的？

大廳有數種建立方式。

* **由您的玩家建立**：想要召集一組玩家一起遊玩的玩家可以建立大廳。這些是用戶端擁有的大廳。
* **由您的遊戲伺服器建立**：您遊戲的遊戲伺服器可以建立大廳並等候玩家加入。這些是伺服器擁有的大廳。
* **由配對建立**：在配對後形成一組玩家時，會建立一個大廳作為遊戲開始前的暫留處。這些是用戶端擁有的大廳。

從技術角度來看，所有大廳根據擁有權分為兩大類——伺服器擁有和用戶端擁有。若要深入了解，請參閱[擁有者需求和權限](/zh-TW/services/playfab/multiplayer/lobby/owner-requirements-and-privileges)。

PlayFab Lobby 的一般用途是暫時將一組玩家聚集在一起。如需 Lobby 的常見應用，請參閱 [PlayFab Lobby 概觀](/zh-TW/services/playfab/multiplayer/lobby)。

### 支援的實體類型

在建立大廳之前，您的遊戲必須以 PlayFab 實體身分登入。

* 代表玩家建立用戶端擁有的大廳時，請以 title\_player\_account 實體身分登入。如需詳細資訊，請參閱[登入基本概念和最佳做法](/zh-TW/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** 定義當目前擁有者離開大廳或難以維持與大廳的連線時，應如何轉移大廳擁有權的原則。

此外，在建立期間，大廳建立者也可以定義自訂大廳屬性和搜尋屬性，以將自訂資料附加至大廳工作階段。在大廳的存留期間，大廳擁有者也可以修改這些自訂屬性。

如需詳細資訊，請參閱[大廳屬性](/zh-TW/services/playfab/multiplayer/lobby/lobby-properties)。

<a id="example-creating-a-client-owned-lobby-using-the-lobby-and-matchmaking-sdk" />

## 使用 Lobby 和 Matchmaking SDK 建立用戶端擁有大廳的範例

此範例使用已以 [title\_player\_account](/zh-TW/services/playfab/live-service-management/game-configuration/entities/available-built-in-entity-types) 實體身分登入 PlayFab 的本機玩家。

在此程式碼片段中，大廳屬性以 **lobbyConfiguration** 傳入，而建立大廳的玩家則透過 **creatorMemberConfiguration** 傳入其初始成員屬性。本機玩家會成為大廳擁有者，因此這是用戶端擁有的大廳。成功建立大廳後，可以使用 **PFLobbyHandle** 透過 [PFLobbySendInvite](/zh-TW/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](/zh-TW/services/playfab/multiplayer/lobby/playfabmultiplayerreference-cpp/pflobby/functions/pfmultiplayerstartprocessinglobbystatechanges) 會提供 [PFLobbyCreateAndJoinLobbyCompletedStateChange](/zh-TW/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](/zh-TW/services/playfab/live-service-management/game-configuration/entities/available-built-in-entity-types) 實體身分建立大廳，並呼叫 [PFMultiplayerCreateAndClaimServerLobby](/zh-TW/services/playfab/multiplayer/lobby/playfabmultiplayerreference-cpp/pflobby/functions/pfmultiplayercreateandclaimserverlobby) 來取代 [PFMultiplayerCreateAndJoinLobby](/zh-TW/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](/zh-TW/services/playfab/multiplayer/lobby/playfabmultiplayerreference-cpp/pflobby/functions/pfmultiplayerstartprocessinglobbystatechanges) 會提供 [PFLobbyCreateAndClaimServerLobbyCompletedStateChange](/zh-TW/services/playfab/multiplayer/lobby/playfabmultiplayerreference-cpp/pflobby/structs/pflobbycreateandclaimserverlobbycompletedstatechange)。

如需用戶端擁有大廳與伺服器擁有大廳之間差異的詳細資訊，請參閱[遊戲伺服器和大廳](/zh-TW/services/playfab/multiplayer/lobby/lobby-server-overview)。

<Note>
  若要在 PlayFab Multiplayer 中啟用遊戲伺服器 API，您必須在包含 PFLobby.h 之前定義 PFMULTIPLAYER\_INCLUDE\_SERVER\_APIS。如需詳細資訊，請參閱[遊戲伺服器和大廳](/zh-TW/services/playfab/multiplayer/lobby/lobby-server-overview)
</Note>

## 另請參閱

* [Lobby SDK 參考](/zh-TW/services/playfab/multiplayer/lobby/playfabmultiplayerreference-cpp/pflobby/pflobby_members)
* [建立可搜尋的大廳](/zh-TW/services/playfab/multiplayer/lobby/define-search-keywords)
* [邀請玩家加入大廳](/zh-TW/services/playfab/multiplayer/lobby/lobby-invites)
* [大廳屬性](/zh-TW/services/playfab/multiplayer/lobby/lobby-properties)
* [大廳和配對](/zh-TW/services/playfab/multiplayer/lobby/lobby-and-matchmaking)


## Related topics

- [建立大廳](/zh-TW/services/playfab/api-references/rest/multiplayer/lobby/create-lobby.md)
- [Lobby SDK 快速入門](/zh-TW/services/playfab/multiplayer/lobby/lobby-getting-started.md)
- [從 MPSD 移至 PlayFab Multiplayer 與 MPA](/zh-TW/services/xbox-services/multiplayer/mpsd/concepts/live-mpsd-to-mlp.md)
- [大廳擁有權](/zh-TW/services/playfab/multiplayer/lobby/owner-requirements-and-privileges.md)
- [尋找大廳](/zh-TW/services/playfab/multiplayer/lobby/find-lobbies.md)
