跳到主要内容

编程快速入门

在本快速入门指南中,您将了解:

  • 如何在编程中对 PgosSDK 进行基本配置。
  • 使用 PGOS 进行编程的一般流程。
  • 如何在游戏客户端编程中使用 FakeAccountPlayerAuth进行登录鉴权。

1. 获取开发配置

在开始之前,请确保您已拥有开发者账号并在 PGOS Web Portal 中创建了游戏。之后,您可以登录门户网站获取游戏所需的配置信息:

1.1 进入 PGOS Portal 控制台

image-20260130145533331

1.2 获取 Title ID 和 Title Region ID

image-20260130150633945

1.3 获取 Secret Key

点击顶部栏的 Setting 链接,切换到 Secret Keys 选项卡,获取游戏客户端的 Secret Key。

image-20260130151446383

提示

为了降低服务器 Secret Key 泄露的风险,开发者无需为游戏服务器(Dedicated Server, DS)配置 Secret Key。PgosSDK 通过以下方式实现这一点:

  • 对于 local DS:local DS 指运行在非 PGOS Hosting 机器上的 Dedicated Server,开发者需要为DS配置 local_placer_id(可通过代码或文件配置)。该 local_placer_id 是一个加密字符串,内嵌了 secret_keytitle_region_idPgosSDK 会对其解密以获取这些配置。local_placer_id 中嵌入的 Secret Key 仅在 Dev 和 Test 的 大区(title region)有效。
  • 对于 online DS:online DS 指运行在 PGOS Hosting 机器上的 Dedicated Server,通过 Build 部署。PgosSDK 会自动从运行 DS 的 PGOS Hosting 机器检索必要配置(包括 secret_keytitle_region_id),因此开发者无需额外配置。

2. 在游戏项目中编程

以下是使用 PGOS 进行开发的一般流程,也是调用 PGOS API 的顺序:

  • For Game client:

    graph LR A(CreatePGOS) -->B[InitConfig] B --> C[LoginPGOS] C -.-> D[Call PGOS APIs in Game Client] D -.-> E(LogoutPGOS) E -.-> F(DestroyPGOS)
  • For Game Server:

    graph LR A(CreatePGOS) -->B[InitConfig] B -.-> C[Call PGOS APIs in Game Server] C -.-> D(DestroyPGOS)

下面通过游戏客户端代码为 PGOS 编程提供快速入门。游戏服务器的集成与游戏客户端类似;有关 DS Hosting 集成的详细说明,请参阅此文档

2.1 使用C++ API进行编程

2.1.1 创建和初始化 PGOS

在游戏项目的适当位置添加以下代码:

#include "PgosSDKCpp.h"

void SomeClass::StartupPGOS()
{
// You must create PGOS service before using PGOS service.
IPgosSDKCpp::Get().CreatePGOS();

// Init PGOS Config
TMap<FString, FString> Config;
Config.Add("title_id", "Your Title ID");
#if UE_SERVER
// No need to set a secret key for the server, whether it is an online DS or a local DS.
IPgosSDKCpp::Get().GetServerAPI()->InitConfig(Config);
#else
Config.Add("secret_key", "Your Client Secret Key");
IPgosSDKCpp::Get().GetClientAPI()->InitConfig(Config);
#endif
}

有关 PgosSDK 实例管理的更多详细信息,请参阅客户端 PgosSDK 实例模式

有关 PGOS 配置的更多详细信息,请参阅配置 PgosSDK

提示

建议不要在代码中硬编码原始 Secret Key,因为这会增加 Secret Key 泄露的风险。一种替代方法是硬编码一个 Secret Key 加密字符串,并在运行时进行解密。

const char* EncryptedSecretKey = "the ciphertext of the original secret key";
auto SecretKey = DecryptString(EncryptedSecretKey);
Config.Add("secret_key", SecretKey);

2.1.2 临时账号登录

在游戏项目的适当位置添加以下代码,仅在游戏客户端中有效: FPgosFakeAccountAPI::Login 接收一个 TFunction 类型的回调参数(支持 lambda),这里使用了 CreateWeakCallback 辅助函数来确保对象数据访问的安全性。

#include "PgosSDKCpp.h"

void SomeUObjectClass::LoginFakeAccount()
{
auto spFAS = IPgosSDKCpp::Get().GetClientFakeAccountAPI();
if (spFAS)
{
FString MyAccount = TEXT("SomeAccountString"); // It can be made to support getting from the input.
auto CallbackFunction = IPgosSDKCpp::CreateWeakCallback<FClientFasLoginInfo>
(this, [this](const FPgosResult& Ret, const FClientFasLoginInfo* LoginInfo) {
UE_LOG(LogTemp, Log, TEXT("On FAS login callback."));
if (Ret.err_code == (int32)Pgos::PgosErrCode::kSuccess) {
this->AccountOpenID = LoginInfo->open_id; // Store OpenID for later use.
this->AccountToken = LoginInfo->token; // Store AccountToken for later use.
}
});

spFAS->Login(MyAccount, CallbackFunction);
}
}

FPgosFakeAccountAPI::Login 接收一个 TFunction 类型的回调参数(支持 lambda),这里使用了 CreateWeakCallback 辅助函数来确保对象数据访问的安全性。

2.1.3 进行 PGOS 身份验证

将以下代码添加到游戏项目的适当位置,仅在游戏客户端中生效:

#include "PgosSDKCpp.h"

// After successful account login
void SomeUObjectClass::LoginPGOS()
{
auto playerAuth = IPgosSDKCpp::Get().GetClientPlayerAuthAPI();
if (playerAuth)
{
FClientLoginPGOSParams Params;
Params.account_provider = EAccountProvider::FAS;
Params.account_open_id = this->AccountOpenID;
Params.account_token = this->AccountToken;
Params.title_region_id = TEXT("region_id_to_login"); // Obtained from PGOS portal console.

auto CallbackFunction = IPgosSDKCpp::CreateWeakCallback<FClientAuthenticationInfo>
(this, [](const FPgosResult& Ret, const FClientAuthenticationInfo* LoginInfo) {
if (Ret.err_code == (int32)Pgos::PgosErrCode::kSuccess) {
UE_LOG(LogTemp, Log, TEXT("OnLoginPGOSSuccess: player_id=%s"), *LoginInfo->player_id);
}
else {
UE_LOG(LogTemp, Log, TEXT("OnLoginPGOSFailed: err_code=%d, err_msg=%s"), Ret.err_code, *Ret.msg);
}
});

playerAuth->LoginPGOS(Params, CallbackFunction);
}
}

关于 PGOS PlayerAuth 的更多详细信息,请参考 PlayerAuth

现在编程工作已完成,构建并运行您的游戏:

  • 如果一切正常,您将看到成功的日志。
  • 如果出现问题,请查看消息以了解发生了什么。

关于调用 PGOS C++ API 的更多详细信息,请参考 调用 PgosSDK C++ API.

2.2 使用蓝图 API 编程

使用蓝图 API 编程非常简单,您可以在可执行操作对话框中搜索以下操作:

  • PGOS | CreatePGOS
  • PGOS | Client | InitConfig
  • PGOS | Client | FakeAccount | AutoLogin
  • PGOS | Client | PlayerAuth | LoginPGOS

参考以下示例在相应的蓝图中拖放节点:

请注意,如果您想在蓝图中使用 PGOS API,即使您已经在 C++ 代码中调用过,也需要在蓝图中调用 CreatePGOS

image-20210918213209540

当您完成编程工作后,编译蓝图并运行游戏:

  • 如果一切正常,您可以看到成功的打印日志。
  • 如果出现问题,请检查消息以了解发生了什么。

关于PgosSDK实例管理的更多详情,您可以参考客户端PgosSDK实例模式

关于PGOS配置的更多详情,您可以参考配置PgosSDK

关于PGOS FAS的更多详情,您可以参考使用FAS.

关于PGOS PlayerAuth的更多详情,您可以参考PlayerAuth

关于调用PGOS蓝图API的更多详情,您可以参考调用PgosSDK蓝图API.