更新游戏托管型成就

本主题涵盖以下内容:

解锁成就

通过合作伙伴中心配置完成就并将其发布到开发沙盒后,您的游戏即可通过调用 XblAchievementsUpdateAchievementAsync API 来解锁成就。 有关详细信息,请参阅下一部分中的示例代码。

如需解锁某项成就,请将 percentComplete 设置为 100。 如果用户处于联机状态,则请求将会立即发送至 Xbox 服务成就服务并会触发以下用户体验。

  • 用户将会收到一则成就解锁通知。
  • 在用户的游戏成就列表中,指定的成就将显示为“已解锁”。
  • 解除锁定的成就将添加到用户的活动源。

注意

对于使用基于事件的成就或游戏管理的成就的成就,用户体验在视觉上是相同的。

如果用户处于脱机状态,解锁请求将在用户的设备上以本地方式排队。 当重新建立网络连接时,该请求将自动发送到成就服务(不需要执行任何操作即可触发此操作),并且将按照所述方式执行之前的用户体验。

更新成就的完成进度

若要更新用户解除成就的进度,请调用 XblAchievementsUpdateAchievementAsync。 将 percentComplete 参数设置为介于 1–100 之间的整数。 有关详细信息,请参阅以下代码示例。

成就的进度仅可增加。 如果 percentComplete 设置为小于成就的最后一个 percentComplete 值的数字,则将忽略更新。 例如,如果成就的 percentComplete 以前设置为 75,则将忽略发送值为 25 的更新。 成就仍显示为已完成 75%。

如果 percentComplete设置为 100,成就将解除锁定。

如果 percentComplete 设为大于 100 的数字,则 API 将按照设为 100 一样运行。

更新现有游戏的成就

你可以通过调用 XblAchievementsUpdateAchievementAsync 来更新当前游戏的成就,如下所示。

平面 C API

auto asyncBlock = std::make_unique<XAsyncBlock>();
asyncBlock->queue = queue;
asyncBlock->context = nullptr;
asyncBlock->callback = [](XAsyncBlock* asyncBlock)
{
    std::unique_ptr<XAsyncBlock> asyncBlockPtr{ asyncBlock }; // Take over ownership of the XAsyncBlock*.
    auto result = XAsyncGetStatus(asyncBlock, false);
    if (SUCCEEDED(result))
    {
        // The achievement was updated.
    }
    else if (result == HTTP_E_STATUS_NOT_MODIFIED)
    {
        // The achievement wasn't updated.
    }
    else
    {
        // The achievement failed to update.
    }
};

HRESULT hr = XblAchievementsUpdateAchievementAsync(
    xboxLiveContext,
    xboxUserId,
    achievementId.c_str(),
    percentComplete,
    asyncBlock.get()
);
if (SUCCEEDED(hr))
{
    // The call succeeded, so release the std::unique_ptr ownership of XAsyncBlock* because the callback will take over ownership.
    // If the call fails, std::unique_ptr will keep ownership and delete XAsyncBlock*.
    asyncBlock.release();
}

有关详细信息,请参阅以下内容:

更新另一个游戏的成就

你可以通过使用 XblAchievementsUpdateAchievementForTitleIdAsync,来更新另一个 titleId 的成就,如下所示。

平面 C API

auto asyncBlock = std::make_unique<XAsyncBlock>();
asyncBlock->queue = queue;
asyncBlock->context = nullptr;
asyncBlock->callback = [](XAsyncBlock* asyncBlock)
{
    std::unique_ptr<XAsyncBlock> asyncBlockPtr{ asyncBlock }; // Take over ownership of the XAsyncBlock*.
    auto result = XAsyncGetStatus(asyncBlock, false);
    if (SUCCEEDED(result))
    {
        // The achievement was updated.
    }
    else if (result == HTTP_E_STATUS_NOT_MODIFIED)
    {
        // The achievement wasn't updated.
    }
    else
    {
        // The achievement failed to update.
    }
};

HRESULT hr = XblAchievementsUpdateAchievementForTitleIdAsync(
    xboxLiveContext,
    xboxUserId,
    titleId,
    scid,
    achievementId.c_str(),
    percentComplete,
    asyncBlock.get()
);
if (SUCCEEDED(hr))
{
    // The call succeeded, so release the std::unique_ptr ownership of XAsyncBlock* because the callback will take over ownership.
    // If the call fails, std::unique_ptr will keep ownership and delete XAsyncBlock*.
    asyncBlock.release();
}

有关详细信息,请参阅以下内容:

返回到本主题顶部。