# MEvents

## Overview

Events are Scriptable Objects can be used to Broadcast messages so they can be received by a [**MEventListener** ](/animal-controller/scriptable-architecture/scriptables/mevent-listener.md)in any GameObject. Is a fantastic tool to avoid dependencies like Singletons on your project.&#x20;

It is based on the [**Unite Austin 2017 - Game Architecture with Scriptable Objects**](https://www.youtube.com/watch?v=raQ3iHhE_Kk)**.**

### How to Create an Event

You can create a new **MEvent** using the Create Menu:

![](/files/-MEU9XDPKQgbxBuAtloj)

![](/files/-MRH5u0sIcHg1RmUERz2)

![](/files/-MRH5x6Ox5mo3WbRZL9U)

Every **event** can be used multiple times with different values like (Int, Float, String, Bool, Vector2, Vector3, Sprite, Transform, GameObject, Component, IntVar, IDs, FloatVar or  Void)

![](/files/-MKc3rCfitVMeuIjUYWp)

They are widely used all over the Animal Controller to notify changes between different systems.&#x20;

{% hint style="info" %}
When an Event is invoked using any of its invoke methods, every active listener will react accordingly. So use them wisely.
{% endhint %}

## Example

Let's create an Event that will notify the UI that the animal is dead.

1 - Prepare a simple scene with a plane, any of the animals player prefabs and the camera prefabs:

![](/files/-MTqwFCO1ECHs_PN4Nap)

2 - [Create ](/animal-controller/scriptable-architecture/scriptables/mevents.md#how-to-create-an-event)a new MEvent and call it Animal is dead.

![](/files/-MTqxB54hGNyCCoU7erS)

3 - On the Events Group -> State, create a new **On Enter**,  On Exit Event and add the StateID -> Death

![](/files/vf49m6KJyGm0OI9J7VSY)

4 - Add a new Listener on the OnEnter Event and use the is Animal Death MEvent asset.

![](/files/-MTqzomoAqJiGlrPZQWt)

5 - Use the Event method Invoke(string). As the value let's set the name of the wolf.&#x20;

{% hint style="info" %}
We are using the MEvent.Invoke( String ). Which will be the type that will use later on the Event listener component
{% endhint %}

6 - Create a Canvas and add a text component. clear the Text value of the text.

![](/files/-MTr11EfnPjk9SkX8Y5y)

7 - Add the EventListener Component and the [**Value to String**](/animal-controller/scriptable-architecture/scriptables/valuetostring.md) Component to the Text GameObject

![](/files/-MTr4jNa_E1uy5IFlhYj)

8 - Add to the Event Listener the Event Is Animal Dead and Select the **String** Event Type

![](/files/-MTr5FGCCkm0inoNQtw5)

9\. Connect the String Event coming from the Event to the Value to string Dynamic Method.

![](/files/-MTr5zwK04hjvZ4T3aoh)

10 - Add the Prefix "The ". Add the Suffix " is Dead"

![](/files/-MTr6R7PF7mYc4aTjwOX)

11 - Connect the ToString Event with the Text.value dynamic string parameter

![](/files/-MTr6nQwDbhjmrhlycQD)

And that's it!. If the animal dies it will set on the screen, The Wolf is **dead**.

To make the final test, drag a Death zone into the Scene and move the animal there:

![The wolf is dead!! not death.. ups](/files/-MTr7r63fackpj5eoLQH)


---

# 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://malbersanimations.gitbook.io/animal-controller/scriptable-architecture/scriptables/mevents.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.
