Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm currently working on a small game-engine and i'm kinda stuck on finding the right pattern to subscribe to an event fired from any instance of type X.To understand what i'm trying to achieve, here are some code snippets :

 public class FireEffect : BaseEffect
 {
   public FireEffect(BaseAbility effectOwner)
   {
    this.Owner = effectOwner; // each effect belong to an ability
    this.Name = "Fire";
   } 
   //apply effect on target
   public override void CalculateEffect(Unit target)
   {
    // set the event here (to get the target as argument for the event)
    this.Event = new EventDispatcher(this.Owner, new EffectEventArgs(target));
    this.Event.DispatchMyEvent(); // rise/dispatch this effect event
    base.CalculateEffect(target); // do stuffs like burn ..damage etc
  } 
}

and Let´s say we have two Units : a "dragon" with FireBall ability (contain FireEffect) and a "Shaolin" with a passiveAbility (contain WindEffect)

 public class PassiveAbility : BaseAbility
 {
   public PassiveAbility(Unit caster)  
   {
    this.Name = "WindFury";
    this.Caster = caster; // ability caster (owner)
    this.EffectList = new List<BaseEffect>(); // effects that an ability possess
    this.EffectList.Add(new WindEffect(this));
    this.initListeners(); // subscribe to events (aka add listeners)
   }

   // apply all effects that this ability has on the selected target
   public override void DoAbility(Unit target)
   {
     foreach (BaseEffect eff in this.EffectList)
    {                
        eff.CalculateEffect(target);
    }
   }
   // THE MAIN PROBLEM : 
   public override void initListeners(){}

  //call this method if FireEffect.Event was rised 
  public void CallBack(object sender, EffectEventArgs e) 
  {
   BaseAbility ab = (BaseAbility)sender;
   this.DoEffect(ab.UnitCaster); // hit the unit caster back (revenge)
  }
}

How to subscribe via PassiveAbility.initListeners() to an event fired from any instance of type FireEffect (an ability that use fireEffect) ???

Otherwise put : when our dragon throw an ability with a FireEffect (like fireBall) on our friend "Shaolin", he should react to it using WindEffect via PassiveAbility.

Notice please that we can have so many dragons and Shaolins in this game or simulation and any passive holder (unit) should only react to the attacker (unit) etc.

Many thanks in advance for your support and contribution.

share|improve this question
    
I don't know Unity, but if the event framework you're describing is part of C# rather than Unity, you might have better luck asking on StackOverflow. –  Anko Jun 26 at 9:41
    
@Anko: Events are indeed part of C# rather than unity, but they don't look like this. I may be sensing some java experience in this question. In the end though, a global subscribe/publish model has a number of issues that need to be addressed. –  Magus Jun 26 at 15:05
add comment

1 Answer 1

It's not clear to me exactly how you're handling this now, but in C# with Unity you can use Events to get the job done. For example, you'll create your event handler (it doesn't have to follow the object sender, EffectEventArgs e format if you don't want it to):

public delegate void HandleAbilityEvent(BaseAbility ab, Unit caster);

This is a delegate method because we're going to treat it like a variable that's going to be passed around. There's no body because we're just telling C# what we want the signature of these delegates to look like. Next we create our event:

public event HandleAbilityEvent TriggerAbilityEvent;

This is an event is specific to the instance of the object that it's called on. For example, to use this we might do something like:

//When a target enters the range of our abilities, they can start listening to our events,
// you can use something like a trigger collider to detect when something has entered the
// range of your abilities
this.TriggerAbilityEvent += target.GetAbilityListener();

//Or when the target leaves our ability range

this.TriggerAbilityEvent -= target.GetAbilityListener();

Where each unit would also have their listener:

public void AbilityEventTriggered(BaseAbility ab, Unit caster) {
    //Notice the above signature matches the types of the signature of our delegate
    //Calculate events based on our defenses and possible passive abilities
    //this code is called inside the target, so we have everything needed
}

public HandleAbilityEvent GetAbilityListener() {
    //Because our signature matches the delegate signature, we can pass
    // this method back as a HandleAbilityEvent 
    return AbilityEventTriggered;
}

You can find a more detailed explanation of events in this video I made (sorry, it's not free, but the site has a lot of useful info if you decide to sign up).

share|improve this answer
    
Thank you for the suggestion Byte56 but is not really what I'm looking for. By the way I know how delegates and events works in c# but I appreciate the explanation ^^. As I said before, every effect fire it own event and the target for example in this line : this.TriggerAbilityEvent -= target.GetAbilityListener(); is not defined at compile-time (on constructor set) the reason that we will never know who will attack us to react properly. As for the trigger it vary from one ability to another, it can be onRange-enter,onDeath etc. I think I will stick to static events after all :) thank you again –  Bakkoto Jun 27 at 9:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.