From 5518a33ea539d741c473b08445d6a97537968ad9 Mon Sep 17 00:00:00 2001 From: Leevi Date: Mon, 23 Jan 2023 19:55:25 +0200 Subject: [PATCH] Remove maxDataPerFrame from inspector, because its unnecessary clutter --- Unity-Twitch-Chat/Assets/Package/Runtime/IRC.cs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/Unity-Twitch-Chat/Assets/Package/Runtime/IRC.cs b/Unity-Twitch-Chat/Assets/Package/Runtime/IRC.cs index 06e4ae7..4a413fe 100644 --- a/Unity-Twitch-Chat/Assets/Package/Runtime/IRC.cs +++ b/Unity-Twitch-Chat/Assets/Package/Runtime/IRC.cs @@ -41,11 +41,8 @@ public partial class IRC : MonoBehaviour [Tooltip("If true, the thread start and stop will be logged to the console.")] [SerializeField] public bool showThreadDebug = true; - [Tooltip("Limit how many data messages can be handled per frame.\n0 = unlimited.\n\nNote that setting this value to unlimited may cause lag spikes, particularly when the game is paused and then resumed, as there may be a large number of messages waiting to be processed.")] - [SerializeField] private int maxDataPerFrame = 30; - - [Tooltip("If true, chatters who haven't set their name color will be assigned a random color, instead of white.")] - [SerializeField] public bool useBackupRandomNameColor = false; + [Tooltip("If true, chatters who haven't set their name color on Twitch will be assigned a random color, instead of white.")] + [SerializeField] public bool useRandomColorForUndefined = false; [Header("Chat read settings (read thread)")] @@ -59,6 +56,11 @@ public partial class IRC : MonoBehaviour [Tooltip("The number of milliseconds between each time the write thread checks its queues.")] public int writeInterval = 50; + + // If the game is paused for a significant amount of time and then unpaused, + // there could be a lot of data to handle, which could cause lag spikes. + // To prevent this, we limit the amount of data handled per frame. + private static readonly int maxDataPerFrame = 100; private int connectionFailCount = 0; private TwitchConnection connection; public static IRC Instance { get; private set; } @@ -132,7 +134,7 @@ private void HandlePendingInformation() // Handle pending connection alerts while (!alertQueue.IsEmpty) { - if (maxDataPerFrame > 0 && dataHandledThisFrame >= maxDataPerFrame) + if (dataHandledThisFrame >= maxDataPerFrame) break; if (alertQueue.TryDequeue(out var alert)) @@ -145,7 +147,7 @@ private void HandlePendingInformation() // Handle pending chat messages while (!chatterQueue.IsEmpty) { - if (maxDataPerFrame > 0 && dataHandledThisFrame >= maxDataPerFrame) + if (dataHandledThisFrame >= maxDataPerFrame) break; if (chatterQueue.TryDequeue(out var chatter)) @@ -291,6 +293,7 @@ public void SendChatMessage(string message) Debug.LogWarning("Chat messages cannot be sent with anonymous login"); return; } + connection.SendChatMessage(message); } }