Skip to content

Commit

Permalink
Merge pull request #115 from rhys-vdw/101-callback-errors
Browse files Browse the repository at this point in the history
Callback errors (#101)
  • Loading branch information
rhys-vdw authored Aug 8, 2022
2 parents a7ce096 + 5d6253d commit 4dae093
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
7 changes: 6 additions & 1 deletion Runtime/Components/CallbackComponents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ static class CallbackUtility {
internal static void Invoke<T>(this EcsWorld world, int entity) where T : struct, ICallback {
var callbackPool = world.GetPool<T>();
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);
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions Runtime/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -441,14 +441,14 @@ public static void Run<TGroup>(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);
Expand Down
24 changes: 23 additions & 1 deletion Tests/OnUpdateTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using UnityEngine;
using NUnit.Framework;
using System;
using UnityEngine;

namespace PeachyTween.Tests {
public class OnUpdateTest : BaseTweenTest {
Expand Down Expand Up @@ -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");
}
}
}

0 comments on commit 4dae093

Please sign in to comment.