Search Unity

Way to debug scriptable object event system

Discussion in 'Scripting' started by tranos, Jan 4, 2021.

  1. tranos

    tranos

    Joined:
    Feb 19, 2014
    Posts:
    180
    Hello. I use Scriptable Objects for an event system where I assign the SO-event to listeners in editor. The thing is, I can't think a simple way to debug this system.
    One way could be maybe to run a debug. log that prints the event name and Gameobject name. What are your thoughts
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Unfortunately, having lots of listeners is a great way to make code hard to debug and reason about.

    It's always best to strip down your problem to the smallest set of data and code that shows it happening, in order to reduce the number of places that you set breakpoints.

    Unfortunately with generic enough listeners, breakpoints become completely useless. You instead need to start hacking extra instrumentation code such as :

    Code (csharp):
    1. // in the guts of your event broadcaster
    2. if (evt.type == TheBugType)
    3. {
    4.   Debug.Log( "About to send bug-causing event!");
    5. }
    6. evt.Invoke();
    7. if (evt.type == TheBugType)
    8. {
    9.   Debug.Log( "The bug-causing event has completed!");
    10. }
    11.  
    Good luck!
     
  3. tranos

    tranos

    Joined:
    Feb 19, 2014
    Posts:
    180
    Thank you @Kurt-Dekker. Is there an alternative to events?