Hey everyone,
I have some difficulties about understanding all the differences and usages of Unity Listener, Unity Events, Delegates, C# Events.
Could someone help me a bit about clarifying this ?
Thanks !
Hey everyone,
I have some difficulties about understanding all the differences and usages of Unity Listener, Unity Events, Delegates, C# Events.
Could someone help me a bit about clarifying this ?
Thanks !
You can think of an event as a “container of functions”, like a box where other classes can put their functions in. When an event is “fired”, all functions in there will be executed.
For example I have an event in my game called OnSave, which is fired when a Save Game procedure is started.
Delegate is basically saying “This kind of functions can be subscribed to an event (be put into the container, if you will)”. Here’s my specific code example, in a public singleton class called GlobalControl:
public delegate void SaveHandler(object sender, EventArgs e);
public event SaveHandler OnSave;
This basically says:
Delegate is like a “blueprint”, which describes a format of a function. My delegate says:
Next, I say: This is a new event called OnSave. It can accept functions that match the delegate called “SaveHandler”.
Consider a player opening a chest in the game, and chest spawns 3 swords.
Each sword (or LongswordDroppable object) says:
public void Start()
{
GlobalControl.Instance.OnSave += MySaveFunction;
}
public void OnDestroy()
{
GlobalControl.Instance.OnSave -= MySaveFunction;
}
...
public void MySaveFunction(object sender, EventArgs e)
{
//Save this sword in the list of objects that should be spawned when level is loaded
}
Translation:
At Start() function, each sword says “OK, I have a function that matches a delegate (it has required arguments, object sender and EventArgs), and I’d like to put that function into OnSave box)”.
OnDestroy() function says: “OK nevermind, I would like to take my function out of the Save box, since I will be destroyed”.
So after three swords are spawned, you can imagine the OnSave event now has three functions registered:
Suppose player picks up the second sword, then saves the game.
Now there are only two functions in the Save “box of functions”
To execute all registered functions, you can call the following:
if (OnSave != null)
OnSave(null, null);
This basically says:
"OK time to save, now, do we have any functions registered at all? If so, let’s go through them:
Sword1, you now execute your MySaveFunction. I’ll wait. OK done?
Sword3, you now execute your MySaveFunction. Done?
OK this is it for the event, now we can continue with the code".
NOTE You can only fire the event from the class that contains the delegate and event declaration. In other words, ONLY GlobalControl can call OnSave(argument, argument). If you try to do from somewhere else:
GlobalControl.Instance.OnSave(null, null);
You will get a compiler error.
Just one more note I am using two arguments for the delegate (and hence all subscribed functions), object and EventArgs simply because they are mandatory by Mono/.NET framework, but I don’t really need them, so I’m just passing “null” values. If you want you can actually pass some useful values, by constructing your EventArgs structure and filling it with some useful data if you need to.
Final note, swear to spirits of Palaven As to the other part of your question, Unity Events and Unity Listeners are basically this exact same thing, only premade (already declared events that you can subscribe to). You may have even used this without realising:
If you have used buttons in your UI, when setting their OnClick function, you basically subscribed a function to an OnClick event that was created when you put the button in your scene.
This is how you can have multiple functions subscribed to the same OnClick (or what have you) event.