Callbacks in component based design

I am trying to use component based design in Unity3d v5. If I have 2 separate C# scripts attached to enemy ships like below:

  1. Script1.shipDead() - Remove game
    object and show explosion animation

  2. Script2.showPoints() - Destroyed
    ship shows points ex: +100 (later
    this will fade away)

How can I perform showPoints() function after shipDead() function has been performed, as a callback?

I can’t put both actions in the same function, as some enemies will not have points. Some enemies will leave power ups/ small enemies/ bombs after being destroyed.

And I don’t want to duplicated the same code in 4 functions.

Like :

  1. function 1: destroy ship (nothing more)
  2. function 2: destroy ship + show points
  3. function 3: destroy ship + spawn small enemies
  4. function 4: destroy ship + show points + spawn bomb

(As you can see above, there are many different possibilities / combinations I can come up with)

I need to separate functions and isolate them to do individual tasks. As shipDead() will not do anything related to showPoints() or showPowerUp() functions.

My question is how can I perform functions sequentially? One after another, like a callback function?

Like this:

  1. run Script1.DestroyShip() first;
  2. after that run Script2.ShowPoints();
  3. and finally run Script3.SpawnBomb();

Please provide a solution that will not violate the following:

  1. Components needs to be reusable (I can drag and drop the scrip in to other enemies/ships)
  2. Code shouldn’t be duplicated
  3. Loosely coupled (Lesser dependancies)

Please help me on this, Thanks in advance!

You likely want to create a C# event delegate.

Try adding code like this in Script1:

public event Action OnShipDestroyed = delegate {};

This will let you reference that event from other objects and add callbacks. The Action type is a Delegate which represents a callback with no arguments. If you want to assign callbacks and pass arguments to them, use Action or Action etc. These generics represent the argument types you’re sending, so Action is passing an int value, Action is passing an int and a string, etc.

With a reference to your Script1 object, assign a delegate as such

script1Object.OnShipDestroyed += MyCallback

private void MyCallback()
{
    //do work
}

If you were using arguments, the callback signature would differ, e.g. 

private void MyCallback(int myInt, string myStr)

The += signifies we are adding to the delegate callbacks. To clean up a delegate after the object no longer needs it use the -= operator.

Events are very powerful! Try searching around for more information on events and delegates.

Seeing as you’ve mentioned some of my favorite coding principles (loose coupling, callbacks, DRY) I feel obligated to link my personal framework. This isn’t an official unity product, it’s something I work on in my personal time. It’s an inversion of control framework built on similar principles. I think you might find it very interesting! StrangeIoC