Search Unity

  1. Unity 2020.2 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Architecture Question (Events)

Discussion in 'Scripting' started by Rob-Meade, May 17, 2019.

  1. Rob-Meade

    Rob-Meade

    Joined:
    Oct 27, 2016
    Posts:
    56
    Hi all,

    Embarking on a new project recently I've made best efforts to keep specific functionality to specific components which can then be added to a game object to add that desired behaviour.

    As an example, an enemy space ship, one component for its attack, one component for its movement. Additionally, one component to effectively describe (Actor) the thing itself.

    I have been trying my best to keep everything isolated and working independently, but as the project grew I found myself needing to be able to communicate between components and components on other game objects.

    As an example, when the game starts, I may need to say "Stop shooting" and "Stop moving, return to your initial positions" - this would be a communication from the main game controller to two components on the enemy space ship.

    I decided to use events, which initially was working quite well, but things have got a little more complicated and I was hoping to get a "second opinion" on a few things...

    One of the aspects I'm trying to get my head around is whether the individual components should be listening for the events, or, whether perhaps just the main "thing" component should listen and then manage the communication to any other attached components itself.

    As an example, if the game controller raises and event for the game to start, an enemy space ship could have its movement and attack components listening for that event, so that they can respond in due course. But should they? Or, should the main enemy space ship component receive that event and then communicate between the components.

    It has begun to get quite challenging when trying to keep events not caring who receives them, and components not knowing about things they dont need to know about.

    The challenge is replicated in the other direction also, if for example the player is destroyed, that event is picked up by the game controller, which then raises an event which is picked up by the ui controller (updates lives/score etc). Again, should these components all be listening individually, or, in a more controlled manner. I've been leaning more towards the controlled manner but it has led to having to "bubble up" events, thus firing more events, so there's a cost to doing this also.

    Any thoughts/comments on my ramblings are appreciated :)

    Cheers,

    Rob
     
    Last edited: May 18, 2019
  2. Rob-Meade

    Rob-Meade

    Joined:
    Oct 27, 2016
    Posts:
    56
    No one have any thoughts on the above? All thoughts/suggestions/ideas are welcomed :)
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    7,367
    A lot of us aren't super active on the weekends. Friday night is sometimes not a good time to post questions geared towards more experienced developers.

    Case in point, I'm only stopping in to take a look around, but I currently don't have the time to go through your post (I'm working on my roof all weekend... just took a break to have a glass of lemonade).
     
    Rob-Meade likes this.
  4. Rob-Meade

    Rob-Meade

    Joined:
    Oct 27, 2016
    Posts:
    56
    *tut* - people with lives eh, what ever next! :)

    Thanks for the heads-up - and good luck with the roof :)
     
    Last edited: May 20, 2019
unityunity