refactored scripts to avoid execution order issues

This commit is contained in:
tom.hempel
2025-10-30 20:32:29 +01:00
parent 761f6b1bfe
commit 272cbfdca8
9 changed files with 98 additions and 43 deletions

View File

@ -101,7 +101,7 @@ public class UDPAvatarReceiverAgent : MonoBehaviour
if (enableReceiver)
{
StartUDPListener();
StartCoroutine(WaitAndSubscribe());
}
if (showDebugInfo)
@ -202,14 +202,6 @@ public class UDPAvatarReceiverAgent : MonoBehaviour
{
try
{
// Wait for shared listener to be ready
if (Convai.Scripts.Runtime.Multiplayer.SharedUDPListener.Instance == null)
{
Debug.LogError("SharedUDPListener not found! Make sure it's in the scene.");
enableReceiver = false;
return;
}
// Subscribe to shared listener
Convai.Scripts.Runtime.Multiplayer.SharedUDPListener.Instance.OnPacketReceived += HandlePacketReceived;
@ -222,6 +214,23 @@ public class UDPAvatarReceiverAgent : MonoBehaviour
enableReceiver = false;
}
}
System.Collections.IEnumerator WaitAndSubscribe()
{
float timeout = 3f;
while (Convai.Scripts.Runtime.Multiplayer.SharedUDPListener.Instance == null && timeout > 0f)
{
timeout -= Time.unscaledDeltaTime;
yield return null;
}
if (Convai.Scripts.Runtime.Multiplayer.SharedUDPListener.Instance == null)
{
Debug.LogError("SharedUDPListener not ready after wait.");
enableReceiver = false;
yield break;
}
StartUDPListener();
}
void HandlePacketReceived(byte[] data, IPEndPoint senderEndPoint)
{