Search Unity

What are your patterns for communication between components?

Discussion in 'Scripting' started by mnarimani, Aug 19, 2019.

  1. mnarimani

    mnarimani

    Joined:
    Mar 27, 2017
    Posts:
    214
    There are a lot of ways to communicate between components.
    When you need to find the target component. You can do this with GetComponent or FindObjectOfType or having a serialized field of the target type. How do you find your dependencies when writing a component?

    How do you send commands to the target component?
    When you want to send commands to multiple listeners, Do you use events or interfaces?
    If you are using events, Are you using Unity Events or C# native events? How do you manage event subscriptions and unsubscriptions?
    If your using interfaces, who is responsible for submitting the receiver class as a listener? Is receiver itself is responsible to submitting itself to the sender or sender finds all of its listeners?
     
  2. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    551
    Some will say there is an ideal way, but there are several contradicting theories.

    What matters most is the global architecture and that it is streamlined, makes sens, is efficient and enjoyable to work with.

    Personnally I try to rely on events fueled by my ScriptableObject framework as much as possible, so all of the inter component communication part is generic and pre-existing.(if you're curious is there https://github.com/met44/uStableObject , there are others similar with more documentation so in case you're looking for the popular kid feel free to ignore USO)
     
unityunity