# Events

Events are ScriptableObjects which contain one event, either with one or no argument. You can think of them as channels that any object, from any scene, can tap into.

Additionally, they present a button in their Inspector that allows to fire the event on demand, for testing purposes.

### Sample usage 💡

In an action game, a player character Prefab could broadcast its death on a "OnPlayerDied" SO Event. Any object in the game that needs to react to it, all the way up to a game manager, can reference into that SO to react.

***

## Using Events

### Creating a new Event

To create a new Event, simply position your mouse in the Project View and right click, then **Create > ScriptableObject Tools > Events**, and then choose the type you want to create.

### Referencing an Event in scripts

Like usual, expose a public or serialised property of the right type:

```csharp
public VoidEvent playerDiedEvent;
// Or also
[SerializeField] private VoidEvent _playerDiedEvent;
```

### Listening to an event

The event contained in the ScriptableObject is not public, so to hook an object to an event, you can use the syntax as if the SO was the event itself.

```csharp
playerDiedEvent += RespawnCharacter;
// and later...
playerDiedEvent -= RespawnCharacter;
```

### Firing an event

To fire on a SO Event, use the `Invoke()` method, passing the correct arguments:

```csharp
playerDiedEvent.Invoke();
playerHealthChanged.Invoke(-3);
```

***

## Extending Events

Extending the Event system with a new class is simple. To create a new event that carries one, two or three arguments, all you need to do is to create a new C# file and subclass one of the existing implementations.

For instance, inheriting from `EventOneArg<T>` like this (we'll use `Collider` as an example):

```csharp
[CreateAssetMenu]
public class ColliderEvent : EventOneArg<Collider> { }
```

In cases like these, nothing else is needed. The event SO will also display the Invoke button automatically.

{% hint style="info" %}
If your event's arguments are of type Struct, don't forget to add the `[Serializable]` attribute to your Struct.
{% endhint %}

### New event with four or more arguments

It's also possible to add an event with four or more arguments, but this guide doesn't cover it. You can use `EventThreeArg<T>` as a guide. You might also want to write a custom Inspector for it, using for instance `EventThreeArgsEditor` as a reference.


---

# 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://tools.continis.io/scriptable-object-tools/components/events.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.
