- Home /
How do Delegates and Events work?
I've looked around and saw few examples and tutorials, each one was explaining differently and I couldn't understand how exactly do they work.
so:
What is Delegate
What is Event
How to use Delegate
How to use Event
I'm posting this Q&A as I saw non other to fit logically how they work at least in my mind.
if you are wandering why OAFAT title? It might be first but I hope it won't be last.
It's not the first one. I've been on the lookout for good Questions and Answers for the oafa -tag and this certainly is worthy to be the 4th. Ins$$anonymous$$d of the title, I use the tag system; you can easily find all questions with the same tag by clicking the tag.
Answer by sdgd · Dec 20, 2013 at 12:27 PM
Delegate is a container of events (we could say array)
Events are container of pointers to functions (we could say array)
I'll try to write as many different names as possible so you can see how it works.
using UnityEngine;
using System.Collections;
public class EventManager : MonoBehaviour {
public delegate void DelegateContainer ();
public static event DelegateContainer EventContainerOne; // this is when DelegateContainer adds event container to him self
public static event DelegateContainer EventContainerTwo;
public bool EditorTriggerOne = false;
public bool EditorTriggerTwo = false;
public bool EditorTriggerReset = false;
void Update(){
if (EditorTriggerOne){
TriggerOne();
EditorTriggerOne = false;
}
if (EditorTriggerTwo){
TriggerTwo();
EditorTriggerTwo = false;
}
if (EditorTriggerReset){
Reset();
EditorTriggerReset = false;
}
}
// NormalFunctions
public void TriggerOne(){
// if there's no pointers to functions in EventContainerOne it'll give us NRE error
if (EventContainerOne != null){
// we fire all functions inside this EventContainer
EventContainerOne();
}
}
public void TriggerTwo(){
if (EventContainerTwo != null){
EventContainerTwo();
}
}
// here we remove all functions from events
public void Reset(){
EventContainerOne = null;
EventContainerTwo = null;
}
}
Random Scripts that Want the behaviour:
using UnityEngine;
using System.Collections;
public class RandomScript : MonoBehaviour {
void Start(){
EventManager.EventContainerOne += NormalFunctionOne;
EventManager.EventContainerTwo += NormalFunctionTwo;
EventManager.EventContainerTwo += NormalFunctionThree;
}
void NormalFunctionOne(){
Debug.Log("Func 1");
}
void NormalFunctionTwo(){
Debug.Log("Func 2");
}
void NormalFunctionThree(){
Debug.Log("Func 3");
}
}
Now each time you trigger one func 1 will appear.
Each time you trigger two func 2&&3 will appear.
This is because there are 2 pointers inside EventContainerTwo.
Another thread regarding events&delegates and their uses can be found here
Sources:
I think you got the terms a 'little bit' mixed up :) - It's actually the delegate that's a pointer to a function. An event is a delegate itself. $$anonymous$$ore specifically, it's a multicast delegate. $$anonymous$$eaning you can't assign an event: onPlayerDied = OnPlayerDiedHandler;
ins$$anonymous$$d you have to: onPlayerDied += OnPlayerDiedHandler;
- in delegates, you don't have that constraint.
I highly recommend going over this series from the beast Jamie king about delegates, events, lambda expressions, anonymous types, etc. http://www.youtube.com/playlist?list=PLAE7FECFFFCBE1A54
and what is
onPlayerDied
OnPlayerDiedHandler
can't you be more specific to this answer than to links I've given?
and can you write in code please and not in salami it's really hard to read.
and above all BTW it's marked for unity wiki and you can change it.
and the code above works exactly as I said, ... I verified it again.
if you trigger 1 you get debug 1 and if you trigger 2 you get 2&&3
and I will try doing it without delegate, ... as I said just started learning this, ...
but you can as always change my answer, ...
maybe after I wake up from my coma. But the youtube list ^ covers it all.
Your answer
Follow this Question
Related Questions
Fire C# Event 3 Answers
Subscribers of Delegates? 2 Answers
Question about unsubscribing eventhandlers OnDisable and OnEnable 2 Answers
Stack Overflow error on delegate call 2 Answers