Skip to main content
This quickstart assists you in making your first PlayFab API call in the Cocos2d-x engine. Before you can call any PlayFab API, you must have a PlayFab developer account.

Cocos2d-x project setup

OS: This guide is written for Windows 10, using Visual Studio 2015. Cocos works on most modern OSs and environments. The installation instructions are similar, but different for each combination. If you’re building for other platforms, the files you need are the same, but you’ll need to do the project setup yourself. Visual Studio 2013 has identical steps, but the screenshots provided here look a bit different from yours.
  1. Download and install Cocos2d-x
  2. Once you have Cocos2d-x configured, create a project using the Cocos CLI:
    • Navigate to a location where you wish to store your Cocos project
    • Open a command window in your parent folder (Cocos CLI will create the actual project directory)
      • Hold down your Shift key, and right-click in the empty white space of the Explorer window.
    • In the new console window enter this command:
      • cocos new CocosGettingStarted -l cpp
        • Make sure your target sub-directory (CocosGettingStarted) doesn’t already exist - This command will fail if the folder already exists.
        • If you get a message that “‘cocos’ isn’t recognized as an internal or external command”, you haven’t configured the cocos installation correctly (Go back to the Cocos windows installation guide).
        • If successful, there will be a new folder CocosGettingStarted. This guide will refer to that directory location as {CocosGettingStarted}.
    • Successful output should appear similar to the example provided below.
  1. Download PlayFab Cocos2d-xSdk: Cocos2D-x SDK (C++). Save and extract it to a temporary location {PlayFabCocos}
    • Open the following folder in Windows Explorer: {PlayFabCocos}/PlayFabClientSDK
    • Open the following folder in a second Windows Explorer: {CocosGettingStarted}/Classes
  2. Copy paste all files from {PlayFabCocos}/PlayFabClientSDK to {CocosGettingStarted}/Classes
  3. In Visual Studio, Load {CocosGettingStarted}/proj.win32/CocosGettingStarted.sln.
  4. We want to add the PlayFab files to the Cocos project.
  5. In Visual Studio, Solution Explorer panel, expand to the folder: Solution/CocosGettingStarted/src
  6. Open a Windows Explorer window at {CocosGettingStarted}/Classes
    • Select all files in {CocosGettingStarted}/Classes, EXCEPT AppDelegate.h, AppDelegate.cpp, HelloWorldScene.h, HelloWorldScene.cpp
    • Drag and drop all of those files from Explorer, onto the Visual Studio Solution/CocosGettingStarted/src folder we found above. If you experience problems, you can drag and drop each file one at a time, just be careful and get all of them.
    • You should see these files in your VS project:
PlayFab uses several Cocos libraries that have to be manually added to the dependencies list.
  • Open the Properties window for your CocosGettingStarted project (as shown below).
  • Replace the Additional Include Directories with this: $(ProjectDir)..\cocos2d\external\zlib\include;$(ProjectDir)..\cocos2d\external\curl\include\win32;$(EngineRoot)cocos\audio\include;$(EngineRoot)external;$(EngineRoot)external\chipmunk\include\chipmunk;$(EngineRoot)extensions;..\Classes;..;%(AdditionalIncludeDirectories);$(_COCOS_HEADER_WIN32_BEGIN);$(_COCOS_HEADER_WIN32_END);..\cocos2d
We’re adding curl and zlib, which are libraries that come with Cocos, but aren’t enabled by default.
Your CocosGettingStarted project should now compile (and even run), but we aren’t yet making any PlayFab API calls. Installation complete!

Set up your first API call

This guide provides the minimum steps to make your first PlayFab API call. Confirmation is visible in the app.
  1. In Visual Studio, inside of the Solution/CocosGettingStarted/src folder, Open HelloWorldScene.h and replace the contents with this:
    • In Visual Studio, inside of the Solution/CocosGettingStarted/src folder, Open HelloWorldScene.h and replace the contents with those shown below.
  1. Immediately next to that, open HelloWorldScene.cpp, and replace the contents with this:
These files take the existing HelloWorldScene that is part of the new-Cocos-project template, and modify them to include your first PlayFab API call.

Finish and execute

  1. Build and Execute your Cocos Project: Dropdowns -> Debug -> Start Debugging.
  2. This will prompt you to build. Select Yes.
  3. You should see a screen that says: Congratulations, you made your first successful API call!
  4. Now, you can start making other API calls, and building your game.
    For a list of all available client API calls, see our PlayFab API References documentation.
Happy coding!

Deconstruct the code

This optional last section describes each part of source code above, in detail.
  • HelloWorldScene.h
    • This is only trivially modified from the default HelloWorldScene.h generated by Cocos.
    • Specifically, it defines some Cocos GUI we’re using, and the prototype for OnLoginSuccess and OnLoginFail.
    • Everything else is standard Cocos Engine functions.
  • HelloWorldScene.cpp
    • createScene() is a standard Cocos Engine function.
    • init()
      • Normal Cocos Gui stuff: closeItem and testReportLabel.
      • PlayFab::PlayFabSettings::titleId = "xxxx";
        • Every PlayFab developer creates a title in Game Manager. When you publish your game, you must code that titleId into your game. This lets the client know how to access the correct data within PlayFab. For most users, just consider it a mandatory step that makes PlayFab work.
      • PlayFab::ClientModels::LoginWithCustomIDRequest request;
        • Most PlayFab API methods require input parameters, and those input parameters are packed into a request object.
        • Every API method requires a unique request object, with a mix of optional and mandatory parameters.
          • For LoginWithCustomIDRequest, there’s a mandatory parameter of CustomId, which uniquely identifies a player and CreateAccount, which allows the creation of a new account with this call.
        • For login, most developers will want to use a more appropriate login method.
          • See the PlayFab Login documentation for a list of all login methods, and input parameters. Common choices are:
            • LoginWithAndroidDeviceID
            • LoginWithIOSDeviceID
            • LoginWithEmailAddress
      • PlayFab::PlayFabClientAPI::LoginWithCustomID(request, OnLoginSuccess, OnLoginFail, nullptr);
        • This begins the async request to LoginWithCustomID, and will invoke the OnLoginSuccess or OnLoginFail function when complete.
    • update(float delta)
      • Simply setting the statusMsg variable doesn’t update the on-screen text.
      • This function sets the GUI text to match the contents of statusMsg every tick (not very efficient).
    • OnLoginSuccess(result, customData)
      • When the success callback is called, the result object of many API callbacks will contain the requested information.
      • LoginResult contains some basic information about the player, but for most users, login is simply a mandatory step before calling other APIs.
    • OnLoginFail(error, customData)
      • If the error-function is called, your API call has failed.
      • API calls can fail for many reasons, and you should always attempt to handle failure.
      • Why API calls fail (In order of likelihood)
        • PlayFabSettings.TitleId isn’t set. If you forget to set titleId to your title, then nothing will work.
          • In Cocos, the curl library will probably just crash your game if you fail to set titleId correctly.
        • Request parameters. If you haven’t provided the correct or required information for a particular API call, then it will fail. See error.errorMessage, error.errorDetails, or error.GenerateErrorReport() for more info.
        • Device connectivity issue. Cell phones lose/regain connectivity constantly, and so any API call at any time can fail randomly, and then work immediately after. Going into a tunnel can disconnect you completely.
        • PlayFab server issue. As with all software, there can be issues. See our release notes for updates.
        • The internet isn’t 100% reliable. Sometimes the message is corrupted or fails to reach the PlayFab server.
      • If you’re having difficulty debugging an issue, and the information within the error callback isn’t sufficient, visit us on our forums.
    • customData is a void-pointer, which can be used in any way to establish context.
      • In C++, it’s harder to maintain the context of an API call, so we added the customData parameter, which can relay any object to the callback, which can be used however you like to establish context.
      • Thus, if you make API calls to retrieve inventory, you can pass a player, or inventory pointer as customData, and update the inventory on that object, in the callback.
Last modified on August 10, 2026