使用游戏实体访问 PlayFab

与以玩家身份登录和进行 PlayFab 调用的方式类似,你可作为游戏实体进行身份验证,并通过该实体进行 PlayFab 调用。 游戏实体可使用玩家实体不可用的某些函数。 其中许多函数都以“server”一词作为前缀,ServerAddPlayerTag 就是这样的一个示例。

获取游戏实体句柄时,初始化步骤与以玩家身份登录时相同。 有关详细信息,请参阅 Win32 快速入门一文的“初始化”部分。

注意:目前,服务器函数仅在 Win32 版本的 SDK 上可用。

对游戏实体进行身份验证

在初始步骤中创建 PFServiceConfigHandle 后,可通过调用 PFAuthenticationGetEntityWithSecretKeyAsync 来获取游戏实体句柄。 成功完成“获取实体”调用后,它将返回 S_OKPFEntityHandle。 此过程与以玩家身份登录的过程类似。 PFEntityHandle 的返回类型相同,但在这种情况下,实体句柄表示游戏实体而不是玩家实体。 我们可以将此实体称为游戏服务器。

    PFAuthenticationGetEntityRequest request{};
    XAsyncBlock async{};

    // Add your own error handling when FAILED(hr) == true
    HRESULT hr = PFAuthenticationGetEntityWithSecretKeyAsync(
        serviceConfigHandle,
        secretKey, // title specific secret key
        &request,
        &async
    );

    hr = XAsyncGetStatus(&async, true); // This is doing a blocking wait for completion, but you can use the XAsyncBlock to set a callback instead for async style usage

    PFEntityHandle entityHandle;
    hr = PFAuthenticationGetEntityWithSecretKeyGetResult(
        &async,
        &entityHandle
    );

服务调用

创建游戏服务器后,可以调用 PlayFab 后端。 下面是向游戏中的特定玩家添加玩家标记的示例。

    XAsyncBlock async{};
    PFSegmentsAddPlayerTagRequest request{};
    addSegmentRequest.PlayFabId = "ABCDEF123456";
    addSegmentRequest.tagName = "tagName";
    HRESULT hr = PFSegmentsServerAddPlayerTagAsync(entityHandle, &request, &async);
    hr = XAsyncGetStatus(&async, true);

与 AddPlayerTag 示例类似,游戏服务器使用的许多函数都涉及获取或更改玩家数据。 此函数需要具有特定玩家的 PlayFab ID。 连接的客户端可提供 PlayFab ID,或者服务器可通过调用 GetPlayFabIDsFromXboxLiveIDs 等直接从 PlayFab 检索这些 ID。

参考

API 参考文档