0
\$\begingroup\$

Note:

I have two Managers: First one creates 2d block upon click. The other one, upon click, grabs a block for dragging. The reason I need them to run in specific order is because newly created block should be instantly grabbed.

Original question:

I have two managers that respond to the InputManager events. They both should do something when InputManager raises mouse0DownEvent. I need the BlockManager delegate to happen first. I have DragManager and BlockManager that subscribe to same event in InputManager: mouse0DownEvent, but this does not give me controll over the execution order.

My ideas:

  1. I am thinking of creating separate event in InputManager, but then InputManager would have to know about the other managers which would cause dehermetization.

  2. I could also create separate manager to wrap InputManager events but this would look like overengineering to me.

Whould would be a unity way of doing this?

Thanks!

\$\endgroup\$
2
  • 3
    \$\begingroup\$ The usual pattern is that you design different managers in a way that they don't have execution order dependencies like that... Why exactly do you have to process one before the other? \$\endgroup\$
    – Philipp
    Commented Dec 13, 2021 at 12:43
  • \$\begingroup\$ @Philipp First one creates 2d block upon click. The other one, upon click, grabs a block for dragging. The reason I need them to run in specific order is becasue newly created block should be instantly grabbed. \$\endgroup\$
    – hungry91
    Commented Dec 15, 2021 at 7:35

2 Answers 2

2
\$\begingroup\$

When you have multiple event receivers reacting to the same event but it still matters in which order they process an event, then that usually means that you are not actually dealing with one event but with two.

The first event being mouse0DownEvent handled by the BlockManager and the second a blockCreatedEvent which gets Invoked by the BlockManager and then needs to be handled by the DragManager.

This also solves two other problems:

  1. How you communicate the newly created block from the BlockManager to the DragManager: By passing it to the blockCreatedEvent.
  2. How you deal with the case that the player clicked in a situation where they are unable to create a block: You just don't invoke the second event.
\$\endgroup\$
1
  • \$\begingroup\$ Thank you! This helps a lot! \$\endgroup\$
    – hungry91
    Commented Dec 15, 2021 at 11:03
1
\$\begingroup\$

I did something similar by just having a tree of listeners. So the event is passed to the root of the tree, then it's processed and passed down to the children recursively.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ A wrapper around the InputManager that accepts listeners with a priority value and stuffs them into a priority queue / sorted list would also work. \$\endgroup\$
    – DMGregory
    Commented Dec 13, 2021 at 12:58
  • 2
    \$\begingroup\$ "There is no problem in software architecture you can't solve using another layer of indirection... except too many layers of indirection." - David Wheeler \$\endgroup\$
    – Philipp
    Commented Dec 13, 2021 at 13:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .