Hi guys. So I’m using events and when a method called by the event generates an error, the output points to the event invoke instead of giving me more of a scope (as in it tells me the line and method where the exact error was generated).
It was doing that before I realized I had to make it a development build and enable Build Settings > Script Debugging, then it would show the specific errors even in the editor. I haven’t changed the settings (besides off then on again, just to see), and it seems to have gone back to its old ways of showing the error as the event invoke.
_
Does anyone have any ideas how to solve this? I cannot work around it, as my game is using the observer pattern heavily so every time I get a simple error I want to tear my hair out because the console is holding information from me lmao
_
Thank you! And, if you have any questions please ask!
- Michael
Attached is an image of my build settings, the event code I am using
Note that I use .Invoke() so in this context, the console points me to the ‘throw exception’ of that method. But, as previously stated, I used to get pinpoint accuracy on the exception and no longer do, not sure what changed.
//0 parameter
public class TrappedEvent {
private event Action privateEvent;
private readonly Dictionary<Action, Action> dictionary = new Dictionary<Action, Action>();
private List<Exception> exceptions;
public static TrappedEvent operator +(TrappedEvent ev, Action action) {
if (ev == null)
ev = new TrappedEvent();
if (ev.dictionary.ContainsKey(action))
ev.privateEvent -= ev.dictionary[action];
Action wrapper = delegate {
try {
action();
} catch (Exception e) {
ev.exceptions.Add(e);
}
};
ev.dictionary[action] = wrapper;
ev.privateEvent += wrapper;
return ev;
}
public static TrappedEvent operator -(TrappedEvent ev, Action action) {
if (ev == null)
ev = new TrappedEvent();
ev.privateEvent -= ev.dictionary[action];
ev.dictionary.Remove(action);
return ev;
}
public TrappedEventException TryInvoke() {
exceptions = new List<Exception>();
if (privateEvent != null)
privateEvent();
if (exceptions.Count > 0)
return new TrappedEventException(exceptions);
return null;
}
public void Invoke() {
var exception = TryInvoke();
if (exception != null)
throw exception;
}
}
public class TrappedEventException : Exception {
public readonly ReadOnlyCollection<Exception> innerExceptions;
public TrappedEventException(IList<Exception> exceptions) {
innerExceptions = new ReadOnlyCollection<Exception>(exceptions);
}
}
//This will cause an exception because the game object does not exist, output below
public class Test : MonoBehaviour {
TrappedEvent ev = new TrappedEvent();
void Start () {
ev += Tests;
ev.Invoke();
}
void Tests() {
GameObject.Find("sdfmnsdjkfhgsdhjf").transform.position = Vector3.zero;
}
}
OUTPUT: Screenshot by Lightshot
Test:13 is ev.Invoke();
TrappedEvent:254 is throw exception;
BUILD SETTINGS: Screenshot by Lightshot