Skip to main content

Programming Quick Start

In this quick start guide, you will learn:

  • How to make a basic configuration of PgosSDK in programming.
  • How to use some basic PGOS services in programming. (Fake Account and PlayerAuth)

We will introduce you in detail according to the following process:

  • Retrieve the development configurations of your title from the portal.
  • Programming in your game project.
    • Create PGOS service and init PGOS config.
    • Make a fake account log in with Fake Account service.
    • Login PGOS to make PGOS authentication with PlayerAuth service.

1. Retrieve the Development Configurations

Before doing this, please make sure you have a developer account and a title in PGOS Web Portal.

After that, you can log in to the protal to retrieve the necessary configurations for your title:

1.1 Enter PGOS Portal Console

image-20230316103611785

1.2 Retrieve the Title ID and Title Region ID

image-20230316104409563

1.3 Retrieve the Secret Key

Click the Setting link, switch to Secret Keys tab, retrieve the Secret Key for game client.

image-20241113152752888

2. Programming in Game Project

The following is the general process of programming with PGOS, and it is also the sequence of calling PGOS APIs:

  • 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)

2.1 Programming with C++ API

The following uses the game client code as an example to demonstrate a quick start on how to use PGOS C++ API programming:

2.1.1 Create and Init PGOS

Add the following codes to the appropriate location of your game project:

#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
}

Call CreatePGOS() function in the main thread before using any PGOS service.

Call DestroyPGOS() function in the main thread during process shutdown if possible.

For more detail about PgosSDK instance management, you can refer to Client PgosSDK Instance Mode

For more detail about PGOS config, you can refer to Configure the PgosSDK.

tip

It is recommended not to hardcode the original secret key in the code as it increases the risk of secret key leakage. An alternative approach is to hardcode a secret key encryption string and decrypt it at runtime.

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

2.1.2 Make a Fake Account Log in

Add the following codes to the appropriate location of your game project, only works in game client:

#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 receives a callback parameter of type TFunction (supporting lambda), and a CreateWeakCallback helper function is used here to ensure the security of object data access.

2.1.3 Make PGOS Authentication

Add the following codes to the appropriate location of your game project, only works in game client:

#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);
}
}

For more detail about PGOS PlayerAuth, you can refer to PlayerAuth.

Now that the programming work is complete, build and run your game:

  • If everything is working fine, you can see the successful log.
  • If something is wrong, check the msg to see what happened.

For more detail about Calling PGOS C++ API, you can refer to Call PgosSDK C++ API.

2.2 Programming with Blueprint API

Programming with blueprint API is quite simple, you can search the following actions in the Executable Actions dialog:

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

Refer to the example below to drag and drop nodes on the appropriate blueprint:

Do remember to call CreatePGOS in the blueprint if you want to use PGOS API in the blueprint, even if you have already called it in the C++ code.

image-20210918213209540

When your programming work is complete, compile the blueprint and run your game:

  • If everything is working fine, you can see the successful print log.
  • If something is wrong, check the msg to see what happened.

For more detail about PgosSDK instance management, you can refer to Client PgosSDK Instance Mode

For more detail about PGOS config, you can refer to Configure the PgosSDK.

For more detail about PGOS FAS, you can refer to Using FAS.

For more detail about PGOS PlayerAuth, you can refer to PlayerAuth.

For more detail about Calling PGOS Blueprint API, you can refer to Call PgosSDK Blueprint API.