Add events to your game

This guide shows you how to use the Events service in a C++ application.

Before you begin

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

To set up your C++ development environment to use the Events service, 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, you must define them first in the Google Play Console.

Submit 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);

Retrieve 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);