Game Engine

how to initialize GS2-SDK for Game Engine

Profile

Initialization of the SDK is done through a utility class called Profile. Profile is used to initialize both the always-connected session used for notifications and the HTTP session used for API calls, as well as to automatically handle miscellaneous processes such as session disconnection and access token expiration.

Request

Argument NameDescription
clientIdCredential Information
clientSecretCredential Information
regionGS2 region to connect to
reopenerReconnect process
distributorNamespaceNameNamespace name of the GS2-Distributor performing the transaction processing

The basic reopener is Gs2BasicReopener. This class defines the general reconnection process assumed by GS2.

Although omitted in the example implementation below, you can specify distributorNamespaceName here to use any GS2-Distributor’s namespace for transaction processing. If you want GS2-Distributor to record the transaction logs it executes in GS2-Log, you must explicitly set the namespace in which the logs are exported here.

Gs2BasicReopener Request

Argument NameDescription
gatewaySettingNotification settings
versionSettingVersion check settings

Gs2BasicReopener requires two arguments to be set as needed.

gatewaySetting

This setting is for receiving push notifications from the server.

When automatically executing transactions (stamp sheets) on the server side or using the matchmaking function, setting this item will enable you to receive notifications when the process is complete. If you are developing a game that utilizes GS2, it may be an essential item to configure. Set the namespace of the GS2-Gateway to be used for notification processing.

versionSetting

This is a setting to automatically perform an app version check when the session is reconnected.

By performing a version check when reconnecting to a session, you can force players to perform a version check at regular intervals. Using GS2-Gateway’s session disconnection API, it is possible to force version check on all playing players and kick out players who are playing with older versions.

This feature also implements credential escalation after the version check. This is the handling process that GS2-Version has for assigning a new credential after passing a version check. The credentials embedded in the game will have only the minimum privileges necessary to perform the version check, and can be promoted to sufficient privileges to play the game after passing the version check.

Implementation Example

    var profile = new Profile(
        clientId,
        clientSecret,
        region: Region.ApNorthEast1,
        reopener: new Gs2BasicReopener(
        )
    );
    var gameSession = await profile.LoginAsync(
        new Gs2AccountAuthenticator(
            session: profile.Gs2RestSession,
            accountNamespaceName: "namespace-0001",
            keyId: "grn:gs2:{region}:{yourOwnerId}:key:namespace-0001:key:key-0001",
            userId: userId,
            password: password,
            gatewaySetting : new GatewaySetting {
                gatewayNamespaceName = gatewayNamespaceName,
                allowConcurrentAccess = true
            },
            versionSetting : new VersionSetting {
                versionNamespaceName = versionNamespaceName,
                targetVersions = new []
                {
                    new EzTargetVersion
                    {
                        VersionName = "app",
                        Version = new EzVersion
                        {
                            Major = 1,
                            Minor = 0,
                            Micro = 0
                        }
                    }
                }
            }
        )
    );
    var profile = new Profile(
        clientId,
        clientSecret,
        region: Region.ApNorthEast1,
        reopener: new Gs2BasicReopener(
        )
    );
    var future = await profile.LoginFuture(
        new Gs2AccountAuthenticator(
            session: profile.Gs2RestSession,
            accountNamespaceName: "namespace-0001",
            keyId: "grn:gs2:{region}:{yourOwnerId}:key:namespace-0001:key:key-0001",
            userId: userId,
            password: password,
            gatewaySetting : new GatewaySetting {
                gatewayNamespaceName = gatewayNamespaceName,
                allowConcurrentAccess = true
            },
            versionSetting : new VersionSetting {
                versionNamespaceName = versionNamespaceName,
                targetVersions = new []
                {
                    new EzTargetVersion
                    {
                        VersionName = "app",
                        Version = new EzVersion
                        {
                            Major = 1,
                            Minor = 0,
                            Micro = 0
                        }
                    }
                }
            }
        )
    );
    yield return future;
    if (future.Error != null)
    {
        OnError(future.Error);
        yield break;
    }

    gameSession = future.Result;
    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(
            MakeShared<Gs2::UE5::Util::FGatewaySettingPtr>(
                gatewayNamespaceName
            ),
            MakeShared<Gs2::UE5::Util::FVersionSettingPtr>(
                versionNamespaceName,
                []{
                    TArray<Gs2::UE5::Version::Model::FEzTargetVersionPtr> Arr;
                    Arr.Add(MakeShared<Gs2::UE5::Version::Model::FEzTargetVersion>()
                        ->WithVersionName(TOptional<FString>("app"))
                        ->WithVersion(
                            MakeShared<Gs2::UE5::Version::Model::FEzVersion>()
                                ->WithMajor(1)
                                ->WithMinor(0)
                                ->WithMicro(0)
                        )
                    );
                    return Arr;
                }()
            )
        ))
    );

Initialization of SDK

Once the profile object is created, initialize GS2-SDK. By performing the initialization process, a GS2 client instance can be obtained.

Implementation Example

    var gs2 = await profile.InitializeAsync();
    var initializeFuture = profile.InitializeFuture();
    yield return initializeFuture;
    if (initializeFuture.Error != null) {
        throw initializeFuture.Error;
    }
    var gs2 = initializeFuture.Result;
    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();