What is a Callback?
I have been doing some online unity research and I can't seem to find an official documentation on Callbacks in unity. Google instead showed a Unity page about delegates. Are Callbacks, Delegates, and Actions all the same? I don't think that they are the same, but if that is true what are their differences?
If you did some research you probably came across Callback, right? So yes callbacks generally are impemented using delegates. The "System.Action" type is literally a delegate without return type and without parameters, so yes, System.Action is a specific delegate type.
Answer by UnityCoach · Nov 19, 2018 at 11:05 PM
In theory, they are similar. Same same, but different :)
A callback is a general term to say a method is meant to be called back whenever something happens. In C#, Events, Actions, and in Unity, UnityEvents, all allow to create callbacks.
In C#, you have Events. They are in the form of a subscriber signature, called a delegate, and an event :
public delegate void EventHandler();
public event EventHandler OnSuchEvent;
Then you subscribe any method that matches the signature like this :
OnSuchEvent += EventHandlingMethod;
void EventHandlingMethod () {}
and unsubscribe like that :
OnSuchEvent -= EventHandlingMethod;
Then you simply raise the event, checking it's not null first :
if (OnSuchEvent != null)
OnSuchEvent ();
This events can carry data. In which case, it looks like this :
public delegate void EventHandler(int param);
public event EventHandler OnSuchEvent;
OnSuchEvent += EventHandlingMethod;
void EventHandlingMethod (int param) {}
if (OnSuchEvent != null)
OnSuchEvent (12);
Then you have Actions. They are generic and save you the trouble of declaring the delegates and checking their null state.
public Action<int> OnSuchEvent;
OnSuchEvent += EventHandlingMethod;
void EventHandlingMethod (int param) {}
OnSuchEvent.Invoke (12);
Unity also has its own UnityEvents, they are also Generic. They show up in the inspector so that you can subscribe other components' members/methods to them. You need to add
using UnityEngine.Events;
Then you can use parameter less events like this :
[SerializeField] UnityEvent myEvent;
myEvent.Invoke ();
and with parameters :
[System.Serializable] public class IntEvent : UnityEvent<int> {}
[SerializeField] IntEvent intEvent;
intEvent.Invoke (12);
Forgot to answer to the question, a callback is a general term to say a method is meant to be called back whenever something happens. In C#, Events, Actions, and in Unity, UnityEvents, all allow to create callbacks.
I think you should add this in the beginning of your answer.
Very helpful and useful, I am still a little confused about when and how callbacks work. Why would someone use a callback? When would someone want to use a callback?
You usually use them to avoid watching variables for changes that'll happen occasionally, and also decouple components, as the event sender doesn't need to know about the receiver.
Consider the following code :
void Update ()
{
if (distanceToPlayer < criticalDistance)
{
// do things
}
}
Now consider this ins$$anonymous$$d :
Action OnCriticalDistanceReached;
void Awake ()
{
OnCriticalDistanceReached += DoThings;
}
void DoThings ()
{
// do things
}
When do you use the CallBack(myCallback) function?
The Action part is a bit misleading and somehow wrong, Action is just a declaration based on delegate. The following is the declaration in the System namespace.
public delegate void Action();
.NET and $$anonymous$$icrosoft, just decided to provide an already created type of delegate. You are also na$$anonymous$$g Action OnSuchEvent like it would be an event while an event would be declared as
public event Action OnSuchEvent;
The Invoke method is not preventing the null check as you seem to mention. The problem with a public delegate, any other object can wipe it off (this is why we have event that prevents it), so your lines about Action can't just assume there is something to point at and should check for nullity.
Or I misunderstood your writing.
https://referencesource.microsoft.com/#mscorlib/system/action.cs
Since C# 6 you can also use the ? Operator to make your Action null-checks a bit more compact:
Before:
System.Action<int> OnSuchAction;
if (OnSuchAction!= null)
OnSuchAction.Invoke ();
Using the new null-operator:
System.Action<int> OnSuchAction;
OnSuchAction?.Invoke ();
Your answer
![](https://koobas.hobune.stream/wayback/20220612182422im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
I need help with Deleting and Initiating a gameobject 1 Answer
UnityAction Vs UnityEvent 1 Answer
[ANSWERED] c# performance impact - Global variables vs Local variables 1 Answer
Delegates & Events registration & deregistration - Doubt 0 Answers
Unity Custom Editor - Lauch callback at the end of the SerializeField. 0 Answers