LogoLogo
HomeOnline DashboardAPIDiscordForums
SDK 0.4.14
SDK 0.4.14
  • Welcome
  • Overview
    • What is coherence?
    • How does coherence work?
    • Features and Roadmap
    • Requirements
  • Get Started
    • Install coherence
    • Scene setup
    • Prefab setup
    • Build and run
    • Baking and code generation
    • Create a free account
    • Deploy and share
  • Authority and communication
    • How authority works
    • Authority transfer
    • Commands
    • Server-side and input queues
    • Animations
  • Persistence
    • Overview
    • Configuring persistence
    • Storage
    • Example – A global counter
  • Optimization
    • Overview
    • Simulation frequency
    • Areas of interest
    • World size
    • Level of detail
    • Interpolation
    • Extrapolation
  • Connected entities
    • Overview
    • Entity references
    • ConnectedEntity component
    • Parent-child relationships
  • Simulators
    • Overview
    • Client vs. simulator logic
    • Build and deploy
    • Simulator load balancing
  • Tutorial project
    • Get the Tutorial Project
    • Start Tutorial
      • 1. Transforms
      • 2. Physics
      • 3. Persistence
      • 4. Animation and Variables
      • 5. AI Navigation
      • 6. Network Commands
      • 7. Network Events
  • Game Services
    • Game account
    • Key-value store
    • Matchmaking
  • API reference
    • Network SDK
      • CoherenceSync
      • MonoBridge
      • LiveQuery
      • Archetype
      • Sample UI
      • Settings Window
    • Cloud API
      • API tokens and keys
      • Server discovery
      • Game account
      • Key-value store
      • Matchmaking
    • Replication Server
    • Simulation Server
    • Entity Component System
      • Network Entities and Components
  • Schema reference
    • Overview
    • Specification
    • Field Settings
    • Archetypes and LOD-ing
  • Resources
    • Downloads
    • Video Tutorials
    • Glossary
    • CLI Utilities
    • Helper Scripts
    • Troubleshooting
  • Community
    • Discord
  • Additional information
    • Pricing
    • SLA
    • Unreal Engine support
    • Peer-to-Peer (P2P)
    • Known Issues
    • Version History
Powered by GitBook
On this page
  • Initialization
  • Callbacks
  • Guest Account
  • User Account
  • Example

Was this helpful?

Export as PDF
  1. API reference
  2. Cloud API

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.

Last updated 3 years ago

Was this helpful?

Initialization

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

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,
    }
       
    // 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.Play;

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));
        }
    });
}
settings
Runtime Key