Full sample code
Sample code to implement everything from account issuance to login
// Setup variables
var clientId = "YourClientId";
var clientSecret = "YourClientSecret";
var accountNamespaceName = "game-0001";
// Setup general setting
Gs2Domain gs2;
{
var future = Gs2Client.CreateFuture(
new BasicGs2Credential(
this.clientId,
this.clientSecret
),
Region.ApNortheast1
);
yield return future;
if (future.Error != null) {
throw future.Error;
}
gs2 = future.Result;
}
// Setup variables
const auto ClientId = "YourClientId";
const auto ClientSecret = "YourClientSecret";
const auto AccountNamespaceName = "game-0001";
const auto AccountEncryptionKeyId = "grn:gs2:{region}:{ownerId}:key:account-encryption-key-namespace:key:account-encryption-key";
// Setup general setting
const auto Profile = MakeShared<Gs2::UE5::Util::FProfile>(
ClientId,
ClientSecret,
Gs2::Core::Model::ERegion::ApNorthEast1,
MakeShareable<Gs2::UE5::Util::IReOpener>(new Gs2::UE5::Util::FGs2BasicReOpener())
);
// Create GS2 client
const auto InitializeFuture = Profile->Initialize();
InitializeFuture->StartSynchronousTask();
if (InitializeFuture->GetTask().IsError())
{
UE_LOG(GameLog, Error, TEXT("%s"), ToCStr(InitializeFuture->GetTask().Error()->String()));
return InitializeFuture->GetTask().Error();
}
const auto Gs2 = InitializeFuture->GetTask().Result();
First, three variables are defined.
Variable name | Usage | Source of acquisition |
---|---|---|
clientId | authentication information to access GS2 | value created in Create credential (API key) |
clientSecret | authentication information for accessing GS2 | value created by create credential (API key) |
accountNamespaceName | the namespace name of the GS2-Account to be used | the value created in Prepare resources required for login process |
CreateFuture(Gs2Client.CreateAsync when using UniTask) to create a GS2 client.
If clientId
or clientSecret
is incorrect, an error is returned here.
Initialize a GS2 client.
If clientId
or clientSecret
is incorrect, an error will be returned here.
// define GS2-Account namespace
var gs2Account = gs2.Account.Namespace(
accountNamespaceName
);
// Create an anonymous account
EzAccount account;
{
Debug.Log("Create an anonymous account");
var future = gs2Account.CreateFuture();
yield return future;
if (future.Error != null) {
throw future.Error;
}
var future2 = future.Result.ModelFuture();
yield return future2;
if (future2.Error != null) {
throw future2.Error;
}
account = future2.Result;
}
// Create an anonymous account
UE_LOG(GameLog, Display, TEXT("Create anonymous account"));
const auto CreateFuture = Gs2->Account->Namespace(
AccountNamespaceName
)->Create();
CreateFuture->StartSynchronousTask();
if (CreateFuture->GetTask().IsError())
{
UE_LOG(GameLog, Error, TEXT("%s"), ToCStr(CreateFuture->GetTask().Error()->String()));
return CreateFuture->GetTask().Error();
}
// Load created account
const auto LoadFuture = CreateFuture->GetTask().Result()->Model();
LoadFuture->StartSynchronousTask();
if (LoadFuture->GetTask().IsError())
{
UE_LOG(GameLog, Error, TEXT("%s"), ToCStr(LoadFuture->GetTask().Error()->String()));
return LoadFuture->GetTask().Error();
}
const auto Account = LoadFuture->GetTask().Result();
// Dump anonymous account
UE_LOG(GameLog, Display, TEXT("UserId: %s"), ToCStr(*Account->GetUserId()));
UE_LOG(GameLog, Display, TEXT("Password: %s"), ToCStr(*Account->GetPassword()));
In this sample, a new account is created at every startup, but normally, an account is created only at the first startup of the application. The actual application saves this response account information in local storage, etc., and logs in as an existing game player using the created account information retrieved from local storage, etc., from the second time onward.
// Log-in created anonymous account
GameSession gameSession;
{
var future = gs2.LoginFuture(
new Gs2AccountAuthenticator(
accountSetting: new AccountSetting {
accountNamespaceName = this.accountNamespaceName,
}
),
account.UserId,
account.Password
);
yield return future;
if (future.Error != null) {
throw future.Error;
}
gameSession = future.Result;
}
// Log-in created anonymous account
const auto LoginFuture = Profile->Login(
MakeShareable<Gs2::UE5::Util::IAuthenticator>(
new Gs2::UE5::Util::FGs2AccountAuthenticator(
AccountNamespaceName,
AccountEncryptionKeyId
)
),
*Account->GetUserId(),
*Account->GetPassword()
);
LoginFuture->StartSynchronousTask();
if (LoginFuture->GetTask().IsError())
{
UE_LOG(GameLog, Error, TEXT("%s"), ToCStr(LoginFuture->GetTask().Error()->String()));
return LoginFuture->GetTask().Error();
}
const auto GameSession = LoginFuture->GetTask().Result();
The resulting GameSession object is returned. From now on, when calling GS2 APIs, GameSession will be passed as an argument for APIs that can only be called if the user is logged in.
// Load TakeOver settings
{
var it = gs2Account.Me(
gameSession
).TakeOvers();
while (it.HasNext()) {
yield return it.Next();
if (it.Error != null)
{
throw it.Error;
}
Debug.Log($"Slot: {it.Current.Slot}");
Debug.Log($"Identifier: {it.Current.UserIdentifier}");
}
}
// Load TakeOver settings
const auto It = Gs2->Account->Namespace(
AccountNamespaceName
)->Me(
GameSession
)->TakeOvers();
for (const auto TakeOver : *It)
{
UE_LOG(GameLog, Display, TEXT("Type: %s"), *TakeOver->GetType());
UE_LOG(GameLog, Display, TEXT("Identifier: %s"), *TakeOver->GetUserIdentifier());
}
As an example of an API that can only be used while logged in, we call the API to get a list of takeover settings. You can pass GameSession to get a list of handover settings set for the currently logged-in game player.
// Finalize GS2-SDK
{
var future = gs2.DisconnectFuture();
yield return future;
if (future.Error != null) {
throw future.Error;
}
}
// Finalize GS2-SDK
const auto FinalizeFuture = Profile->Finalize();
FinalizeFuture->StartSynchronousTask();
if (FinalizeFuture->GetTask().IsError())
{
UE_LOG(GameLog, Error, TEXT("%s"), ToCStr(FinalizeFuture->GetTask().Error()->String()));
return FinalizeFuture->GetTask().Error();
}
Terminate the connection with GS2.
[Check out our YouTube channel for guidance on how to use GS2!] (/articles/sample/youtube/)
[We also provide more practical examples] (/articles/sample/project/)
find out what else you can achieve with GS2
learn about the development workflow using GS2
Sample code to implement everything from account issuance to login