From 84346ad01ab59529d54033fdc1fb7d5470663487 Mon Sep 17 00:00:00 2001 From: Rhys van der Waerden Date: Sat, 6 Aug 2022 03:34:41 +1000 Subject: [PATCH 1/2] Fix guard on Run --- Runtime/Core.cs | 4 ++-- Tests/OnUpdateTest.cs | 24 +++++++++++++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/Runtime/Core.cs b/Runtime/Core.cs index 914142f..27810e4 100644 --- a/Runtime/Core.cs +++ b/Runtime/Core.cs @@ -441,14 +441,14 @@ public static void Run(float deltaTime) where TGroup : struct { AssertNotRunning(); if (_groupFilters.TryGetValue(typeof(TGroup), out var filter)) { _runState.Set(filter, deltaTime); - _systems.Run(); + Run(); } } public static void ManualUpdate(int entity, float deltaTime) { AssertNotRunning(); _runState.Set(entity, deltaTime); - _systems.Run(); + Run(); } public static void Sync(int entity) => ManualUpdate(entity, 0); diff --git a/Tests/OnUpdateTest.cs b/Tests/OnUpdateTest.cs index d6062fc..ee26a11 100644 --- a/Tests/OnUpdateTest.cs +++ b/Tests/OnUpdateTest.cs @@ -1,5 +1,6 @@ -using UnityEngine; using NUnit.Framework; +using System; +using UnityEngine; namespace PeachyTween.Tests { public class OnUpdateTest : BaseTweenTest { @@ -56,5 +57,26 @@ public void RemoveOnUpdate() { Assert.AreEqual(1, aCount, $"first {nameof(OnUpdate)} callback called"); Assert.AreEqual(0, bCount, $"second {nameof(OnUpdate)} callback was not called"); } + + [Test] + public void SyncOnUpdate() { + var caught = null as Exception; + var onUpdate = 0; + var tween = Peachy.Tween(0f, 1f, 1f, _ => {}); + + tween.OnUpdate(() => { + onUpdate++; + try { + tween.Sync(); + } catch (Exception e) { + caught = e; + } + }); + + tween.ManualUpdate(0.5f); + + Assert.AreEqual(1, onUpdate, $"{nameof(OnUpdate)} callback called"); + Assert.True(caught is Exception, $"{nameof(Exception)} caught"); + } } } From 5d6253df4ec8252235c4d64880c792c8cd8fdc0b Mon Sep 17 00:00:00 2001 From: Rhys van der Waerden Date: Mon, 8 Aug 2022 21:10:27 +1000 Subject: [PATCH 2/2] Catch callback errors To prevent them from interrupting the systems. --- Runtime/Components/CallbackComponents.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Runtime/Components/CallbackComponents.cs b/Runtime/Components/CallbackComponents.cs index 850160a..b5e41c2 100644 --- a/Runtime/Components/CallbackComponents.cs +++ b/Runtime/Components/CallbackComponents.cs @@ -26,7 +26,12 @@ static class CallbackUtility { internal static void Invoke(this EcsWorld world, int entity) where T : struct, ICallback { var callbackPool = world.GetPool(); if (callbackPool.Has(entity)) { - callbackPool.Get(entity).Callback(); + try { + callbackPool.Get(entity).Callback(); + } catch (Exception e) { + var error = new Exception($"Error in {typeof(T)} callback", e); + UnityEngine.Debug.LogError(error); + } } }