Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question How to notify MuiltyPlay that the game has started?

Discussion in 'Game Server Hosting' started by tony040209_unity, Dec 13, 2022.

  1. tony040209_unity

    tony040209_unity

    Joined:
    Mar 21, 2021
    Posts:
    34
    At present, after being distributed through MatchMaker, the Server will be opened
    But after an hour, the server will be shut down,

    This is very disturbing,
    So here I want to ask how to notify the server that the game has started,
    Don't automatically forcibly close the game after an hour because you didn't receive the allocation message.

    Code (CSharp):
    1. public async void Start_MuiltyPlay()
    2. {
    3.     LogServerConfig();
    4.     m_MultiplayEventCallbacks = new MultiplayEventCallbacks();
    5.     m_MultiplayEventCallbacks.Allocate += OnAllocate;
    6.     m_MultiplayEventCallbacks.Deallocate += OnDeallocate;
    7.     m_MultiplayEventCallbacks.Error += OnError;
    8.     m_MultiplayEventCallbacks.SubscriptionStateChanged += OnSubscriptionStateChanged;
    9.     m_ServerEvents = await MultiplayService.Instance.SubscribeToServerEventsAsync(m_MultiplayEventCallbacks);
    10.  
    11.     if (!string.IsNullOrEmpty(MultiplayService.Instance.ServerConfig.AllocationId))
    12.     {
    13.         await StartGame();
    14.     }
    15. }
    16.  

    Code (CSharp):
    1. public async Task StartGame()
    2. {
    3.     matchmakingResults = await MultiplayService.Instance.GetPayloadAllocationFromJsonAs<MatchmakingResults>();
    4.  
    5.     string match_id = matchmakingResults.MatchId;
    6.  
    7.     Debug.Log("Fusion Sesion: "+ match_id);
    8.  
    9.     Photon_Manager.Instance.StartServer(match_id);
    10.  
    11.     while (!Photon_Manager.Instance.isConnect)
    12.     {
    13.         await Task.Delay(250);
    14.     }
    15.  
    16.     ReadyingServer();
    17. }
    18.  

    Code (CSharp):
    1. private async void ReadyingServer()
    2. {
    3.     await MultiplayService.Instance.ReadyServerForPlayersAsync();
    4.     Debug.Log("ReadyServerForPlayersAsync");
    5. }
     
  2. danri

    danri

    Unity Technologies

    Joined:
    Jun 21, 2019
    Posts:
    27
    Hello,

    Could you provide a link to the server where you are seeing this behaviour?

    Multiplay will know that the game has started once a server is allocated. However, there is a default timeout of 1 hour on an allocation. The support team can change this value for you.

    We are evaluating ways to put the configuration of this value safely into the API and dashboard in the near future.
     
  3. tony040209_unity

    tony040209_unity

    Joined:
    Mar 21, 2021
    Posts:
    34
    @danri
    I provide the Log of the previous test,
    These two times did not receive the Allocate event,
    There should be server information in it for reference.


    Yes, because it may be required early in the game,
    Do a lot of stress testing on the server,
    It would be much more convenient if we could test without interruption, thank you~
     

    Attached Files:

  4. danri

    danri

    Unity Technologies

    Joined:
    Jun 21, 2019
    Posts:
    27
    Thanks for the extra information.

    I can see from our backend systems that your server is allocated correctly. I also see that from the latest log from server 43024964 that the server does have the correct allocation IDL


    Server ID : 43024964
    AllocationID : cef5b25f-f35e-4467-b6ed-2f491ebee819
    Port : 9000
    QueryPort : 9010
    LogDirectory : 43024964/logs


    So it seems like the allocate callback is partially working, or at least the server is able to get the allocation ID on startup from MultiplayService.Instance.ServerConfig.AllocationId.

    My guess would be that the the
    Start_MuiltyPlay
    method is the problem here. Is that method being called by another MonoBehavior
    Start 
    method somewhere else?

    We recommend that the allocate callback is set up in the
    Start
    method, so it is as early as possible in the server lifecycle. See the docs for more info:

    * https://docs.unity.com/game-server-...=2542536344#Handle_Game_Server_Hosting_events
    * https://docs.unity3d.com/ScriptReference/MonoBehaviour.Start.html
     

    Attached Files:

  5. tony040209_unity

    tony040209_unity

    Joined:
    Mar 21, 2021
    Posts:
    34
    @danri


    I am creating a MuiltyPlayManager in other code,

    MuiltyPlayManager contains the Start_MuiltyPlay( ) method,

    And it is indeed placed in Start( ),

    So MultiplayEventCallbacks are placed directly in

    Just in async void Start( )?


    Loader.cs
    12142022-10940-AM.jpg


    MuiltyPlayManager.cs

    12142022-10906-AM.jpg


    will be modified
    Code (CSharp):
    1. public async void Start()
    2. {
    3.     LogServerConfig();
    4.     m_MultiplayEventCallbacks = new MultiplayEventCallbacks();
    5.     m_MultiplayEventCallbacks.Allocate += OnAllocate;
    6.     m_MultiplayEventCallbacks.Deallocate += OnDeallocate;
    7.     m_MultiplayEventCallbacks.Error += OnError;
    8.     m_MultiplayEventCallbacks.SubscriptionStateChanged += OnSubscriptionStateChanged;
    9.     m_ServerEvents = await MultiplayService.Instance.SubscribeToServerEventsAsync(m_MultiplayEventCallbacks);
    10.  
    11.     if (!string.IsNullOrEmpty(MultiplayService.Instance.ServerConfig.AllocationId))
    12.     {
    13.         await StartGame();
    14.     }
    15. }
    16.  
     
  6. danri

    danri

    Unity Technologies

    Joined:
    Jun 21, 2019
    Posts:
    27
    Thanks for the extra information. Your code looks correct, calling Start_Multiplay from the Start method should work.

    I am going to ask the SDK development team about this, they will be able to give a more detailed answer.
     
  7. samballardadamsunity

    samballardadamsunity

    Unity Technologies

    Joined:
    Sep 3, 2021
    Posts:
    3
    I can't speak towards the 1 hour timeout, as danri mentioned.
    However, I can speak towards the potentially missed allocations:
    The code:
    Code (CSharp):
    1.     m_ServerEvents = await MultiplayService.Instance.SubscribeToServerEventsAsync(m_MultiplayEventCallbacks);
    2.  
    3.     if (!string.IsNullOrEmpty(MultiplayService.Instance.ServerConfig.AllocationId))
    4.     {
    5.         await StartGame();
    6.     }
    Reads to me that, if an allocation has not been done at startup, then "StartGame()" will not be called.
    Do you also call StartGame() inside of your OnAllocate function?