# EventManager

### Using the EventManager

You can use event manager to handle the game state and game events

To get game state use **EventManager.GameStatus**:

```csharp
EventManager.GameStatus == EStatus.Play
```

To set game state use **EventManager.SetGameStatus**

```csharp
EventManager.SetGameStatus(EStatus.Play);
```

The EventManager supports events with different argument types. There are different `GetEvent` methods that you can use to get a reference to the event of your choosing.

#### Subscribing to an event

To subscribe to an event, you first need to retrieve the event object using the `GetEvent` method, and then use the `Subscribe` method on the event, passing the function you want to be called when the event is triggered.

```csharp
EventManager.GetEvent<BallCollectEventArgs>(EGameEvent.BallDestroyed).Subscribe(ShowExplosionEffect);
```

In this example, `ShowExplosionEffect` is a method in your class that matches the signature of the delegate type defined by the event. When the `BallDestroyed` event is triggered, `ShowExplosionEffect` will be called.

The method you subscribe should have the same argument types as the Event, for example, if the event is `Event<BallCollectEventArgs>` then the method you subscribe should take `BallCollectEventArgs` as an argument.

#### Unsubscribing from an event

It's good practice to unsubscribe from events when they're not needed, like when the object is disabled. This helps to prevent unwanted side effects and improve performance.

You can unsubscribe from an event in the same way you subscribe but use the `Unsubscribe` method instead.

```csharp
EventManager.GetEvent<BallCollectEventArgs>(EGameEvent.BallDestroyed).Unsubscribe(ShowExplosionEffect);
```

In this example, `ShowExplosionEffect` will no longer be called when the `BallDestroyed` event is triggered.

#### Triggering an event

To trigger an event, you retrieve the event object using the `GetEvent` method and then use the `Trigger` method on the event, passing any arguments as required.

```csharp
EventManager.GetEvent<BallCollectEventArgs>(EGameEvent.BallDestroyed).Invoke(ballCollectEventArgs);
```

In this example, `ballCollectEventArgs` is the data you want to send to all the subscribers when the `BallDestroyed` event is triggered.

### Summary

The EventManager provides a powerful way to manage events in your game, and it's an important part of creating a flexible and modular game architecture. By subscribing, triggering, and unsubscribing to events, you can create a wide range of game behaviors without tightly coupling your game components.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.candy-smith.com/main/bubble-shooter-toolkit/scripts/scripts-overview/eventmanager.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
