Hide

Adding Events and Quests to Your Game

This guide shows you how to use the Events and Quests services in a C++ application.

Before you begin

If you haven’t already done so, you might find it helpful to review the events and quests game concepts.

To set up your C++ development environment to use the Events and Quests services, follow the instructions in the Getting Started for C++ guide. You can download the Play Games services C++ SDK from the SDK downloads page.

Before your game can access events and quests, you must define them first in the Google Play Developer Console.

Submitting an event

You can add code in your game to notify the Events service whenever an event of interest to your game occurs. Examples of events you could capture in your game are: killing enemies, exploring or returning to various game regions, or acquiring in-game items. Typically, you would call the Increment method on the event manager to increment an event’s count by 1 every time the player performs an action associated with the event (for example, “Killed one monster”).

The following example shows how you might submit the updated event count to the Events service.

  // Increment the event count when player performs the 'Attack blue
  // monster' action.
  game_services_->Events().Increment(BLUE_MONSTER_EVENT_ID);

Retrieving events

To retrieve the current count value stored in Google's servers for a specific event, call one of the Fetch* methods. You might do this, for example, if you want to show a player’s in-game statistics or progress from a custom UI in your game.

The following example shows how you might retrieve and log the event data in your game.

  // Log Events details.
  LogI("---- Showing Event Counts -----");
  gpg::EventManager::FetchAllCallback callback =
      [](gpg::EventManager::FetchAllResponse const &response) {
    for (auto i : response.data) {
      gpg::Event const &event = i.second;
      LogI("Event name: %s  count: %d", event.Name().c_str(),
           event.Count());
    }
  };
  game_services_->Events().FetchAll(callback);

Displaying quests

To integrate quests into your game, provide players with a user interface (UI) to view the list of available quests. To simplify development, the Play Games SDK provides a default Quest list UI through the QuestManager that you can use right out of the box. The Quest list UI allows players to view details about quests, accept an open quest, and claim rewards for quests they have completed.

To show UI with a list of all quests, call one of the ShowAllUI* functions. To show UI with a single quest, call one of the ShowUI* functions and pass a Quest object.

Handling quest acceptance

Your game can begin tracking players' progress on a quest once they accept it. If your game uses the built-in Quest list UI, player acceptance of a quest causes the QuestUIResponse object to have a valid accepted_quest (call .Valid() to check).

If you are using a UI other than the built-in one, you can use the QuestManager object’s Accept* methods to enable the player to accept the quest.

Handling quest completion

To claim a quest reward, your game must first inform the Quest service that the player has claimed the reward. Your game can then reward the player with the appropriate in-game items or benefits designated by the CompletionRewardData property associated with the quest milestone.

After players accept a quest, make sure to capture their progress in your game. Do this by submitting event updates as they progress toward, or achieve, milestones on the way to completing the quest.

When players achieve all criteria required to complete a quest, they can claim the quest reward from the Quest list UI. If a player makes this selection, your QuestUIResponse struct’s milestone_to_claim field contains a valid QuestMilestone object. You must then claim this milestone by calling the ClaimMilestone method on the QuestManager object; a milestone must be claimed so that it cannot be claimed again. Check the response from ClaimMilestone to see whether the claim succeeded.

gpg::QuestManager::ClaimMilestoneCallback claim_callback =
  [](gpg::QuestManager::ClaimMilestoneResponse const &claim_response) {
    if (IsSuccess(claim_response.status)) {
      LogI("Successfully claimed milestone! Do something with "
           "CompletionRewardData().");
    }
  };

gpg::QuestManager::QuestUICallback ui_callback =
  [this, claim_callback](gpg::QuestManager::QuestUIResponse const &ui_response) {
    if (ui_response.accepted_quest.Valid()) {
      LogI("Accepted quest: %s", response.accepted_quest.Name().c_str());
    }
    if (ui_response.milestone_to_claim.Valid()) {
      game_services_->Quests().ClaimMilestone(ui_response.milestone_to_claim,
                                              claim_callback);
    }
  };

game_services_->Quests().ShowAllUI(ui_callback);

Send feedback about...

Play Games Services for C++