Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Multiple GameObjects with the same EventManager: component based programming

Discussion in 'Scripting' started by tobad, Mar 21, 2015.

  1. tobad

    tobad

    Joined:
    Mar 12, 2015
    Posts:
    90
    current Situation:

    mulitple Player GameObjects, each has

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EventManager : MonoBehaviour
    5. {
    6.     public delegate void TriggerAction(GameObject killer, GameObject victim);
    7.  
    8.     public static event TriggerAction OnTriggerKill;
    9.  
    10.     void OnTriggerEnter2D(Collider2D other)
    11.     {
    12.         if(other.gameObject.layer == layer.head)
    13.         {
    14.             if(OnTriggerKill != null)
    15.             {
    16.                 OnTriggerKill(this.gameObject, other.transform.parent.gameObject);
    17.             }
    18.         }
    19.     }
    20. }

    and

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerScript : MonoBehaviour
    5. {
    6.     Animator anim;
    7.  
    8.     void Start()
    9.     {
    10.         anim = GetComponent<Animator>();
    11.     }
    12.  
    13.     void OnEnable()
    14.     {
    15.         EventManager.OnTriggerKill += Kill;
    16.     }
    17.  
    18.  
    19.     void OnDisable()
    20.     {
    21.         EventManager.OnTriggerKill -= Kill;
    22.     }
    23.  
    24.  
    25.     void Kill(GameObject killer, GameObject victim)
    26.     {
    27.     if(this.gameObject == victim)
    28.         KillAnimation();
    29.     }
    30.  
    31.     void KillAnimation()
    32.     {
    33.     anim.SetTrigger("kill");
    34.     }
    35. }

    the one GameController in scene has


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GameControllerScript : MonoBehaviour
    5. {
    6.     void OnEnable()
    7.     {
    8.         EventManager.OnTriggerKill += Kill;
    9.     }
    10.  
    11.  
    12.     void OnDisable()
    13.     {
    14.         EventManager.OnTriggerKill -= Kill;
    15.     }
    16.  
    17.  
    18.     void Kill(GameObject killer, GameObject victim)
    19.     {
    20.     //add points
    21.     playerDictionary.AddPoint(killer.GetComponent<Owner>().owner);
    22.     playerDictionary.AddDeath(victim.GetComponent<Owner>().owner);
    23.     }
    24. }

    is this a design issue if you are using multiple instances of the same eventmanager with one static event?
    or is this example exactly how it's meant to be used?
    i tested it and it worked, but i want to be sure that this is not a lucky coincidence.

    is a event called in the same frame or does it wait in a eventqueue to get called?
     
  2. tobad

    tobad

    Joined:
    Mar 12, 2015
    Posts:
    90
    *bump*

    @L-Tyrosine
    @KelsoMRK
    @Suddoha
    @vexe

    you directed other users in using events to keep gameobjects independent (component based programming design)

    i hope you can help me with the "using multiple instances of the same eventmanager with one static event" question.

    big thanks.
     
  3. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    That pattern really doesn't make sense to me. You're firing the event for everyone and then checking if it should apply to each specific thing instead of just firing an event on the thing that entered the trigger. What are you gaining by making this an event in the first place? It looks like you're just using it to do the same job that maintaining a list would do.

    Long a short of it is - the whole thing seems backwards to me.
     
  4. L-Tyrosine

    L-Tyrosine

    Joined:
    Apr 27, 2011
    Posts:
    305
    Events are a C#/.net feature, there's no relation with Unity Engine, so as any other procedure, it will be called synchronous (looping trough every registered listener in sequence).

    Your event manager looks good to me. GameController and PlayerScript are decoupled. AFAIK there's no problem using static events. Just be sure to unregister every observer as it will hold the object in memory otherwise (you are doing ok at OnDisable) - garbage collector won't clear objects with reference somewhere.

    Please note however that this is unrelated to component pattern (it's more observer/dispatcher pattern). It's ok it's just a matter of designation. Anyway you may want to read more about the component pattern to be able to see the possibilities of entity/behaviour separation (so you can write more specific behaviour and mix then in different entities reusing a lot of code).
     
    tobad likes this.