# Trigger Logic

## Begin Game Maker 5

### Trigger Logic with Messages

#### :yellow\_circle: MEDIUM

In this activity, you will learn how to set up behaviors and components to create simple logic systems with objects as you explore the basics of communications in Game Maker using messages to create cause and effect.

{% embed url="<https://youtu.be/dinxx3yp9pc?feature=shared&t=2>" %}

{% tabs %}
{% tab title="Start" %}

### **Launch Game Maker**

* Open or create an Experience and move to an area with empty space.&#x20;
* Let's turn the grid off and focus on logic.

{% hint style="warning" %}
Some behaviors and components are only available as <mark style="color:yellow;">**\[MP]**</mark>, which means they are multiplayer synchronized. For this activity, choose <mark style="color:yellow;">**\[MP]**</mark> only when a <mark style="color:green;">**\[SP]**</mark> option isn't available.

Designing gameplay with <mark style="color:green;">**\[SP]**</mark> and <mark style="color:yellow;">**\[MP]**</mark> logic will be explored later.
{% endhint %}
{% endtab %}

{% tab title="Using Messages" %}

### Collisions & Messages

{% hint style="info" %}
**Behaviours** and **components** are needed to make objects active and/or interactive. This logic provides a framework so **messages** can be sent and received to trigger gameplay events.

**Tags** and **Broadcast Type** allow for greater control over how events are triggered.
{% endhint %}

#### Add the following objects to the viewport:

<table data-card-size="large" data-view="cards"><thead><tr><th></th></tr></thead><tbody><tr><td><strong>Asset:</strong> Door (any)<br>No behavior</td></tr><tr><td><strong>Asset:</strong> Door (any)<br><strong>Door</strong> behaviour</td></tr></tbody></table>

{% hint style="info" %}
Pressing **`E`** next to an object sends the message **`Interact`** to it.
{% endhint %}

#### Test (Press **`TAB`**)

* Press **`E`** to interact with each door. One door is only a prop. The other animates to appear open and turns off its collisions so players can pass through.

{% hint style="info" %}
Most objects must have **collisions** to prevent players from passing through them. You can control if an object has collisions, is visible, or has gravity in the Properties Panel.
{% endhint %}

***

### Toggle Behaviours On/Off

{% hint style="info" %}
A message sent to an object's **Toggle** component can turn it's behaviour on or off. Think of it as locking or unlocking a behaviour.
{% endhint %}

#### Add an object that sends a message:

<table data-card-size="large" data-view="cards"><thead><tr><th></th><th></th><th></th></tr></thead><tbody><tr><td><strong>Asset:</strong> Museum Info Panel</td><td><p><strong>Button</strong> component</p><ul><li><strong>Message to Send:</strong> <code>Door.Unlocked</code></li><li><strong>Broadcast Type:</strong> EveryoneInRange</li></ul></td><td><em>Create a custom message by typing it into the message field and click to <strong>Add</strong> it.</em></td></tr></tbody></table>

#### Add two objects <mark style="color:yellow;">**in range**</mark> (the yellow sphere) of the first object to receive the message:

<table data-card-size="large" data-view="cards"><thead><tr><th></th><th></th><th></th></tr></thead><tbody><tr><td><strong>Asset:</strong> Door (any)</td><td><p><strong>Door</strong> behavior</p><ul><li><strong>Message to Open:</strong> <code>Door.Unlocked</code></li></ul></td><td><em>Select the existing message from the message dropdown list.</em></td></tr><tr><td><strong>Asset:</strong> Door (any)</td><td><p><strong>Door</strong> behavior</p><ul><li><strong>Message to Open:</strong> <code>Interact</code></li></ul></td><td><p><strong>Toggle</strong> component</p><ul><li><strong>Turn On Message:</strong> <code>Door.Unlocked</code></li><li><strong>Initial Behaviour State:</strong> False <br>(behavior inactive at game start)</li></ul></td></tr></tbody></table>

#### Test (Press **`TAB`**)

* Neither door can be opened. Interact with the panel to send its message, and then:
  * One door opens
  * The other door's behaviour is toggled on so players can open the door with **`E`**.
    {% endtab %}

{% tab title="Trigger & Filter" %}

### Trigger & Filter Messages

#### Add the following objects to the viewport:

#### TRIGGER: AVATAR DETECTED Message sent to *everything* in the Experience (location doesn't matter)

<table data-card-size="large" data-view="cards"><thead><tr><th></th><th></th></tr></thead><tbody><tr><td><strong>Asset:</strong> Door (any)<br><strong>Name:</strong> DoorA</td><td><p><strong>Door</strong> behaviour</p><ul><li><strong>Message to Open:</strong> <code>DoorA.Open</code></li></ul></td></tr><tr><td><strong>Asset:</strong> Ancient Lever</td><td><p><strong>Message Broadcaster</strong> behaviour</p><ul><li><strong>Message to Send:</strong> <code>DoorA.Open</code></li><li><strong>Broadcast Type:</strong> All</li><li><strong>Wait Before Broadcast:</strong> True</li><li><strong>Message Required:</strong> Interact</li></ul></td></tr></tbody></table>

#### TRIGGER: OBJECT COLLECTED Message sent to specific objects

<table data-card-size="large" data-view="cards"><thead><tr><th></th><th></th><th></th></tr></thead><tbody><tr><td><strong>Asset:</strong> Door (any)<br><strong>Name:</strong> DoorB</td><td><p><strong>Door</strong> behavior</p><ul><li><strong>Message to Open:</strong> <code>DoorB.Open</code></li></ul></td><td></td></tr><tr><td><strong>Asset:</strong> Key (any)</td><td><p><strong>Collectable</strong> component</p><ul><li><strong>Message On Collect:</strong> <code>DoorB.Open</code></li><li><strong>Broadcast Type:</strong> SelectedActors</li><li><strong>Broadcast Targets:</strong> DoorB</li></ul></td><td></td></tr></tbody></table>

> Alternative Method: Add a tag to the door, set the key to broadcast the message to SpecificTagsInRange, and adjust the key's broadcast range to include the door's location.

#### TRIGGER: TAG DETECTED Message sent to self (when the object detects a specific tag in range)

<table data-card-size="large" data-view="cards"><thead><tr><th></th><th></th><th></th></tr></thead><tbody><tr><td><strong>Asset:</strong> Door (any)<br><strong>Name:</strong> DoorC</td><td><p><strong>Door</strong> behavior</p><ul><li><strong>Message to Open:</strong> <code>DoorC.Open</code></li></ul></td><td><p><strong>Speaker</strong> component</p><ul><li><strong>Detection Type</strong>: SpecificTagsinRange</li><li><strong>Include Tags:</strong> <em><code>DoorC.Key</code></em></li><li><strong>Message to Send:</strong> <code>DoorC.Open</code></li><li><strong>Broadcast Type:</strong> Self</li></ul></td></tr><tr><td><strong>Asset:</strong> Key (any)<br><strong>Tag:</strong> <em><code>DoorC.Key</code></em></td><td><strong>Pickable</strong> component</td><td></td></tr></tbody></table>

#### Test (Press **`TAB`**)

* Trigger doors A, B, and C to open based on the logic set up. Imagine ways to use these simple trigger methods for other design needs!

{% hint style="info" %}
There are [**many more ways to send messages**](https://docs.sandbox.game/en/creator/docs/build-singleplayer-and-multiplayer-logic#triggering-logic-across-systems). We'll explore more later.
{% endhint %}
{% endtab %}

{% tab title="Trigger Many Objects" %}

### One Message to Trigger Multiple Objects

{% hint style="info" %}
Build hidden logic into your Experience with invisible and collision-free Logic Assets.
{% endhint %}

#### Add the following objects to the viewport:

#### TRIGGER: AVATAR DETECTED INSIDE A VOLUME

<table data-card-size="large" data-view="cards"><thead><tr><th></th><th></th><th></th></tr></thead><tbody><tr><td><strong>Asset:</strong> Logic Asset</td><td><p><strong>Trigger Volume</strong> component</p><ul><li><strong>Detection Type</strong>: EveryAvatarInRange</li><li><strong>Detection Shape:</strong> Box</li><li><strong>Detection Area:</strong>  <mark style="background-color:red;">x</mark><code>15</code>  <mark style="background-color:green;">y</mark><code>5</code>  <mark style="background-color:blue;">z</mark><code>15</code></li><li><strong>Enter Message:</strong> <code>Night</code></li><li><strong>Exit Message:</strong> <code>Day</code></li><li><strong>Broadcast Type:</strong> All</li></ul></td><td></td></tr><tr><td><strong>Asset:</strong> Car (any)</td><td><p><strong>Basic Platform</strong> behaviour</p><ul><li><strong>Move Offset:</strong>  <mark style="background-color:red;">x</mark><code>0</code>  <mark style="background-color:green;">y</mark><code>0</code>  <mark style="background-color:blue;">z</mark><code>5</code></li><li><strong>Look at Destination:</strong> True</li></ul></td><td><p><strong>Toggle</strong> component</p><ul><li><strong>Turn On Message:</strong> <code>Night</code></li><li><strong>Initial Behaviour State:</strong> False <br>(behavior inactive at game start)</li></ul></td></tr></tbody></table>

<table data-card-size="large" data-view="cards"><thead><tr><th></th><th></th></tr></thead><tbody><tr><td><strong>Asset:</strong> Weather Asset<br><strong>Name:</strong> Night</td><td><ul><li><strong>Message Required:</strong> <code>Night</code></li><li><strong>Profile:</strong> Night</li></ul></td></tr><tr><td><strong>Asset:</strong> Luxury Street Light</td><td>(prop with no logic)</td></tr></tbody></table>

<table data-card-size="large" data-view="cards"><thead><tr><th></th><th></th></tr></thead><tbody><tr><td><strong>Asset:</strong> Logic Light Asset<br><strong>Name:</strong> Street Light<br><strong>Position:</strong> Luxury Street Light (point down)</td><td><ul><li><strong>Light Type:</strong> Spot Light</li><li><strong>Starting State:</strong> Off</li><li><strong>Spot Angle:</strong> 15</li><li><strong>Message Turn ON:</strong> <code>Night</code></li><li><strong>Message Turn OFF:</strong> <code>Day</code></li></ul></td></tr><tr><td><mark style="background-color:orange;">(2x)</mark> <strong>Asset:</strong> Logic Light Asset<br><strong>Name:</strong> Headlight<br><strong>Position:</strong> Car headlights (point forward)</td><td><ul><li><strong>Light Type:</strong> Spot Light</li><li><strong>Starting State:</strong> Off</li><li><strong>Spot Angle:</strong> 15</li><li><strong>Message Turn ON:</strong> <code>Night</code></li><li><strong>Message Turn OFF:</strong> <code>Day</code></li></ul></td></tr></tbody></table>

#### Test (Press **`TAB`**)

* The lights do not move with the car. Let's create a relationship in the hierarchy to fix that.

#### Modify

Set the "Headlight" assets to move with the car's platform motion. Open the hierarchy, select them, and drag them onto the Car. This makes them child objects and the car the parent object.

#### Test (Press **`TAB`**)

* The location of the "Headlight" assets is now controlled by their parent object's motion.
  {% endtab %}

{% tab title="Messages with Arguments" %}

### Messages with Arguments

{% hint style="info" %}
Some **special messages** appear in <mark style="color:yellow;">yellow</mark> and allow you to send information (an argument) with a message.&#x20;

You can also create custom **messages with arguments** that will appear yellow in the message dropdown list. These will be explored in a later activity.
{% endhint %}

#### Continue with the previous example so weather and the streetlight will gradually change.

#### Add objects to communicate special messages:

<table data-card-size="large" data-view="cards"><thead><tr><th></th><th></th></tr></thead><tbody><tr><td><strong>Asset:</strong> Logic Asset<br><strong>Name:</strong> Street Light Trigger</td><td><p><strong>Message Broadcaster</strong> behaviour</p><ul><li><strong>Message to Send:</strong> <mark style="color:yellow;"><code>LightColor</code></mark></li><li><strong>Time:</strong> 10</li><li><strong>Wait Before Broadcast:</strong> True</li><li><strong>Message Required:</strong> <code>Night</code></li></ul></td></tr><tr><td><strong>Asset:</strong> Logic Asset<br><strong>Name:</strong> Weather Trigger</td><td><p><strong>Message Broadcaster</strong> behaviour</p><ul><li><strong>Message to Send:</strong> <mark style="color:yellow;"><code>WeatherFade</code></mark></li><li><strong>Time:</strong> 10</li><li><strong>Wait Before Broadcast:</strong> True</li><li><strong>Message Required:</strong> <code>Night</code></li></ul></td></tr></tbody></table>

#### Modify existing objects to receive the special messages:

<table data-card-size="large" data-view="cards"><thead><tr><th></th><th></th></tr></thead><tbody><tr><td><strong>Asset:</strong> Logic Light Asset<br><strong>Name:</strong> Street Light</td><td><ul><li><del><strong>Message Turn ON:</strong></del> <del><code>Night</code></del></li><li><strong>Message Turn ON:</strong> <mark style="color:yellow;"><code>LightColor</code></mark></li></ul></td></tr><tr><td><strong>Asset:</strong> Weather Asset<br><strong>Name:</strong> Night</td><td><ul><li><del><strong>Message Required:</strong></del> <del><code>Night</code></del></li><li><strong>Message Required:</strong> <mark style="color:yellow;"><code>WeatherFade</code></mark></li></ul></td></tr></tbody></table>

#### Test (Press **`TAB`**)

* The weather and streetlight gradually change using these special messages.

{% hint style="warning" %}
Save your Experience so this logic is available for use in another activity.
{% endhint %}
{% endtab %}
{% endtabs %}

### Follow Up

This activity introduced new tools and concepts to understand objects more and how to make them communicate in custom ways.

In the next activity, you will learn to integrate quests with some of the logic from this activity so you can begin designing a flow for players with clear guidance to play through your Experience.

#### Ready for More Practice?

This activity leaves room for you to test out your own ideas when the player exits the **Trigger Volume** and the message `Day` is sent. Test out custom weather profiles and light colours, too!

> Remember that the special message <mark style="color:yellow;">`LightColor`</mark> sets transition timing *and colour*.

***

## :heavy\_plus\_sign: Relevant Resources

Triggering logic is a deep, complex topic. The resources below are beginner friendly so you won't feel overwhelmed. We'll add more to later Begin Game Maker resource sections when topics are covered.

<table data-view="cards"><thead><tr><th data-type="content-ref"></th><th></th><th data-hidden data-card-target data-type="content-ref"></th><th data-hidden data-type="files"></th><th data-hidden data-card-cover data-type="files"></th></tr></thead><tbody><tr><td><a href="../../basics/logic-and-gameplay">logic-and-gameplay</a></td><td>Learn about triggering common game events and get a clear big picture view of logic use in Game Maker</td><td><a href="../../basics/logic-and-gameplay">logic-and-gameplay</a></td><td></td><td><a href="https://331512196-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F83PS3g9zpWmq73xzlwFn%2Fuploads%2FefIFoGOza5Rk4BRTTJ78%2Fimage.png?alt=media&#x26;token=3e34691e-e13a-410d-8be9-231006a2dd31">image.png</a></td></tr><tr><td><a href="../game-production-guide/logic">logic</a></td><td>Use our general guidelines to efficiently build logic that fits your skill level as you create your Experience</td><td><a href="../game-production-guide">game-production-guide</a></td><td></td><td><a href="https://331512196-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F83PS3g9zpWmq73xzlwFn%2Fuploads%2FILL8WmeB1jnKjsXtk7ae%2Fproduction%20guide.png?alt=media&#x26;token=2a3ade5b-e76e-4c14-990a-f37c64f938de">production guide.png</a></td></tr></tbody></table>

***

## Forum: Community Ideas & Support

<table data-card-size="large" data-view="cards"><thead><tr><th></th><th data-hidden data-card-cover data-type="files"></th><th data-hidden data-card-target data-type="content-ref"></th></tr></thead><tbody><tr><td><a href="https://forum.sandbox.game/t/begin-game-maker-series-5-trigger-logic/1840"><strong>Visit the Forum</strong></a></td><td><a href="https://content.gitbook.com/content/83PS3g9zpWmq73xzlwFn/blobs/R2GdZMImITUqYrmiThBY/13%20community%20%20-%20%20blog.png">13 community  -  blog.png</a></td><td><a href="https://forum.sandbox.game/t/begin-game-maker-series-5-trigger-logic/1840">https://forum.sandbox.game/t/begin-game-maker-series-5-trigger-logic/1840</a></td></tr><tr><td><p>A forum post has been created for this learning activity! Visit to:</p><ul><li>Ask or answer questions</li><li>Post ideas about the topic</li><li><p>Share what you've learned or created</p><ul><li>from the activity</li><li>from the bonus resources</li></ul></li></ul></td><td></td><td><a href="https://forum.sandbox.game/t/begin-game-maker-series-5-trigger-logic/1840">https://forum.sandbox.game/t/begin-game-maker-series-5-trigger-logic/1840</a></td></tr></tbody></table>

***

## Other "Begin Game Maker" Videos

<table data-header-hidden><thead><tr><th data-type="content-ref"></th></tr></thead><tbody><tr><td><a href="play-test">play-test</a></td></tr><tr><td><a href="build-with-blocks">build-with-blocks</a></td></tr><tr><td><a href="place-assets">place-assets</a></td></tr><tr><td><a href="set-object-actions">set-object-actions</a></td></tr><tr><td><a href="trigger-logic">trigger-logic</a></td></tr><tr><td><a href="craft-quests">craft-quests</a></td></tr><tr><td><a href="set-up-mechanics">set-up-mechanics</a></td></tr><tr><td><a href="build-multiplayer-games">build-multiplayer-games</a></td></tr></tbody></table>
