refactored scripts to avoid execution order issues
This commit is contained in:
@ -107,7 +107,7 @@ namespace Convai.Scripts.Runtime.Multiplayer
|
|||||||
{
|
{
|
||||||
_cancellationTokenSource = new CancellationTokenSource();
|
_cancellationTokenSource = new CancellationTokenSource();
|
||||||
}
|
}
|
||||||
StartListening();
|
StartCoroutine(WaitAndSubscribe());
|
||||||
|
|
||||||
// Immediately try to assign an enabled NPC
|
// Immediately try to assign an enabled NPC
|
||||||
if (useActiveNPC && targetNPC == null)
|
if (useActiveNPC && targetNPC == null)
|
||||||
@ -227,13 +227,6 @@ namespace Convai.Scripts.Runtime.Multiplayer
|
|||||||
|
|
||||||
try
|
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
|
// Subscribe to shared listener
|
||||||
SharedUDPListener.Instance.OnPacketReceived += HandlePacketReceived;
|
SharedUDPListener.Instance.OnPacketReceived += HandlePacketReceived;
|
||||||
_isListening = true;
|
_isListening = true;
|
||||||
@ -246,6 +239,22 @@ namespace Convai.Scripts.Runtime.Multiplayer
|
|||||||
ConvaiLogger.Error($"Stack trace: {ex.StackTrace}", ConvaiLogger.LogCategory.Character);
|
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()
|
public void StopListening()
|
||||||
{
|
{
|
||||||
|
|||||||
@ -144,7 +144,7 @@ namespace Convai.Scripts.Runtime.Multiplayer
|
|||||||
{
|
{
|
||||||
_cancellationTokenSource = new CancellationTokenSource();
|
_cancellationTokenSource = new CancellationTokenSource();
|
||||||
}
|
}
|
||||||
StartListening();
|
StartCoroutine(WaitAndSubscribe());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnDestroy()
|
private void OnDestroy()
|
||||||
@ -209,12 +209,6 @@ namespace Convai.Scripts.Runtime.Multiplayer
|
|||||||
|
|
||||||
try
|
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
|
// Subscribe to shared listener
|
||||||
SharedUDPListener.Instance.OnPacketReceived += HandlePacketReceived;
|
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);
|
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()
|
public void StopListening()
|
||||||
{
|
{
|
||||||
|
|||||||
@ -12,6 +12,7 @@ namespace Convai.Scripts.Runtime.Multiplayer
|
|||||||
/// Shared UDP Listener - Single UDP socket that dispatches packets to appropriate handlers
|
/// Shared UDP Listener - Single UDP socket that dispatches packets to appropriate handlers
|
||||||
/// Solves the port binding issue where multiple components tried to bind to the same port
|
/// Solves the port binding issue where multiple components tried to bind to the same port
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[DefaultExecutionOrder(-10000)]
|
||||||
public class SharedUDPListener : MonoBehaviour
|
public class SharedUDPListener : MonoBehaviour
|
||||||
{
|
{
|
||||||
// Singleton
|
// Singleton
|
||||||
@ -43,6 +44,7 @@ namespace Convai.Scripts.Runtime.Multiplayer
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
_instance = this;
|
_instance = this;
|
||||||
|
DontDestroyOnLoad(gameObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Start()
|
private void Start()
|
||||||
|
|||||||
@ -98,7 +98,7 @@ public class UDPAvatarReceiver : MonoBehaviour
|
|||||||
|
|
||||||
if (enableReceiver)
|
if (enableReceiver)
|
||||||
{
|
{
|
||||||
StartUDPListener();
|
StartCoroutine(WaitAndSubscribe());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (showDebugInfo)
|
if (showDebugInfo)
|
||||||
@ -199,14 +199,6 @@ public class UDPAvatarReceiver : MonoBehaviour
|
|||||||
{
|
{
|
||||||
try
|
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
|
// Subscribe to shared listener
|
||||||
Convai.Scripts.Runtime.Multiplayer.SharedUDPListener.Instance.OnPacketReceived += HandlePacketReceived;
|
Convai.Scripts.Runtime.Multiplayer.SharedUDPListener.Instance.OnPacketReceived += HandlePacketReceived;
|
||||||
|
|
||||||
@ -219,6 +211,23 @@ public class UDPAvatarReceiver : MonoBehaviour
|
|||||||
enableReceiver = false;
|
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)
|
void HandlePacketReceived(byte[] data, IPEndPoint senderEndPoint)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -101,7 +101,7 @@ public class UDPAvatarReceiverAgent : MonoBehaviour
|
|||||||
|
|
||||||
if (enableReceiver)
|
if (enableReceiver)
|
||||||
{
|
{
|
||||||
StartUDPListener();
|
StartCoroutine(WaitAndSubscribe());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (showDebugInfo)
|
if (showDebugInfo)
|
||||||
@ -202,14 +202,6 @@ public class UDPAvatarReceiverAgent : MonoBehaviour
|
|||||||
{
|
{
|
||||||
try
|
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
|
// Subscribe to shared listener
|
||||||
Convai.Scripts.Runtime.Multiplayer.SharedUDPListener.Instance.OnPacketReceived += HandlePacketReceived;
|
Convai.Scripts.Runtime.Multiplayer.SharedUDPListener.Instance.OnPacketReceived += HandlePacketReceived;
|
||||||
|
|
||||||
@ -222,6 +214,23 @@ public class UDPAvatarReceiverAgent : MonoBehaviour
|
|||||||
enableReceiver = false;
|
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)
|
void HandlePacketReceived(byte[] data, IPEndPoint senderEndPoint)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -64,7 +64,7 @@ public class VRExperimentController : MonoBehaviour
|
|||||||
}
|
}
|
||||||
|
|
||||||
InitializeObjectMaps();
|
InitializeObjectMaps();
|
||||||
StartUDPListener();
|
StartCoroutine(WaitAndSubscribe());
|
||||||
|
|
||||||
// Initially deactivate all objects and avatars
|
// Initially deactivate all objects and avatars
|
||||||
DeactivateAllObjects();
|
DeactivateAllObjects();
|
||||||
@ -193,13 +193,6 @@ public class VRExperimentController : MonoBehaviour
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Wait for shared listener to be ready
|
|
||||||
if (Convai.Scripts.Runtime.Multiplayer.SharedUDPListener.Instance == null)
|
|
||||||
{
|
|
||||||
LogError("SharedUDPListener not found! Make sure it's in the scene.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Subscribe to shared listener
|
// Subscribe to shared listener
|
||||||
Convai.Scripts.Runtime.Multiplayer.SharedUDPListener.Instance.OnPacketReceived += HandlePacketReceived;
|
Convai.Scripts.Runtime.Multiplayer.SharedUDPListener.Instance.OnPacketReceived += HandlePacketReceived;
|
||||||
isListening = true;
|
isListening = true;
|
||||||
@ -212,6 +205,22 @@ public class VRExperimentController : MonoBehaviour
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private 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)
|
||||||
|
{
|
||||||
|
LogError("SharedUDPListener not ready after wait.");
|
||||||
|
yield break;
|
||||||
|
}
|
||||||
|
StartUDPListener();
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Handle packet received from shared listener
|
/// Handle packet received from shared listener
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
7
Unity-Master/Assets/Scripts/script-overview.md.meta
Normal file
7
Unity-Master/Assets/Scripts/script-overview.md.meta
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 0b725a59a21efb044ba2d8b4ef29095c
|
||||||
|
TextScriptImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
BIN
Unity-Master/ProjectSettings/EditorBuildSettings.asset
(Stored with Git LFS)
BIN
Unity-Master/ProjectSettings/EditorBuildSettings.asset
(Stored with Git LFS)
Binary file not shown.
BIN
Unity-Master/ProjectSettings/ProjectSettings.asset
(Stored with Git LFS)
BIN
Unity-Master/ProjectSettings/ProjectSettings.asset
(Stored with Git LFS)
Binary file not shown.
Reference in New Issue
Block a user