创建大厅
本文介绍如何创建大厅。
如何创建大厅?
可通过多种方式创建大厅。
- 玩家:玩家可以创建大厅,这些玩家希望一组玩家聚在一起玩游戏。 这些是客户端拥有的大厅。
- 游戏服务器:游戏的游戏服务器可以创建大厅并等待玩家加入。 这些是服务器拥有的大厅。
- 通过匹配:匹配后,在一组玩家形成后,会在游戏开始前将大厅创建为保留位置。 这些是客户端拥有的大厅。
从技术角度来看,所有大厅都归入两个主要类别,具体取决于所有权-服务器拥有和客户端拥有。 若要了解详细信息,请参阅 所有者要求和权限。
PlayFab 大厅的一般用途是暂时将一组玩家保持在一起。 有关大厅的常用应用程序,请参阅 Azure PlayFab 大厅概述。
支持的实体类型
在创建大厅之前,您的游戏必须以 PlayFab 实体身份登录。
- 代表玩家创建客户端拥有的大厅时,请以title_player_account实体身份登录。 有关详细信息,请参阅 登录基础知识和最佳做法。
- 代表游戏服务器创建服务器拥有的大厅时,请以game_server实体身份登录。 有关详细信息,请参阅 AuthenticateGameServerWithCustomId。
如何配置大厅
在创建期间为大厅配置了几个重要设置:maxMemberCount 、accessPolicy 、owner和ownerMigrationPolicy。
- maxMemberCount 定义大厅可以容纳的玩家数
- accessPolicy 定义谁可以发现大厅的连接字符串
- 所有者 定义大厅的所有者,该所有者具有更新大厅上数据的特殊权限。 此值隐式是创建大厅的客户端或服务器实体。
- ownerMigrationPolicy 定义了在当前所有者离开大厅或无法维护与大厅的连接时应如何转移大厅所有权的策略。
此外,在创建过程中,大厅创建者还可以定义自定义大厅属性和搜索属性,以将自定义数据追加到大厅会话。 这些自定义属性也可以由大厅所有者在大厅的生存期内进行修改。
有关详细信息,请参阅 大厅属性。
使用大厅和匹配 SDK 创建客户端拥有的大厅的示例
此示例使用作为title_player_account实体登录 PlayFab 的 本地玩家。
在此代码片段中,大厅属性作为 lobbyConfiguration 传入,创建大厅的玩家通过 creatorMemberConfiguration传入其初始成员属性。 本地玩家成为大厅所有者,因此它是客户端拥有的大厅。 成功创建大厅后,PFLobbyHandle 可用于邀请具有 PFLobbySendInvite 的其他玩家。
// 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 将提供 PFLobbyCreateAndJoinLobbyCompletedStateChange。
使用大厅和匹配 SDK 创建服务器拥有的大厅的示例
此示例类似于 客户端拥有的大厅示例 ,只不过它将大厅创建为 game_server 实体,并 调用 PFMultiplayerCreateAndClaimServerLobby 来代替 PFMultiplayerCreateAndJoinLobby。
// 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 将提供 PFLobbyCreateAndClaimServerLobbyCompletedStateChange。
有关客户端拥有的大厅和服务器拥有的大厅之间的差异的详细信息,请参阅游戏服务器和大厅。
注意
若要在 PlayFab 多人游戏中启用游戏服务器 API,必须在包含 PFLobby.h 之前定义PFMULTIPLAYER_INCLUDE_SERVER_APIS。 有关详细信息,请参阅游戏服务器和大厅