Search Unity

Question Help getting Multiplay working

Discussion in 'Game Server Hosting' started by HaploZyorhist, Feb 21, 2023.

  1. HaploZyorhist

    HaploZyorhist

    Joined:
    Oct 17, 2016
    Posts:
    3
    Hey I am struggling with getting multiplay to work properly. Something i'm doing isn't right and i'm not sure what. If someone could help me out that would be amazing. Basically here's what i have so far.

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using Unity.Services.Core;
    4. using Unity.Services.Multiplay;
    5.  
    6. public class LoadingService : MonoBehaviour
    7. {
    8.     #region Fields
    9.  
    10.     // Publics
    11.  
    12.     // Privates
    13.     private MultiplayEventCallbacks _multiplayEventCallbacks;
    14.     private IServerEvents _serverEvents;
    15.     private IServerQueryHandler _serverQueryHandler;
    16.     private bool isInitialized = false;
    17.  
    18.     //
    19.     private const ushort _DefaultMaxPlayers = 1;
    20.     private const string _DefaultServerName = "ElemonstersServer";
    21.     private const string _DefaultGameType = "TestGameType";
    22.     private const string _DefaultMap = "TestMap";
    23.  
    24.     public ushort currentPlayers;
    25.  
    26.     #endregion
    27.  
    28.     #region Interface
    29.  
    30.     private async void Awake()
    31.     {
    32.         if (Application.isBatchMode == true)
    33.         {
    34.             StartBatchmode();
    35.         }
    36.         else
    37.         {
    38.             if (UnityServices.State == ServicesInitializationState.Uninitialized)
    39.             {
    40.                 await UnityServices.InitializeAsync();
    41.             }
    42.         }
    43.  
    44.     }
    45.     private async void Update()
    46.     {
    47.         if (isInitialized == false)
    48.         {
    49.             _serverQueryHandler = await MultiplayService.Instance.StartServerQueryHandlerAsync(
    50.                 (ushort)_DefaultMaxPlayers,
    51.                 _DefaultServerName,
    52.                 _DefaultGameType,
    53.                 Application.version,
    54.                 _DefaultMap);
    55.             isInitialized = true;
    56.         }
    57.  
    58.         _serverQueryHandler.UpdateServerCheck();
    59.  
    60.         //maybe
    61.         LogServerConfig();
    62.     }
    63.  
    64.     #endregion
    65.  
    66.     #region Privates
    67.  
    68.     private async void StartBatchmode()
    69.     {
    70.         // Setup allocations
    71.         _multiplayEventCallbacks = new MultiplayEventCallbacks();
    72.         _multiplayEventCallbacks.Allocate += OnAllocate;
    73.         _multiplayEventCallbacks.Deallocate += OnDeallocate;
    74.         _multiplayEventCallbacks.Error += OnError;
    75.         _serverEvents = await MultiplayService.Instance.SubscribeToServerEventsAsync(_multiplayEventCallbacks);
    76.  
    77.         await MultiplayService.Instance.ReadyServerForPlayersAsync();
    78.  
    79.         LogServerConfig();
    80.     }
    81.  
    82.     private void OnError(MultiplayError error)
    83.     {
    84.         LogServerConfig();
    85.         throw new NotImplementedException();
    86.     }
    87.  
    88.     private void OnDeallocate(MultiplayDeallocation deallocation)
    89.     {
    90.         LogServerConfig();
    91.         throw new NotImplementedException();
    92.     }
    93.  
    94.     private void OnAllocate(MultiplayAllocation allocation)
    95.     {
    96.         LogServerConfig();
    97.         throw new NotImplementedException();
    98.     }
    99.  
    100.     private void LogServerConfig()
    101.     {
    102.         var serverConfig = MultiplayService.Instance.ServerConfig;
    103.         Debug.Log($"Server ID[{serverConfig.ServerId}], AllocationId[{serverConfig.AllocationId}], Port[{serverConfig.Port}], QueryPort[{serverConfig.QueryPort}], LogDirectory[{serverConfig.ServerLogDirectory}]");
    104.     }
    105.  
    106.     #endregion
    107. }
    108.  
    When i try to run local i'm getting issues with it looking for the server.json file. This tells me that its not connecting to the unity servers application and thus not getting the server.json file from there.
     
  2. danri

    danri

    Unity Technologies

    Joined:
    Jun 21, 2019
    Posts:
    27
    Hello,

    The server.json file is something that is created when you run your game server on Multiplay. Running the server locally will return an error if that file does not exist.

    You have two options here:

    1. Create some logic in your code to check if you are running locally (maybe a -local flag) and then bypass the code looking for server.json when running locally
    2. Create an example server.json on your local machine. See https://docs.unity.com/game-server-hosting/en/manual/concepts/server-json for the format and file location
     
  3. HaploZyorhist

    HaploZyorhist

    Joined:
    Oct 17, 2016
    Posts:
    3
    does that mean that anytime i try to reference Multiplay.Instance it will fail? because thats the bit of code giving the error.

    Code (CSharp):
    1.         if (isInitialized == false)
    2.         {
    3.             _serverQueryHandler = await MultiplayService.Instance.StartServerQueryHandlerAsync(
    4.                 (ushort)_DefaultMaxPlayers,
    5.                 _DefaultServerName,
    6.                 _DefaultGameType,
    7.                 Application.version,
    8.                 _DefaultMap);
    9.             isInitialized = true;
    10.         }
    11.         _serverQueryHandler.UpdateServerCheck();
    this is the primary offender, when i'm trying to start the query handler.