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

Using events for communication

Discussion in 'Scripting' started by vee41, Apr 4, 2016.

  1. vee41

    vee41

    Joined:
    Nov 14, 2013
    Posts:
    32
    Hi,

    I'm having a problem wrapping my head around on how should I effectively use events to decouple objects.

    For example when unit is clicked few things could happen:
    - ui panel should appear and display some information about the unit
    - sound is played
    - selection box is displayed around the unit
    Etc.

    So using an event where unit shouts "hey i was selected!" and listeners react to that seems like a nice way to handle this.

    My biggest issue is, how should I handle adding those listeners? If I have 100 units do i add listeners for every one of those, what would be the good practice here?

    One way i was thinking of handling this is using message center, where unit sends it messages to singleton object and listeners have registered to the object to receive certain types of messages. Are there any downsides in this approach i am missing?
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    All those units would be setup to send a "selection" event, the listeners then listen out for "selection" events. There would be one type of event to listen for, not 100.
     
  3. vee41

    vee41

    Joined:
    Nov 14, 2013
    Posts:
    32
    But are listeners not setup per object instance? So each unit would have it's own set of listeners listening to events. What you are describing is more like broadcasting which I understood to be not so good practice.

    What I'd love is simple way to define events ie. "Event Selected(GameObject selected)" and for objects to send and receive these events.

    EDIT: So I think the question is, are UnityEvents designed for this kind of pattern? Or should I use something custom like Message Center I mentioned above?
     
    Last edited: Apr 4, 2016
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    I think you mean per 'instance', not per 'class'.

    Well, technically, your 'singleton' is basically the same thing, and embodies other supposed "bad practices".

    Of course, as long as you understand the implications of the design, it's not like it's going to kill you if you use such patterns.

    Honestly, your singleton idea is 'sort of' the way I'd go.

    I might not make it a Singleton... but I'd basically have a "Unit Manager" that knows about all the units, and it would forward events about if a unit is clicked or the sort. It's simple and gets the job done.
     
    vee41 likes this.
  5. vee41

    vee41

    Joined:
    Nov 14, 2013
    Posts:
    32
    Cheers! I think I'll go with that approach, like you said it gets the job done and fits the way I think about the whole thing.