Call PgosSDK APIs
PgosSDK
provides a set of APIs for accessing PGOS services. Some APIs need to be called before others, that is, you need to call CreatePGOS
, InitConfig
, LoginPGOS
first, which will prepare the working environment for other API calls. Click Programming Quick Start for details.
The
FakeAccount
interface APIs can be called beforeLoginPGOS
, theFakeAccount
interface is used for FAS login.
After completing this tutorial, you will learn:
- How to call
PgosSDK
API.
1. Get Service Interfaces
All service interfaces can be found in PgosClientSDK.cs
and PgosServerSDK.cs
:
// PgosClientSDK.cs
// Client API
public class PgosClientSDK
{
/** Get PgosClientSDK singleton instance */
public static PgosClientSDK Get();
/** Create and Destroy PGOS in the main thread */
public void CreatePGOS();
public void DestroyPGOS();
public Client.ClientSDK GetClientSDKAPI(); // Client global interface
public Client.Economy GetEconomyAPI(); // Economy interface
public Client.FakeAccount GetFakeAccountAPI(); // FakeAccount interface
public Client.PlayerAuth GetPlayerAuthAPI(); // PlayerAuth interface
...
}
// PgosServerSDK.cs
// Server API
public class PgosClientSDK
{
/** Get PgosServerSDK singleton instance */
public static PgosServerSDK Get();
/** Create and Destroy PGOS in the main thread */
public void CreatePGOS();
public void DestroyPGOS();
public Server.ServerSDK GetServerSDKAPI(); // Server global interface
public Server.Battle GetBattleAPI(); // Battle interface
public Server.Economy GetEconomyAPI(); // Economy interface
public Server.Hosting GetHostingAPI(); // Hosting interface
...
}
To obtain the corresponding service instance, just call the corresponding Get*API()
function. For example, the following code will obtain the service instance of FakeAccount
:
var clientAPI = PgosClientSDK.Get().GetClientSDKAPI();
After getting the service instance, you can call all its service APIs, which can be found in the file "BusinessModules/Pgos{ServiceName}API.cs"
.
2. Make API Call
There are synchronous and asynchronous APIs in PgosSDK
, generally speaking, APIs with callback parameters are asynchronous, and APIs without callback parameters are synchronous. Asynchronous API callbacks are executed in Game Thread. As an example, the following is a API declaration for the FAS login function:
public delegate void OnLogin(PgosResult ret, FasLoginInfo data);
/**
* Log in with the specified account.
* The FAS account does not need to be registered in advance, it can be any string without line breaks ('\r', '\n') and is managed by the game.
*
* @param account FAS account, if the account is empty, then will use a local historical account.
* @param callback The result callback after the API execution ends, and it will be called in the GAME THREAD. For lifetime safety, it is recommended to use the CreateWeakCallback provided in PgosSDKCpp.h to create a lambda bound to an UObject.
*/
public void Login(string account, OnLogin callback)
{
...
}
3. Monitor Events
PgosSDK
will trigger certain events when certain things happen, such as when the player session status changes, when a player joins/leaves the Party, and so on. These events are executed in Game Thread. Developers can monitor these events and take appropriate actions (such as updating the UI). Take the OnPlayerSessionChanged
event of the PlayerAuth
service as an example, you can refer to the following code to monitor the PgosSDK
event:
void MonitorPlayerSessionEvt()
{
var playerAuthAPI = PgosClientSDK.Get().GetPlayerAuthAPI();
playerAuthAPI.SetOnPlayerSessionChanged((Pgos.Client.PlayerSessionChangedEvt pgos_event) =>
{
Debug.Log("pgos player session has changed.");
});
}
❗ Note
- To avoid missing events, it is recommended to monitor them before they are triggered.
- Both of asynchronous API callbacks and triggered events are executed in Game Thread, so it is not recommended to do any time-consuming actions in them.