Switching Running Bolt as Client to as Server

Options
So I want to build a P2P 1v1 game and am trying to figure out how to match folks together. My plan is to have something as follows:

1. Try to connect to a Session already created as a Client
2. If this fails then become a server and create a session
3. Wait for a client connection then load whatever I need

For now I am just going to match the first two people as they come then later I'll work to handle different cases. Here is my logic so far.

1. BoltLauncher.StartClient();
2. In BoltStartDone I call BoltMatchmaking.JoinRandomSession();
3. Assuming a failure, I enter SessionConnectFailed(session, token).

Do I stop Bolt from running somehow then restart as a Server? Thanks for reading!

Comments

  • ShuntBalushian
    Options
    That was pretty helpful, but I am not certain how I can remove the need for the player to have to choose between being a server or a client.

    I want to abstract that decision-making for the player and handle it myself. If the example had something in there to help with this, my apologies I missed it. But from my understanding this just uses a GUI and has the player choose to start as Client or Server, with no way to switch between being either.

    Thanks for the reply seriously this forum is great.
  • ShuntBalushian
    edited May 2020
    Options
    Ooooh is it the part about the Headless instance? I'm pretty new to network programming, but it seems like this is a server that I can start up at the beginning of my game to handle all the logic of connecting since I can have access to all my current sessions? I think that's worth a shot unless I am a complete idiot
  • stanchion
    Options

    The headless part is just for testing. You run a bunch at the same time, they automatically all match up.

  • ShuntBalushian
    Options
    Right I have no problem connecting a client to a server. My problem is that the player makes the choice to start the game as a server or client.

    I don't want a GUI where the player can choose, I want a button like "Play Online" then my network logic decides whether this instance should be a Client or Server based on the number of current sessions with less than 2 players.
  • ShuntBalushian
    Options
    Here's the code I have right now that I figured should work just fine. The only problem is that when I do this, it stops SceneLoadLocalDone in my Character_Select_Screen scene from triggering.
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using Bolt;
    using Bolt.Matchmaking;
    using UdpKit;
    using UnityEngine;
    
    public class Play_Online : GlobalEventListener
    {
        public GameObject connection;
        
        void OnGUI()
        {
            if (GUILayout.Button("Play_Online", GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true)))
            {
                SpriteRenderer[] connects = connection.GetComponentsInChildren<SpriteRenderer>();
                foreach (SpriteRenderer s in connects)
                {
                    s.enabled = true;
                }
                BoltLauncher.StartClient();
            }
        }
    
        public override void BoltStartDone()
        {
            if (BoltNetwork.IsClient)
            {
                BoltMatchmaking.JoinRandomSession();
            }
            else if (BoltNetwork.IsServer)
            {
                //Debug.Log("Started as Server");
                string matchName = Guid.NewGuid().ToString();
    
                BoltMatchmaking.CreateSession(
                    sessionID: matchName,
                    sceneToLoad: "Character_Select_Screen"
                );
            }
        }
    
        public override void SessionConnectFailed(UdpSession session, IProtocolToken token)
        {
            BoltLauncher.Shutdown();
            BoltLauncher.StartServer();
            //BoltLog.Error("Failed to connect to session {0} with token {1}", session, token);
        }
    
    }
    
  • ramonmelo
    Options
    Hello @ShuntBalushian ,

    We already replied to your other post here: https://forum.photonengine.com/discussion/16084/sceneloadlocaldone-not-being-called-but-sceneloadlocalbegin-is-being-called#latest

    Please, take a look at the sample at this link: https://github.com/BoltEngine/Bolt-Sample/tree/master/Sample_1x1

    It can give you ideas of how to archive what you are looking for.
    If you are new to network programming and new to Photon Bolt, we recommend that you read our sample documentations and try to make small projects to test your ideas.