# MEvents

## Overview

Events are Scriptable Objects can be used to Broadcast messages so they can be received by a [**MEventListener** ](https://malbersanimations.gitbook.io/animal-controller/scriptable-architecture/scriptables/mevent-listener)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:

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-MEU99YGq1Gr4GNTJ4wP%2F-MEU9XDPKQgbxBuAtloj%2Fimage.png?alt=media\&token=d2e95930-65a8-4cdc-9fbf-d8237f1cf8f4)

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-MRH5jXocblSbllH5G-4%2F-MRH5u0sIcHg1RmUERz2%2Fimage.png?alt=media\&token=ff0b2303-7b6e-45a5-ac92-27eb8a28ae2d)

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-MRH5jXocblSbllH5G-4%2F-MRH5x6Ox5mo3WbRZL9U%2Fimage.png?alt=media\&token=974fd32a-11da-4270-8450-8cfeebf5bd1c)

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)

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-MKc2T6pcw1RrfXPST7J%2F-MKc3rCfitVMeuIjUYWp%2Fimage.png?alt=media\&token=0b8016ce-f5af-4753-9deb-9c9942d210d8)

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:

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-MTqvW24e4MZ8RhdODlq%2F-MTqwFCO1ECHs_PN4Nap%2Fimage.png?alt=media\&token=a2a88943-e34e-4594-a99a-7cbc7cb2b235)

2 - [Create ](#how-to-create-an-event)a new MEvent and call it Animal is dead.

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-MTqvW24e4MZ8RhdODlq%2F-MTqxB54hGNyCCoU7erS%2Fimage.png?alt=media\&token=a75b9ad6-6c77-4d6e-91ea-91dc065166e6)

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

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Lzhr1XSMzMqNXjRnNlb%2Fuploads%2FWk94FAy9ujsVtKIgrc8p%2Fimage.png?alt=media\&token=3fc5d416-fe2e-423f-9d85-c686d597def6)

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

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-MTqvW24e4MZ8RhdODlq%2F-MTqzomoAqJiGlrPZQWt%2FsfEn7QqPtO.gif?alt=media\&token=398d9200-4d8a-4a10-a358-30caeae812c0)

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.

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-MTr0wD_3e1spgmycCRb%2F-MTr11EfnPjk9SkX8Y5y%2Feya9DT6Qai.gif?alt=media\&token=cb2db4cc-e684-4c62-94ae-8e89d3051538)

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

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-MTr4cntNVAAqyTZw7tD%2F-MTr4jNa_E1uy5IFlhYj%2FEcpyuqrK2d.gif?alt=media\&token=ac5641cf-9cb6-41fb-9f75-47dd26f20db7)

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

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-MTr4cntNVAAqyTZw7tD%2F-MTr5FGCCkm0inoNQtw5%2FbRobS0lKGL.gif?alt=media\&token=8c8e7a03-71e7-4dab-9bcb-d2bbd75e8cbd)

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

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-MTr4cntNVAAqyTZw7tD%2F-MTr5zwK04hjvZ4T3aoh%2F1wbsYdaNvo.gif?alt=media\&token=98f8578f-08c1-41cd-8843-ea680253553c)

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

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-MTr4cntNVAAqyTZw7tD%2F-MTr6R7PF7mYc4aTjwOX%2Fimage.png?alt=media\&token=1170e4df-1e96-472b-8c8e-552a2c0ed99c)

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

![](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-MTr4cntNVAAqyTZw7tD%2F-MTr6nQwDbhjmrhlycQD%2FVNNHcneCdK.gif?alt=media\&token=63acc1d9-3f76-4c89-b230-db926bd186c8)

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](https://963537199-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lzhr1XSMzMqNXjRnNlb%2F-MTr4cntNVAAqyTZw7tD%2F-MTr7r63fackpj5eoLQH%2F3OtZhQYES9.gif?alt=media\&token=71e71f15-44e1-4f8b-a042-d9ec6d5f5dab)
