2

I am trying to learn how to work with Zenject's and unity and I have encountered a particular problem that I do not know if it has a possible solution solely using Zenject's api.

Let assume i have MethodA, MethodB and MethodC, and a SignalA.

Is it possible to make this sequence:

SignalA.Fire() => MethodA (until released/finished)

               => MethodB (until released/finished)

               => MethodC (until released/finished)

Right now i have this pice of code :

private void BindSignalA()
{

    Container.DeclareSignal<SignalA>().RunSync();
                    
    Container.BindSignal<SignalA>().ToMethod<MethodA>(handler => handler.Execute).FromNew();
    Container.BindSignal<SignalA>().ToMethod<MethodB>(handler => handler.Execute).FromNew();
    Container.BindSignal<SignalA>().ToMethod<MethodC>(handler => handler.Execute).FromNew();
}

And MethodA looks like this :

public class MethodA
{     
    public async void Execute()
    {
        await new WaitUntil(() => false);
    }
}

So the expected result is that MethodB and MethodC will be never executed.

But the actual result is that all Methods are executed.

Is there a solution using Zenject's api to make this happen?

Thanks for the help, and in case any more information is needed I would love to know.

Guy Chriqui
  • 53
  • 1
  • 6

1 Answers1

0

I am not familiar with signals, but checking the documentation , mainly the the 3rd point into consideration maybe your case is not good scenario for signal use.

When To Use Signals Signals are most appropriate as a communication mechanism when:

  • There might be multiple interested receivers listening to the signal
  • The sender doesn't need to get a result back from the receiver
  • The sender doesn't even really care if it gets received. In other words, the sender should not rely on some state changing when the signal is called for subsequent sender logic to work correctly. Ideally signals can be thought as "fire and forget" events
  • The sender triggers the signal infrequently or at unpredictable times

On the other hand I you might be confusing decoupling the code with the syncronous execution concept. Delegates or events (which are a specific type of delegate) are spare pieces of logic of one class that you can keep so that can be executed in other class, so that you can "subscribe" or "listen" to something that might happen to execute/invoke your event in other part of the code. However that does not involve any asyncronous code or multithreading.

As far as I can guess, signals are used to handle event subscription/invocation in a decoupled manner with the Zenject signal object, in a similar way as dependencies between classes are handled in a decoupled manner with interfaces, so first I would check if your case is suitable for their use checking the documentation carefully and following the examples provided along until the concept clicks.
Meanwhile, I would first try to use c# normal delegates so that after the zenject signal misson can be understood. Also providing a simple working example of what you are trying to do without zenject would be very helpful as departing point to achieve the zenject way of achieving that same thing.

rustyBucketBay
  • 4,320
  • 3
  • 17
  • 47