I’m a little bit confused about this. Here’s the example by a user called LightStriker that made it click for me, but there’s one thing I don’t get.
public class One
{
public delegate void Enter (int x);
// Static is only needed if you want it to be static. It's like a variable.
// If the event is related to an instance, it should not be static.
public event Enter OnEntered;
public void Enter()
{
int myInt = 0;
if (DateTime.Now.IsDaylightSavingTime())
myInt = 1;
// It is up to you to fire an event.
if (OnEntered != null)
OnEntered(myInt)
};
}
public class Two
{
private One one;
public Two()
{
one = new One();
one.OnEntered += Method;
}
private void Method (int y)
{
if (y == 1)
Debug.Log("Day Time Saving!");
else
Debug.Log("No saving.");
}
}
My current understanding is that Enter (int x) sends the parameter to the subscribing method in another class. Just like in the example and int Enter is set to either 0 or 1 and then when the event is fired, that int is passed in as a parameter and is read by the subscribed class.
Now what if I had a return type other than void?
If I had a
public delegate float Number (int x);
public event Number OnNum;
then would that mean that the subscribing method in another class will get the int from here, but return a float to the delegate? I’ve read that it isn’t a good idea to have an event with a return type, but I never quite gathered as to why was that so bad. At any rate I’d appreciate some clarification on the matter.