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

@ -144,7 +144,7 @@ namespace Convai.Scripts.Runtime.Multiplayer
{
_cancellationTokenSource = new CancellationTokenSource();
}
StartListening();
StartCoroutine(WaitAndSubscribe());
}
private void OnDestroy()
@ -209,12 +209,6 @@ namespace Convai.Scripts.Runtime.Multiplayer
try
{
// Wait for shared listener to be ready
if (SharedUDPListener.Instance == null)
{
ConvaiLogger.Error("SharedUDPListener not found! Make sure it's in the scene.", ConvaiLogger.LogCategory.Character);
return;
}
// Subscribe to shared listener
SharedUDPListener.Instance.OnPacketReceived += HandlePacketReceived;
@ -227,6 +221,22 @@ namespace Convai.Scripts.Runtime.Multiplayer
ConvaiLogger.Error($"❌ FAILED to subscribe Speech Receiver: {ex.Message}", ConvaiLogger.LogCategory.Character);
}
}
private System.Collections.IEnumerator WaitAndSubscribe()
{
float timeout = 3f;
while (SharedUDPListener.Instance == null && timeout > 0f)
{
timeout -= Time.unscaledDeltaTime;
yield return null;
}
if (SharedUDPListener.Instance == null)
{
ConvaiLogger.Error("SharedUDPListener not ready after wait.", ConvaiLogger.LogCategory.Character);
yield break;
}
StartListening();
}
public void StopListening()
{