Skip to content

Commit 2e93607

Browse files
authored
Interface Compatibility for Signals (#91)
* Interface compatibility for signals * Fixed acces to unity api Removed async aproach for AbstractFire in intefraces * Fixed typo * Redundant Comments removed comments removed since documentation will give info about * Abstract Signals Documentation Documentation explaining the use of Signals with Interfaces and advantages of using them
1 parent eac40ce commit 2e93607

File tree

3 files changed

+183
-0
lines changed

3 files changed

+183
-0
lines changed

Documentation/Signals.md

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* <a href="#signalbusinstaller">SignalBusInstaller</a>
1212
* <a href="#when-to-use-signals">When To Use Signals</a>
1313
* Advanced
14+
* <a href="#abstract-signals">Abstract Signals</a>
1415
* <a href="#use-with-subcontainers">Signals With Subcontainers</a>
1516
* <a href="#async-signals">Asynchronous Signals</a>
1617
* <a href="#settings">Signal Settings</a>
@@ -479,6 +480,152 @@ These are just rules of thumb, but useful to keep in mind when using signals. T
479480

480481
When event driven program is abused, it is possible to find yourself in "callback hell" where events are triggering other events etc. and which make the entire system impossible to understand. So signals in general should be used with caution. Personally I like to use signals for high level game-wide events and then use other forms of communication (unirx streams, c# events, direct method calls, interfaces) for most other things.
481482

483+
## <a id="abstract-signals"></a>Abstract Signals
484+
485+
One of the problems of the signals is that when subscribe to their concrete types
486+
you are coupling your concrete signal types to the subscribers
487+
488+
For example, Lets say I have a player and i want to save the game when i finish a level.
489+
Ok easy, I create ``SignalLevelCompleted`` and then I subscribe it to my ``SaveGameSystem``
490+
then I also want to save when i reach a checkpoint, again i create ``SignalCheckpointReached``
491+
and then I subscribe it to my ``SaveGameSystem``
492+
you are begining to get something like this...
493+
```csharp
494+
public class Example
495+
{
496+
SignalBus signalBus;
497+
public Example(Signalbus signalBus) => this.signalBus = signalBus;
498+
499+
public void CheckpointReached() => signalBus.Fire<SignalCheckpointReached>();
500+
501+
public void CompleteLevel() => signalBus.Fire<SignalLevelCompleted>();
502+
}
503+
504+
public class SaveGameSystem
505+
{
506+
public SaveGameSystem(SignalBus signalBus)
507+
{
508+
signalBus.Subscribe<SignalCheckpointReached>(x => SaveGame());
509+
signalBus.Subscribe<SignalLevelCompleted>(x => SaveGame());
510+
}
511+
512+
void SaveGame() { /*Saves the game*/ }
513+
}
514+
515+
//in your installer
516+
Container.DeclareSignal<SignalLevelCompleted>();
517+
Container.DeclareSignal<SignalCheckpointReached>();
518+
519+
//your signal types
520+
public struct SignalCheckpointReached{}
521+
public struct SignalLevelCompleted{}
522+
```
523+
524+
And then you realize you are coupling the types``signalLevelCompleted`` and ``SignalCheckpointReached``to ``SaveGameSystem``.
525+
``SaveGameSystem`` shouldn't know about those "non related with saving" events...
526+
527+
So let's give the power of interfaces to signals!
528+
So i have the ``SignalCheckpointReached`` and ``SignalLevelCompleted`` both implementing **``ISignalGameSaver``**
529+
and my ``SaveGameSystem`` just Subscribes to **``ISignalGameSaver``** for saving the game
530+
So when i fire any of those signals the ``SaveGameSystem`` saves the game.
531+
Then you have something like this...
532+
```csharp
533+
public class Example
534+
{
535+
SignalBus signalBus;
536+
public Example(Signalbus signalBus) => this.signalBus = signalBus;
537+
538+
public void CheckpointReached() => signalBus.AbstractFire<SignalCheckpointReached>();
539+
540+
public void CompleteLevel() => signalBus.AbstractFire<SignalLevelCompleted>();
541+
}
542+
543+
public class SaveGameSystem
544+
{
545+
public SaveGameSystem(SignalBus signalBus)
546+
{
547+
signalBus.Subscribe<ISignalGameSaver>(x => SaveGame());
548+
}
549+
550+
void SaveGame() { /*Saves the game*/ }
551+
}
552+
553+
//in your installer
554+
Container.DeclareSignalWithInterfaces<SignalLevelCompleted>();
555+
Container.DeclareSignalWithInterfaces<SignalCheckpointReached>();
556+
557+
//your signal types
558+
public struct SignalCheckpointReached : ISignalGameSaver{}
559+
public struct SignalLevelCompleted : ISignalGameSaver{}
560+
561+
public interface ISignalGameSaver{}
562+
```
563+
564+
Now your ``SaveGameSystem`` doesnt knows about CheckPoints nor Level events, and just reacts to signals that save the game.
565+
The main difference is in the Signal declaration and Firing
566+
- ``DeclareSignalWithInterfaces`` works like ``DeclareSignal`` but it declares the interfaces too.
567+
- ``AbstractFire`` is the same that ``Fire`` but it fires the interfacesjust if you have Declared the signal with interfaces
568+
otherwise it will throw an exception.
569+
570+
Ok, let's show even more power.
571+
Now i create another signal for the WorldDestroyed Achievement "SignalWorldDestroyed"
572+
But i also want my SoundSystem to play sounds when i reach a checkpoint and/or unlock an Achievement
573+
So the code could look like this.
574+
```csharp
575+
public class Example
576+
{
577+
SignalBus signalBus;
578+
public Example(Signalbus signalBus) => this.signalBus = signalBus;
579+
580+
public void CheckpointReached() => signalBus.AbstractFire<SignalCheckpointReached>();
581+
582+
public void DestroyWorld() => signalBus.AbstractFire<SignalWorldDestroyed>();
583+
}
584+
585+
public class SoundSystem
586+
{
587+
public SoundSystem(SignalBus signalBus)
588+
{
589+
signalBus.Subscribe<ISignalSoundPlayer>(x => PlaySound(x.soundId));
590+
}
591+
592+
void PlaySound(int soundId) { /*Plays the sound with the given id*/ }
593+
}
594+
595+
public class AchievementSystem
596+
{
597+
public AchievementSystem(SignalBus signalBus)
598+
{
599+
signalBus.Subscribe<ISignalAchievementUnlocker>(x => UnlockAchievement(x.achievementKey));
600+
}
601+
602+
void UnlockAchievement(string key) { /*Unlocks the achievement with the given key*/ }
603+
}
604+
605+
//in your installer
606+
Container.DeclareSignalWithInterfaces<SignalCheckpointReached>();
607+
Container.DeclareSignalWithInterfaces<SignalWorldDestroyed>();
608+
609+
//your signal types
610+
public struct SignalCheckpointReached : ISignalGameSaver, ISignalSoundPlayer
611+
{
612+
public int SoundId { get => 2} //or configured in a scriptable with constants instead of hardcoded
613+
}
614+
public struct SignalWorldDestroyed : ISignalAchievementUnlocker, ISignalSoundPlayer
615+
{
616+
public int SoundId { get => 4}
617+
public string AchievementKey { get => "WORLD_DESTROYED"}
618+
}
619+
620+
//Your signal interfaces
621+
public interface ISignalGameSaver{}
622+
public interface ISignalSoundPlayer{ int SoundId {get;}}
623+
public interface ISignalAchievementUnlocker{ string AchievementKey {get;}}
624+
```
625+
626+
It offers a lot of modularity and abstraction for signals,
627+
you fire a concrete signal telling what you did and give them functionality trough Interface implementations
628+
482629
## <a id="use-with-subcontainers"></a>Signals With Subcontainers
483630

484631
Signals are only visible at the container level where they are declared and below. For example, you might use Unity's multi-scene support and split up your game into a GUI scene and an Environment scene. In the GUI scene you might fire a signal indicating that the GUI popup overlay has been opened/closed, so that the Environment scene can pause/resume activity. One way of achieving this would be to declare a signal in a ProjectContext installer (or a shared <a href="../README.md#scene-parenting">scene parent</a>), then subscribe to it in the Environment scene, and then fire it from the GUI scene.

UnityProject/Assets/Plugins/Zenject/OptionalExtras/Signals/Internal/Binders/SignalExtensions.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,22 @@ public static DeclareSignalIdRequireHandlerAsyncTickPriorityCopyBinder DeclareSi
3030
return container.DeclareSignal(typeof(TSignal));
3131
}
3232

33+
public static DeclareSignalIdRequireHandlerAsyncTickPriorityCopyBinder DeclareSignalWithInterfaces<TSignal>(this DiContainer container)
34+
{
35+
Type type = typeof(TSignal);
36+
37+
var declaration = container.DeclareSignal(type);
38+
39+
Type[] interfaces = type.GetInterfaces();
40+
int numOfInterfaces = interfaces.Length;
41+
for (int i = 0; i < numOfInterfaces; i++)
42+
{
43+
container.DeclareSignal(interfaces[i]);
44+
}
45+
46+
return declaration;
47+
}
48+
3349
public static BindSignalIdToBinder<TSignal> BindSignal<TSignal>(this DiContainer container)
3450
{
3551
var signalBindInfo = new SignalBindingBindInfo(typeof(TSignal));

UnityProject/Assets/Plugins/Zenject/OptionalExtras/Signals/Main/SignalBus.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,26 @@ public int NumSubscribers
4949
get { return _subscriptionMap.Count; }
5050
}
5151

52+
53+
//Fires Signals with their interfaces
54+
public void AbstractFire<TSignal>() where TSignal : new() => AbstractFire(new TSignal());
55+
public void AbstractFire<TSignal>(TSignal signal) => AbstractFireId(null, signal);
56+
public void AbstractFireId<TSignal>(object identifier, TSignal signal)
57+
{
58+
// Do this before creating the signal so that it throws if the signal was not declared
59+
Type signalType = typeof(TSignal);
60+
var declaration = GetDeclaration(signalType, identifier, true);
61+
declaration.Fire(signal);
62+
63+
Type[] interfaces = signalType.GetInterfaces();
64+
int numOfInterfaces = interfaces.Length;
65+
for (int i = 0; i < numOfInterfaces; i++)
66+
{
67+
declaration = GetDeclaration(interfaces[i], identifier, true);
68+
declaration.Fire(signal);
69+
}
70+
}
71+
5272
public void LateDispose()
5373
{
5474
if (_settings.RequireStrictUnsubscribe)

0 commit comments

Comments
 (0)