# Subscribing

Binds a given callback function to the specified topic(s). The callback will be executed upon any object sending event with matching topic.

There are three ways to subscribe:

1. Subscribe to single topic with delegate
2. Subscribe to all topics with delegate
3. Subscribe to all topics as an interface implementer

Single object can subscribe multiple times to multiple topics using single or many callback functions. There is virtually no limitation other than your sanity.

The callback function must take in two parameters: Topic of type `FGameplayTag` and Payload of type `UObject*`, and have no return type (`void`).

#### Single topic with delegate

Achieved by calling `SubscribeToTopic` function and providing the callback function. The function returns a handle to the created binding.

{% tabs %}
{% tab title="Blueprint" %}
<figure><img src="https://1181272575-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfQy1REqz5GdSLZ1Mbpoo%2Fuploads%2FGBe1dz6ep3cTojJaiNzm%2Fimage.png?alt=media&#x26;token=c2582dfe-d819-425e-a3dc-35ae892975df" alt=""><figcaption></figcaption></figure>
{% endtab %}

{% tab title="C++" %}
```cpp
UDispatchitoSubsystem* subsystem = GEngine->GetEngineSubsystem<UDispatchitoSubsystem>();

TWeakObjectPtr<UMyClass> subscriber = NewObject<UMyClass>();

FDispatchitoDelegate callback;

callback.BindUObject(subscriber.Get(), &UMyClass::CallbackMethod);

FDispatchitoEventBindingHandle handle = subsystem->SubscribeToTopic(eventTopic, callback);
```

{% hint style="warning" %}
Callback has to be bound to UObject that represents callback's lifetime scope. If you bind simple lambda or a static function, the cleanup won't be able to remove the entry from the invocation list.&#x20;
{% endhint %}
{% endtab %}
{% endtabs %}

#### All topics with delegate

Achieved by calling `SubscribeToAllTopics` function and providing the callback function. This allows to have a single handler for all topics. This is great for "catch 'em all" scenario or when need to filter/decide which events to respond to at runtime.

{% tabs %}
{% tab title="Blueprint" %}
<figure><img src="https://1181272575-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfQy1REqz5GdSLZ1Mbpoo%2Fuploads%2F0r92YVOMr0Z5oOLX1Jmq%2Fimage.png?alt=media&#x26;token=5fc9036e-381c-46e6-893e-8fb080b0b1e9" alt=""><figcaption></figcaption></figure>
{% endtab %}

{% tab title="C++" %}
```cpp
UDispatchitoSubsystem* subsystem = GEngine->GetEngineSubsystem<UDispatchitoSubsystem>();

TWeakObjectPtr<UMyClass> subscriber = NewObject<UMyClass>();

FDispatchitoDelegate callback;

callback.BindUObject(subscriber.Get(), &UMyClass::CallbackMethod);

subsystem->SubscribeToAllTopics(callback);
```

{% hint style="warning" %}
Callback has to be bound to UObject that represents callback's lifetime scope. If you bind simple lambda or a static function, the cleanup won't be able to remove the entry from the invocation list.&#x20;
{% endhint %}
{% endtab %}
{% endtabs %}

#### All topics as an interface implementer

Achieved by calling `SubscribeToAllTopicsAsInterface` function. The caller has to implement `IDispatchitoSubscriberInterface` which provides the `OnEvent` function acting as a callback.

{% tabs %}
{% tab title="Blueprint" %}
<figure><img src="https://1181272575-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfQy1REqz5GdSLZ1Mbpoo%2Fuploads%2FfvYVhkjWuCvrzB95bQu5%2Fimage.png?alt=media&#x26;token=8ed6ab38-1e2e-409b-88c9-ec9a7f77ed62" alt=""><figcaption></figcaption></figure>
{% endtab %}

{% tab title="C++" %}
Define class implementing the interface:

```cpp
UCLASS()
class USubscriberWithInterface : public UObject, public IDispatchitoSubscriberInterface
{
    GENERATED_BODY()

public:
    virtual void OnEvent_Implementation(FGameplayTag Tag, UObject* Payload) override
    {
        // event handling logic
    }
};
```

Subscribe:

```cpp
UDispatchitoSubsystem* subsystem = GEngine->GetEngineSubsystem<UDispatchitoSubsystem>();

USubscriberWithInterface* subscriber = NewObject<USubscriberWithInterface>();

subsystem->SubscribeToAllTopicsAsInterface(subscriber);
```
{% endtab %}
{% endtabs %}
