#Callbacks and delegates

1 messages · Page 1 of 1 (latest)

true ingot
#

Let's put this in a separate thread

#

To explain what a callback does. Imagine trying to bake something, you want to simply put it in the oven, go and play in another room and come back when it's finished. The issue is that while you know it will take 30minutes, you do not have a clock in your game room, and do not want to get up every five minute to see how much time has passed.
So what you do instead if set up a timer that will loudly beep whenever 30mins have passed, informing you that you can get up and get your food out of the oven.

#

This is basically what a callback does, except with a few additional rules :
1- To be notified of a callback, you need to subscribe to it.
2- A callback does not only notify subscribers, but can also be used to pass variables (in your case, the math_number contained in your enemy_script)
3- To subscribe to a callback, you need to provide said callback with the method to be called back, which is contained in whatever script subscribes. The method must match the callback, if the callback provides one int, then the method must have one parameter of type int as well.
4- To invoke a callback, you need to put the line "MyCallback?.Invoke();" anywhere in your code where you want it to notify its subscribers.

#

In practice, this is what you want to do :
1- In enemy_script, add a variable "public Action<int> OnDestroyed;" This is your callback. By adding <int> after "Action", we're saying that this callback will provide an integer (in this case, math_number) whenever invoked.
2- In enemy_script, in the part of your script where you want the callback to be invoked (in your case, probably in the same place you call "Destroy(gameObject)"), write the line "OnDestroyed?.Invoke(math_number);" This is how you notify subscribers.
3- In enemy_spawner, create a method that matches the callback we just created, which is to say, a method that returns void and take one "int" as a parameter. Something like : "public void OnEnemyDestroyed(int math_number) { }".
4- In enemy_spawner, you need to get a reference to the enemy_script instances you spawn. To do this, first, convert the type of the variable "enemyPrefab" to "enemy_script" rather than GameObject. After that, add "enemy_script newEnemyScript = " in front of the "Instantiate(enemyPrefab..." line.
5- Now that you have a reference to the new enemy_script, you can subscribe to its callback by adding the line "newEnemyScript.OnDestroyed += OnEnemyDestroyed;" right below the "Instantiate" line. Whenever the line "OnDestroyed?.Invoke(math_number);" is called in enemy_script, it will call the "OnEnemyDestroyed" method

sturdy gull
#

Calling everything a callback may lead to confusion. In c#, you are adding to the delegate chain when you subscribe to something. Commonly you'll use a delegate with "event" to enforce extra rules about how it is managed or invoked.

#

Technically a callback is just the function that is called when you invoke. You dont subscribe to a callback

true ingot
#

6- Your last issue is that you currently do not have a way to communicate from your "enemy_spawner" to your "math_generator", so you will need to add a reference to math_generator in the enemy_spawner class. Now, you can write code in the "OnEnemyDestroyed" method to set the text of "starting_number" and "second_number" as you please

true ingot
sturdy gull
#

You may feel that, but it is just not the correct terms. I think the word callback is used more in web dev

round crag
#

Correct!

#

This would be more appropriate for the "callback" name. A function you pass to another that will be called when the work is complete (or the execution of the function is required):

private void DoSomething(int param, Action callback) { }

This is an event:

public event Action SomethingHappened;