Skip to content

This is a simple library that helps you send data between modules that you want to keep independent. The library also includes a logger to help you debug signals.

License

Notifications You must be signed in to change notification settings

AlehZakharau/ReactiveMessenger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Reactive Messenger

reactive messenger, event bus, observer pattern

This is a simple library that helps you send data between modules that you want to keep independent. The library also includes a logger to help you debug signals.

I personally recommend using it between gameplay modules, services, and UI modules. However, it is possible to overuse it, which can decrease the readability of the code in the project and create difficulties with debugging.

Install via UPM

Add the git url in the unity package manager:

https://github.com/AlehZakharau/ReactiveMessenger.git?path=/Assets/ReactiveSystem

or Add the line below in the manifest

"com.alehzakharau.reactivesystem": "https://github.com/AlehZakharau/ReactiveMessenger.git?path=/Assets/ReactiveSystem",

How to start

To learn more, check the samples in the package.

You can use the EventBus like a singleton or create an instance and register it in dependency injection.

Create a signal

public struct MessageSignal
{
    public float Value;
    public string Message;
    
    public MessageSignal(string message, float value)
    {
        Message = message;
        Value = value;
    }
}

Register the signal in a reciver class

{
    EventBus.Instance.Subscribe<MessageSignal>(obj => Debug.Log(obj.Message));
    // or
    EventBus.Instance.Subscribe<MessageSignal>(OnMessageArrived);
}

private void OnMessageArrived(MessageSignal obj)
{
    Debug.Log(obj.Message);
    Debug.Log(obj.Value);
}

Send the signal from a sender class

private void Start()
{
    EventBus.Instance.Fire(new MessageSignal("Hello world", 2));
}

Unsubscribe

If you no longer want to receive messages, you can unsubscribe.

{
    EventBus.Instance.UnSubscribe<MessageSignal>(OnMessageArrived);
}

Logger

Press "Tools/EventBusLogActivator" to activate the logger. The logger will send messages about Fire, Subscribe, and Unsubscribe to the console.

Screenshot 2023-04-19 at 11 41 32 AM

Performance

I insist on not using it in an update loop.

Screenshot 2023-03-01 at 11 50 19 AM

Author

Licence

MIT License

About

This is a simple library that helps you send data between modules that you want to keep independent. The library also includes a logger to help you debug signals.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages