Game account

Creating a player account is the first step towards using the coherence Cloud API. It is required in order to use the rest of the services.

Initialization

To initialize the Cloud API you need to provide a Runtime Key that can be obtained from the settings.

public static class Play
{
    // Initialises the Cloud API
    public static void Init(string RuntimeKey)
}

Callbacks

public static class Auth
{
    public enum Result
    {
        // Successful operation.
        Success,
        
        // Network error, e.g. connection failed.
        NetworkError,
        
        // Server error, e.g. exception on the server.
        ServerError,
        
        // User not found, e.g. when trying to login with non-existing user
        UserNotFound,
        
        // Invalid credentials, e.g. when trying to login with wrong password
        InvalidCredentials,
        
        // Session has expired, e.g. when trying to renew an existing session
        SessionExpired,
        
        // Feature is not enabled, e.g. K/V store
        FeatureDisabled,
    }
       
    // Invoked when completing the login process.
    public delegate void OnSession(Result result);
}

Guest Account

The easiest way to get started is by using a guest account. The only thing that is needed is to call LoginAsGuest. This will create a random username / password combination and will authenticate the player with the coherence Cloud.

Once logged in, the credentials are securely persisted so if the game is restarted the player will be able to log in automatically.

If the game is uninstalled then the account credentials will be lost and a new guest account will be created next time the game is installed.

public static class Auth
{
    // Authenticates the players as a guest.
    // callback - invoked upon completion.
    public static void LoginAsGuest(OnSession callback)
}

User Account

Another alternative is to login with a username and a password. You have to provide the user interface.

public static class Auth
{
    // Authenticates the player with a username and a password.
    // userName   - any combination of letters, numbers, underscore and dash.
    // password   - any combination of ASCII characters
    // autosignup - whether to create an account automatically if it
    //              doesn't exist and the username is not taken
    // callback   - invoked upon completion
    public static void LoginAsUser(string userName, string password,
        bool autosignup, OnSession callback)
}

Example

This example initializes the Cloud API, checks for an existing session and, if no session was found or if it expired, logs in the player as guest.

using Play = Coherence.Runtime;

void OnLogin()
{
    Debug.Log(string.Format("Logged in. PlayerId={0}", Play.Auth.UserId));
}

void Start()
{
    // Initialise the Cloud API
    Play.Play.Init(apiToken);
    
    // Check for existing session
    Play.Auth.IsLoggedIn((res) => {
        if (res == Play.Result.Success)
        {
            // Session resumed
            OnLogin();
        }
        else if (res == Play.Result.SessionExpired || res == Play.Result.UserNotFound)
        {
            // No session found, or session expired. Proceed with normal login.
            Play.Auth.LoginAsGuest((res2) =>
            {
                if (res2 == Play.Result.Success)
                {
                    OnLogin();
                    return;
                }
                UnityEngine.Debug.LogError(string.Format("Could not login. Error={0}", res2));
            });
        }
        else
        {
            UnityEngine.Debug.LogError(string.Format("Could not login. Error={0}", res));
        }
    });
}

Last updated