Unity Multiplay Service gets stuck in OnAllocate at GetPayloadAllocation

Hello,

im currently having a problem when using netcode for gameobjects and matchmaker. A few weeks ago everything was working fine and i could connect to the server. But suddenly it stopped working. After investigating i found the error. In the OnAllocate function

m_MultiplayEventCallbacks = new MultiplayEventCallbacks();
m_MultiplayEventCallbacks.Allocate += OnAllocate;

i try to get the result of the allocation with

string payload = MultiplayService.Instance.GetPayloadAllocationAsPlainText().Result;

I added a debug.log before and after this statement. The server reaches the first debug.log but never the second. So that means it is stuck at this statement. After that i added a for loop that logs the status of the task

for (int i = 0; i < 1000; i++)
{
    Debug.Log(test.Status);
    System.Threading.Thread.Sleep(100);
    if (test.IsCompleted)
    {
        payload = test.Result;
        i = 100000;
    }
}

It prints: WaitingForActivation

I tried to rearrange the order of the initialization options but it did not work. After removing this statement everything went on fine and crashed later, because the payload was empty which is perfectly fine, because my game needs this information.

I do the following things:

if (UnityServices.State == ServicesInitializationState.Uninitialized)
    await UnityServices.InitializeAsync();

m_MultiplayEventCallbacks = new MultiplayEventCallbacks();
m_MultiplayEventCallbacks.Allocate += OnAllocate;
m_MultiplayEventCallbacks.Deallocate += OnDeallocate;
m_MultiplayEventCallbacks.Error += OnError;
m_MultiplayEventCallbacks.SubscriptionStateChanged += OnSubscriptionStateChanged;
m_ServerEvents = await MultiplayService.Instance.SubscribeToServerEventsAsync(m_MultiplayEventCallbacks);

NetworkManager.Singleton.NetworkConfig.ConnectionApproval = true;
NetworkManager.Singleton.ConnectionApprovalCallback += OnConnectionApprovalCallback;
NetworkManager.Singleton.OnClientConnectedCallback += OnClientConnectCallback;
NetworkManager.Singleton.OnClientDisconnectCallback += OnClientDisconnectCallback;

m_ServerQueryHandler = await MultiplayService.Instance.StartServerQueryHandlerAsync(UnityNetworkData.DEFAULT_MAX_PLAYERS, UnityNetworkData.DEFAULT_SERVER_NAME, UnityNetworkData.DEFAULT_GAME_TYPE, UnityNetworkData.DEFAULT_BUILD_ID, UnityNetworkData.DEFAULT_MAP);

ServerConfig serverConfig = MultiplayService.Instance.ServerConfig;
if (serverConfig.AllocationId == "") return;
     NetworkManager.Singleton.GetComponent<Unity.Netcode.Transports.UTP.UnityTransport>().SetConnectionData("0.0.0.0", serverConfig.Port, "0.0.0.0");

NetworkManager.Singleton.StartServer()

Is there anything missing, am i doing something wrong or has something changed i don’t know about?

Thank you for your help.

Hi! One thing I can suggest is trying to get the payload allocation using GetPayloadAllocationFromJsonAs(). See as an example the MatchPlay sample here

Hi. I tried it but with the same result.

This

Debug.Log("TestA");
Task<MatchmakingResults> task = MultiplayService.Instance.GetPayloadAllocationFromJsonAs<MatchmakingResults>();
Debug.Log(task);
Debug.Log(task.Status);
Debug.Log("TestB");
string value = task.Result.ToString();
Debug.Log(value);
Debug.Log("TestC");

results in

TestA
System.Threading.Tasks.Task`1[Unity.Services.Matchmaker.Models.MatchmakingResults]
WaitingForActivation
TestB

I found my problem. The way i handled the allocation was wrong or the order of the initialization. After finding this example

and reorganizing my code it started to work again.

When you got a returned Task, it just created without running.
You have to Run()、Start() or “await” it… otherwise, it just blocks the thread while you’re checking if it completed…