From 3f50625cc3058b3b79ba21f4a8355bd59c943676 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Wed, 30 Oct 2024 17:26:06 +0100 Subject: [PATCH] Fix proper handling of `Scripting.InvokeOnUpdate` if called within that callback --- Source/Engine/Scripting/Scripting.cs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/Source/Engine/Scripting/Scripting.cs b/Source/Engine/Scripting/Scripting.cs index 66a34faf0..8faf52c2b 100644 --- a/Source/Engine/Scripting/Scripting.cs +++ b/Source/Engine/Scripting/Scripting.cs @@ -316,7 +316,8 @@ namespace FlaxEngine lock (UpdateActions) { - for (int i = 0; i < UpdateActions.Count; i++) + int count = UpdateActions.Count; + for (int i = 0; i < count; i++) { try { @@ -327,7 +328,18 @@ namespace FlaxEngine Debug.LogException(ex); } } - UpdateActions.Clear(); + int newlyAdded = UpdateActions.Count - count; + if (newlyAdded == 0) + UpdateActions.Clear(); + else + { + // Someone added another action within current callback + var tmp = new List(); + for (int i = newlyAdded; i < UpdateActions.Count; i++) + tmp.Add(UpdateActions[i]); + UpdateActions.Clear(); + UpdateActions.AddRange(tmp); + } } MainThreadTaskScheduler.Execute();