# Creating an Event Through Code

## Creating an Event Through Code

Emerald AI's Unity Events are located on the EmeraldEvents script. To access these, you will need a reference to the EmeraldEvents script. This can be done using the code below. It is best to have a class level variable for EventsComponent so it can be accessed anywhere within your script.

```csharp
EventsComponent = GetComponent<EmeraldEvents>();
```

You can add events programmatically using the following code format. These should be applied on Start.

{% code fullWidth="false" %}

```csharp
EventsComponent.TheEmeraldAIUnityEvent.AddListener(() => { YourFunctionToCall(); });
```

{% endcode %}

For example, lets say you want to track each AI death and add it to a static variable so the player can track their kills. For this example, the kill count script name is KillCounterSystem and the static variable is an int called AmountOfKills.

{% code fullWidth="false" %}

```csharp
...
void Start ()
{
   //Create an OnDeathEvent by adding a listener and applying the CountDeath function.
   //Even though this is assigned on start, the CountDeath function will be called when the AI dies.
   EventsComponent.OnDeathEvent.AddListener(() => { CountDeath(); });
}

//The CountDeath function adds the AI's death to the static variable AmountOfKills for tracking kills.
void CountDeath ()
{
   KillCounterSystem.AmountOfKills++;
}
...
```

{% endcode %}

***

## All Events with Examples

#### OnDeathEvent

The OnDeathEvent is invoked when an AI is killed.

Example:

```csharp
EventsComponent.DeathEvent.AddListener(() => { YourCustomFunction(); });
```

***

#### OnTakeDamageEvent

The OnTakeDamageEvent is invoked each time an AI is damaged.

Example:

```csharp
EventsComponent.DamageEvent.AddListener(() => { YourCustomFunction(); });
```

***

#### OnDoDamageEvent

The OnDoDamageEvent is invoked each time an AI deals damage for both Melee and Ranged weapon types.

Example:

```csharp
EventsComponent.OnDoDamageEvent.AddListener(() => { YourCustomFunction(); });
```

***

#### OnReachedDestinationEvent

The OnReachedDestinationEvent is invoked when an AI reaches their destination location (must be using the Destination Wander Type).

Example:

```csharp
EventsComponent.ReachedDestinationEvent.AddListener(() => { YourCustomFunction(); });
```

***

#### OnStartEvent

The OnStartEvent is invoked once within the AI's Start function.

Example:

```csharp
EventsComponent.OnStartEvent.AddListener(() => { YourCustomFunction(); });
```

***

#### OnEnabledEvent

The OnEnabledEvent is invoked when an AI is enabled after it has been deactivated.

Example:

```csharp
EventsComponent.OnEnabledEvent.AddListener(() => { YourCustomFunction(); });
```

***

#### OnPlayerDetectionEvent

The OnPlayerDetectionEvent is invoked when an AI detects a player object. Users can adjust the cooldown for how often this event is invoked within the Emerald AI editor.

Example:

```csharp
EventsComponent.OnPlayerDetectionEvent.AddListener(() => { YourCustomFunction(); });
```

***

#### OnAttackEvent

The OnAttackEvent is invoked when an AI triggers their attack animation.

Example:

```csharp
EventsComponent.OnAttackEvent.AddListener(() => { YourCustomFunction(); });
```

***

#### OnFleeEvent

The OnFleeEvent is invoked when an AI begins to flee (This only happens with AI that have a Brave or Coward Confidence Level).

Example:

```csharp
EventsComponent.OnFleeEvent.AddListener(() => { YourCustomFunction(); });
```

***

#### OnStartCombatEvent

The OnStartCombatEvent is invoked when an AI first enters combat and is reset when the battle is over.

Example:

```csharp
EventsComponent.OnStartCombatEvent.AddListener(() => { YourCustomFunction(); });
```

***

#### OnEndCombatEvent

The OnEndCombatEvent is invoked when the AI ends combat and there are no detectable enemy targets nearby.

Example:

```csharp
EventsComponent.OnEndCombatEvent.AddListener(() => { YourCustomFunction(); });
```

#### OnKillTargetEvent

The OnKillTargetEvent is invoked when an AI has killed their target. This event is called before an AI's target has been cleared so it can still be accessed if needed.

Example:

```csharp
EventsComponent.OnKillTargetEvent.AddListener(() => { YourCustomFunction(); });
```
