Explain Delegates with a simple game scenario

I have been reading a lot about Delegates and m working on NGUI and plugins. I have understand what delegates are(most of the part…) but cant decide when and where to use.

Can anyone plz explain with a simple gaming scenario.

Ok so basically a variable has data in it whereas a delegate contains methods/functions. You can use them to create complex behaviors for objects. So lets say you have a weapon in your game and want it to have different functions to it. Say an assault rifle.

public delegate void ShootWeapon();
ShootWeapon weaponShoot;

pulic enum FireMode

FireMode currentFireMode = FireMode.Single;

So we have created a delegate and an enum here which contains the types of shooting your gun can do. Once this is done later in our update we can do something like this.

{ switch(currentFireMode)

//if your current weapon fire mode is single shot, switch to burst
currentFireMode = FireMode.Burst;
weaponShoot = BurstShot;

//if your current weapon fire mode is single burst, switch to auto
currentFireMode = FireMode.Auto;
weaponShoot = AutoShot;

//if your current weapon fire mode is single auto, switch to single
CurrentFireMode = FireMode.Single;
weaponShoot = SingleShot;


So basically based on the enum the delegate is altered to run a different function. So you would have functions for SingleShot(), BurstShot(), AutoShot().

Have a look at this unity video too hopefully it will clear some things up for you. 1