Hello everyone. I’m wondering how to use interfaces as responses to an event. I’ve got interface:
public interface IBombable
{
void OnBombExplosion();
}
I’ve Implemented that interface into other classes.
Here is an invoke part:
public class Explosion : MonoBehaviour
{
public static event System.Action<Vector2> EventExplode;
private void Start()
{
EventExplode?.Invoke(transform.position);
}
}
And there is my question: Is it possible to add subscribing to an event somehow different than adding " csharp** **Explosion.EventExplode += OnBombExplosion;** ** line in every script that implements that interface?
For example IDragHandler or other built-in interfaces works like that :
Inherit from specifc interface then write your own implementation and voila, everything is working, so where are that subscription to an event parts?
your interface IBombable’s OnBombExplosion handler does not follow the same shape as your EventExplode event.
(one has no parameters, the other expects a Vector2 parameter)
you seem to be mixing 2 different implementations of the subscriber pattern. It’s usually an “either or”… you either go the interface subscriber method, OR you go the delegate method.
Thanks for the response!
About differences in parameters - yep, it’s an effect of me messing around with the code, but it’s an error of course.
I consider that this might be the thing that I should use delegates OR interfaces, not both, but this take me to the next consideration : How does IDragHandler and other built-in interfaces work then? Found something on the web about IEventSystemHandler (which for example IDragHandler inherits from) but does not really understand how this works.
public interface IDragHandler : IEventSystemHandler
{
//
// Summary:
// When draging is occuring this will be called every time the cursor is moved.
//
// Parameters:
// eventData:
// Current event data.
void OnDrag(PointerEventData eventData);
This is sort of a black-box in design… but essentially when you create an EventSystem and InputModule in your scene it keeps track of everything going on input wise. What GameObject is focused, what inputs are being pressed (as configured by the InputModule), and all that sort.
When some action occurs the EventSystem resolves what should occur and dispatches the appropriate events. For example lets say the user presses the mouse button, the InputModule/EventSystem performs a raycast and sees if it hit any UI elements, if it has any components that implement IPointerClickHandler it calls the appropriate methods on it.
Effectively all this does is it calls GetComponent on the GameObject in question for the interface type, loops over every isntance, and forwards the call along.
This is a design very unique to Unity and Unity’s component model.
I guess I will just add subscription to an event in every script that inherits from IBombable interface. With combining interfaces and delegates I at least don’t have to add bool for check if my object is ‘bombable’ so maybe it was worth it.
**Thank You very much @lordofduct ** for such a quick responses, I really appreciate it.