- Home /
Why does my code hang when calling a script in a multi-nested event function
I'm struggling to get my events system working in unity. I'm making an online turn based game with Nakama server.
Nakama server uses events to notify clients. I have a MatchClient.cs
script which handles connecting to the server and matchmaking. This script invokes MatchReady
(a UnityEvent) when the players have been matched by the matchmaker and the game is ready to start.
A MatchEventBroadcastManager.cs
script handles match events sent to the client by the server. Game specific events are declared in this script (Such as OnOpponentMove etc. which other scripts subscribe to) It has a MatchReady()
function that is called when MatchClient's MatchReady
event is invoked.
So far everything works correctly.
This is part of the MatchEventBroadcastManager.cs
script:
public delegate void MatchStartedEvent();
public static event MatchStartedEvent OnMatchStart;
public void MatchReady()
{
//Handle notifications from the server
_client.Socket.OnNotification += (_, notification) =>
{
switch (notification.Code)
{
case 1:
OnMatchStart?.Invoke();
break;
}
};
}
I tried to use another UnityEvent for OnMatchStart
, but it wouldn't fire any of the functions referenced in the inspector (I believe this may be something to do with nesting of events but am not certain).
This needs to be within an event as I can't subscribe to the Socket.OnNotification
event until after the players are matched (and Socket
is not null).
At this point we have:
Nakama event notifies MatchClient that the players are matched.
MatchClient notifies MatchEventBroadcastManager (among other scripts) that the match is now ready.
MatchEventBroadcastManager subscribes to the OnNotification event of the server.
When this event fires, OnMatchStart is invoked.
So far everything appears to work correctly.
Until
I have a WaitingScreenMenu.cs
script that handles the waiting screen (obviously).
It subscribes to the OnMatchStart
event and simply wants to transition to the next menu.
private void Start()
{
MatchEventBroadcastManager.OnMatchStart += MatchStarted;
Debug.Log("Menu manager: ");
Debug.Log(_menuManager.name);
}
public void MatchStarted()
{
Debug.Log("Menu manager: ");
Debug.Log(_menuManager.name);
Debug.Log("Test after");
}
I removed the transition to the next menu and just try and access an outside object to test, but this is the point execution hangs.
In the debug output when the game starts _menuManager.name
prints correctly to the console.
When MatchStarted is called "Menu manager: "
is printed, but nothing else.
If I comment out Debug.Log(_menuManager.name);
then it prints:
"Menu manager: "
"Test after"
I have tried with other scripts, with the same results. I have called the function from within one that is subscribed to an event that isn't nested and it functions correctly.
I'm attempting to separate the game logic into manageable pieces, but it seems the only way to handle the events is to have everything handled by MatchClient.cs
and have a gigantic, unwieldy mess.
Is Unity really limited in this way with regards to events or am I missing something?
Thanks for your help in advance.
Dan