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

@ -107,7 +107,7 @@ namespace Convai.Scripts.Runtime.Multiplayer
{
_cancellationTokenSource = new CancellationTokenSource();
}
StartListening();
StartCoroutine(WaitAndSubscribe());
// Immediately try to assign an enabled NPC
if (useActiveNPC && targetNPC == null)
@ -227,13 +227,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;
_isListening = true;
@ -246,6 +239,22 @@ namespace Convai.Scripts.Runtime.Multiplayer
ConvaiLogger.Error($"Stack trace: {ex.StackTrace}", 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()
{