> For the complete documentation index, see [llms.txt](https://pahutyakv.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://pahutyakv.gitbook.io/docs/evently/how-to-use.md).

# How To Use

### <mark style="color:$success;">Subscribing to Events</mark>

To listen for an event, use `Subscribe<T>()`.\
This registers a handler that will be invoked every time the specified event type is published.

Example:

```csharp
// Subscribe to the PlayerDied event
Evently.Subscribe<PlayerDied>(OnPlayerDied);

// Handler method
private void OnPlayerDied(PlayerDied evt)
{
    Debug.Log($"Player died with health {evt.Health}");
}
```

In this example:

* `PlayerDied` represents the event type
* `OnPlayerDied` is the handler that reacts to the event
* the handler will be called every time the event is published

The `Subscribe()` method returns an `EventlyListener<T>` instance, which can optionally be used to configure the listener (for example with `.Once()`, `.Where()` or `.BindTo()`).

Events in Evently can be represented by **either classes or structs**, depending on the needs of the system.

***

### <mark style="color:$success;">Publishing Events</mark>

To dispatch an event to all registered listeners, use `Publish<T>()`.

Example:

```csharp
Evently.Publish(new PlayerDied { Health = 0 });
```

When an event is published:

1. Evently locates all listeners subscribed to the event type
2. Each listener receives the event instance
3. All registered handlers are invoked

***

### <mark style="color:$success;">Unsubscribe Manually</mark>

If a system no longer needs to listen for an event, the handler can be removed using `Unsubscribe<T>()`.

Example:

```csharp
Evently.Unsubscribe<PlayerDied>(OnPlayerDied);
```
