Photon Cloud and Relay

Content

Setup

Bolt now has full support for Photon's industry proven matchmaking and global relay network.

The "NEW-PhotonCloud" folder contain example scripts showing how you can start using Bolt + Photon Cloud. You will be able to create Bolt servers and connect clients to it using the same API you'd use for Bolt on LAN, but using our cloud services.

First you need to install the Photon Cloud packages and the example games. Follow the next steps to accomplish the installation:

Photon Cloud configuration on Bolt Settings window
Bolt Welcome Window. (1) Bolt core scripts, (2) Other Bolt samples, needed to run Photon Cloud examples, (3) Photon Cloud binding assemblies and scripts, (4) Photon examples, (5) Package installed into your Unity Editor.

Before proceeding, please register and create an Bolt Photon App ID on Photon Engine Dashboard. You'll need to fill the App ID on "Window/Bolt/Settings", under the Photon Relay category.

Photon Cloud configuration on Bolt Settings window. Setup your App ID and Region.
Photon Cloud configuration on Bolt Settings window. Setup your App ID and Region.

Demo

On this example, we show the simplest way to use Bolt with Photon Cloud services.

Navigate to the folder "Assets/bolt_samples/NEW-PhotonCloud/demo_simple", you will find two folders:

  • "Scenes": contains a basic scene that will show the menu to create a server or client player.
  • "Scripts": folder with all scripts needed to make the demo work:
    • "PhotonInit.cs": entry point class to use Photon Cloud integration, more details below.
    • "PhotonTokens.cs": class to register some "Tokens" necessary to run the demo.
    • "PhotonServerCallbacks.cs": class that extends from Bolt.GlobalEventListener and show debug messages about the connection between server and clients, like "connect", "disconnect" and "connection requests".

Open the "PhotonInit.cs" script and take a look at some topics:

  1. Register all Tokens. Some "Tokens" are necessary to build the sample, the registration is make as usual. To know more about "Tokens" follow this link.

        public override void BoltStartBegin() // Line 20 on PhotonInit.cs
    {
        BoltNetwork.RegisterTokenClass<RoomProtocolToken>();
        BoltNetwork.RegisterTokenClass<ServerAcceptToken>();
        BoltNetwork.RegisterTokenClass<ServerConnectToken>();
        ...
    }
  2. Configure Bolt to use the Photon Cloud platform. This will setup Bolt to use Photon as a relay service in order to publish rooms to the cloud and listen for updates.

        void Awake() // Line 30 on PhotonInit.cs
    {
        // Set Bolt to use Photon as transport layer
        // this will connect to Photon using configuration values from Bolt's settings window
        BoltLauncher.SetUdpPlatform(new PhotonPlatform());
    
    
        // OR
    
    
        // You may want to configure the Photon transport layer programatically:
        BoltLauncher.SetUdpPlatform(new PhotonPlatform(new PhotonPlatformConfig
        {
            AppId = "<your-app-id>",
            RegionMaster = "<your-region>",
            UsePunchThrough = false // set to true, to use PunchThrough
        }));
    }
  3. Starting Bolt. Initialize the player as usual:

        // Line 53 on PhotonInit.cs
    if (GUILayout.Button("Start Server", GUILayout.ExpandWidth(true)))
    {
        BoltLauncher.StartServer(); // Start as Server
        ...
    }
    if (GUILayout.Button("Start Client", GUILayout.ExpandWidth(true)))
    {
        BoltLauncher.StartClient(); // Start as Client
        ...
    }
  4. Create and publish a room. As a server player, you can create a room and publish to the cloud, later the clients will be able to connect to it.

        // Line 71 on PhotonInit.cs
    ...
    if (GUILayout.Button("Publish HostInfo And Load Map", GUILayout.ExpandWidth(true)))
    {
        // You can setup a custom Token with some properties
        RoomProtocolToken token = new RoomProtocolToken()
        {
            ArbitraryData = "My DATA",
            password = "mysuperpass123"
        };
    
    
        ... 
        string matchName = "MyPhotonGame #" + UnityEngine.Random.Range(1, 100);
    
    
        BoltNetwork.SetServerInfo(matchName, token); // This call will create the room at Photon Cloud
        BoltNetwork.LoadScene("Level1"); // Load you in-game scene
    }
    ...
  5. List all available rooms to connect. As a client, you can get the list of rooms, the information associated with it and try to connect to it's server.

        ...
    // Line 98 on PhotonInit.cs
    GUILayout.Label("Session List");
    
    
    // This list is updated automatically every time a room is
    // created or deleted from the cloud server, no need to request
    // updates
    foreach (var session in BoltNetwork.SessionList)
    {
        // Simple session
        UdpSession udpSession = session.Value as UdpSession;
    
    
        // Skip if is not a Photon session
        if (udpSession.Source != UdpSessionSource.Photon)
            continue;
    
    
        // Photon Session
        PhotonPoller.PhotonSession photonSession = udpSession as PhotonPoller.PhotonSession;
    
    
        // Short description of the room
        string sessionDescription = String.Format("{0} / {1} ({2})", photonSession.Source, photonSession.HostName, photonSession.Id);
    
    
        // This will get the same Token passed by the server when creating
        // the room. You will be able to get all associated data from it.
        RoomProtocolToken token = photonSession.GetProtocolToken() as RoomProtocolToken;
    
    
        if (token != null)
        {
            sessionDescription += String.Format(" :: {0}", token.ArbitraryData);
        } 
        ...
        if (GUILayout.Button(sessionDescription, GUILayout.ExpandWidth(true)))
        {
            // Create a connection Token, that can be used by the client
            // to pass some data to the server, or even filter clients
            // from a password protect room
            ServerConnectToken connectToken = new ServerConnectToken
            {
                data = "ConnectTokenData"
            };
    
    
            // Connect to the room as usual
            // If the server accepts the client, it will be redirected
            // to the current loaded scene on the server player
            BoltNetwork.Connect(photonSession, connectToken);
        }
    }

Playing the Game

After you know what to do in order to make Bolt run with Photon Cloud, we will run the current demo code. Follow the next instructions to run the game.

  1. Compile Bolt. Before anything, you need to compile Bolt to make it aware of all yours Prefabs/Entities. Please run "Assets/Bolt/Compile Assembly".
  2. Setup the scenes. Go to "File -> Build Settings" and in the "Scenes in Build" drag and drop two scenes:

    • (0) "Assets/bolt_samples/NEW-PhotonCloud/demo_simple/Scenes/PhotonTutorial"
    • (1) "Assets/bolt_samples/AdvancedTutorial/scenes/Level1"

    Scenes Setup
    Scenes Setup.

  3. Build the player. Go ahead and build a standalone version of the game.

  4. Running the players. Open two instances of the game.

    • On the first one click on "Start Server", then "Publish HostInfo And Load Map". This will load the game scene and publish the room to the cloud.
    • On the other player, click on "Start Client", then wait for a few seconds, then the list of rooms will be updated and you will get one room listed with some descriptive information. Click on the room's name, the client should connect to server and load the in-game scene.
    • Have fun.

    Game running
    Game running.

Support

If you have any inquiry on this, contact us through our support channels:

 To Document Top