Skip to content
This repository was archived by the owner on Aug 10, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions Assets/Runtime/GameEvents/Game/GameEvent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using GameEvents.Generic;
using UnityEngine;
Expand Down Expand Up @@ -39,7 +40,15 @@ public void RaiseGameEvent()
Debug.Log($"Raise event: {name}, listener: {listener}");
}

listener.RaiseGameEvent();
try
{
listener.RaiseGameEvent();
}
catch (Exception e)
{
Debug.Log($"Listener: {listener} of event: {name} has thrown an exception.");
Debug.LogException(e, this);
}
}
}

Expand Down
13 changes: 11 additions & 2 deletions Assets/Runtime/GameEvents/Generic/ArgumentGameEvent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using UnityEngine;

Expand Down Expand Up @@ -38,7 +39,15 @@ public void RaiseGameEvent(TArgument argument)
Debug.Log($"Raise event: {name}, listener: {listener}, argument: {argument}");
}

listener.RaiseGameEvent(argument);
try
{
listener.RaiseGameEvent(argument);
}
catch (Exception e)
{
Debug.Log($"Listener: {listener} of event: {name} has thrown an exception.");
Debug.LogException(e, this);
}
}
}

Expand Down
37 changes: 36 additions & 1 deletion Assets/Tests/Runtime/GameEvents/GameEventTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using System;
using System.Linq;
using GameEvents.Bool;
using GameEvents.Float;
using GameEvents.Game;
Expand Down Expand Up @@ -117,6 +118,40 @@ public void ShouldRaiseGameEventEvent()
Assert.AreEqual(0, count[0]);
}


[Test]
public void ShouldNotBreakChainWhenExceptionIsThrown()
{
// Given.
var gameObject = new UnityEngine.GameObject();
gameObject.SetActive(false);

var listenerWithError = gameObject.AddComponent<GameEventListener>();
var listener = gameObject.AddComponent<GameEventListener>();

listenerWithError.OnGameEvent = new UnityEvent();
listenerWithError.GameEvent = ScriptableObject.CreateInstance<GameEvent>();

listener.OnGameEvent = new UnityEvent();
listener.GameEvent = listenerWithError.GameEvent;

var count = new int[1];
listenerWithError.OnGameEvent.AddListener(() => throw new NullReferenceException());
listener.OnGameEvent.AddListener(() => count[0]++);

// Then.
gameObject.SetActive(true);
listener.GameEvent.RaiseGameEvent();

Assert.AreEqual(1, count[0]);
count[0] = 0;

gameObject.SetActive(false);
listener.GameEvent.RaiseGameEvent();

Assert.AreEqual(0, count[0]);
}

[Test]
public void ShouldRegisterAndUnregisterGameObjectGameEventListener()
{
Expand Down