适用于 Linux 的 C++ 快速入门
PlayFab 客户端库入门 C#。 按照步骤安装程序包,并尝试基本任务的示例代码。
本快速入门可帮助你使用 C++ 客户端库进行首次 PlayFab API 调用。
本快速入门是使用 Ubuntu 18.04 LTS 编写的。
要求
- 一个 PlayFab 开发者帐户。
Linux C++ 项目设置
- 安装以下内容(在 Ubuntu 上使用 sudo apt install ____):
- g++
- gdb
- make
- cmake
- libjsoncpp-dev
- libcurl4-openssl-dev
- git-all
- 将 PlayFab 跨平台 (CPP) SDK 克隆到项目文件夹中。
验证安装是否正确。
创建 main.cpp 并插入下面所示的"Hello World"代码。
// main.cpp: entry point for the console application
#include <iostream>
int main()
{
std::cout << "Hello World" << std::endl;
return 0;
}
如果使用 IDE,请确保:
将以下文件夹添加到您的源:
- XPlatCppSdk/cppsdk/source/playfab
将以下文件夹添加到包含目录:
- XPlatCppSdk/cppsdk
- XPlatCppSdk/cppsdk/include
链接以下库:
- jsoncpp
- curl
- pthread
否则,请创建一个名为 CMakeLists.txt 的文件并复制如下所示的生成命令。
cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_STANDARD 14)
project(PlayFab_Test)
file(GLOB PF_SOURCE XPlatCppSdk/cppsdk/source/playfab/*.cpp)
add_executable(PlayFab_Test ${PF_SOURCE} main.cpp)
include_directories(XPlatCppSdk/cppsdk)
include_directories(XPlatCppSdk/cppsdk/include)
target_link_libraries(PlayFab_Test -ljsoncpp -lcurl -lpthread)
- 在 IDE 中生成并运行项目,或打开终端并运行:
cmake .
make
./PlayFab_Test
设置您的首次 API 调用
本指南提供进行首次 PlayFab API 调用(无任何 GUI 或屏幕反馈)的最少步骤。 确认通过控制台输出语句进行。
首先,将 main.cpp
的内容替换为以下所示内容。
// main.cpp: entry point for the console application
#include "playfab/PlayFabClientDataModels.h"
#include "playfab/PlayFabClientApi.h"
#include "playfab/PlayFabSettings.h"
#include <unistd.h>
using namespace PlayFab;
using namespace ClientModels;
bool finished = false;
void OnLoginSuccess(const LoginResult& result, void* customData)
{
printf("Congratulations, you made your first successful API call!\n");
finished = true;
}
void OnLoginFail(const PlayFabError& error, void* customData)
{
printf("Something went wrong with your first API call.\n");
printf("Here's some debug information:\n");
printf(error.GenerateReport().c_str());
printf("\n");
finished = true;
}
int main()
{
PlayFabSettings::staticSettings->titleId = ("144");
LoginWithCustomIDRequest request;
request.CreateAccount = true;
request.CustomId = "GettingStartedGuide";
PlayFabClientAPI::LoginWithCustomID(request, OnLoginSuccess, OnLoginFail);
while (PlayFabClientAPI::Update() != 0)
sleep(1);
printf("Press enter to exit\n");
getchar();
return 0;
}
完成并执行
在 IDE 中生成并运行项目,或打开终端并运行:
./PlayFab_Test
加载时,将显示以下文本:
- “恭喜,你首次调用 API 成功!”
开始进行其他 API 调用并生成游戏。