# Set Up Mechanics

## Begin Game Maker 7

### Add Game Mechanics with Rules

#### :red\_circle: COMPLEX

In this activity, you will learn the basics of the Game Rules system and how to apply it for more engaging game mechanics that can be used repeatedly. This visual scripting system is an efficient tool for designing behind the scenes logic using variables, unlocking many new gameplay varieties and a customizable HUD, or heads up display, for the player.

{% embed url="<https://youtu.be/uEjNsmH7KHY>" %}

{% tabs %}
{% tab title="About" %}
{% hint style="success" %}
**STAY ORGANISED IN GAME MAKER**

* Group rules into rulesets any way you like.
* Group objects in the hierarchy using folders.
* Find messages sent and received with the Message Inspector in the Debug menu.
* Manage messages used in your Experience in the Settings menu.
  {% endhint %}

### What are Rules?

The Game Rules system is a visual scripting tool located in the [**Gameplay**](https://docs.sandbox.game/en/creator/game-maker/docs/build-menus/top-bar/gameplay) menu.

It allows you to set, use, and change values such as numbers, text, time, and true/false values for your gameplay needs and control what appears on the player HUD, or heads up display.

### **Launch Game Maker**

* <mark style="background-color:yellow;">Open the Experience from the previous activity</mark> to reuse some of its logic.
  {% endtab %}

{% tab title="Trigger Rules" %}
{% hint style="info" %}
A rule needs to be triggered by a **Message In** to carry out its actions. It can then trigger objects, other rules, etc. Some rules can also be interrupted with a message.
{% endhint %}

### Trigger: Joining the Game

&#x20;Let's begin with a simple popup window that appears when a player enters the game:

<table data-card-size="large" data-view="cards"><thead><tr><th></th><th></th></tr></thead><tbody><tr><td><strong>Rule:</strong> Popup Window<br><strong>Type:</strong> Game Screen</td><td><strong>Required Message:</strong> <code>Playerjoined</code><br><strong>Title:</strong> Welcome!<br><strong>Description:</strong> Explore as a player to test the logic you create.</td></tr><tr><td><strong>Rule:</strong> OnPlayerJoin<br><strong>Type:</strong> Events</td><td><strong>Message to Send:</strong> <code>Playerjoined</code></td></tr></tbody></table>

#### Test

Press **`TAB`** to test. The popup window appears as soon as the Experience starts for you.

For now, let's set the Popup Window rule's **Required Message** to *`none`* so the window won't pop up every time we press **`TAB`** to enter the game and test logic.
{% endtab %}

{% tab title="Use Variables" %}

### Use a Text Variable in a Rule

{% hint style="info" %}
Use text variables to show status, player/location names, level up notices, and more!
{% endhint %}

Create a variable and a rule to use it:

<table data-card-size="large" data-view="cards"><thead><tr><th></th><th></th></tr></thead><tbody><tr><td><strong>Variable</strong><br><strong>Type:</strong> Text</td><td><strong>Apply variable:</strong> Global<br><strong>Name:</strong> Area<br><strong>Value:</strong> Banner Text</td></tr><tr><td><strong>Rule:</strong> Banner<br><strong>Type:</strong> Game screen</td><td><ul><li><strong>Required Message:</strong> <code>Night</code></li><li><strong>Duration:</strong> Fixed value of 5 (seconds)</li><li><strong>Variable:</strong> Area</li><li><strong>Name:</strong> Area</li><li><strong>Icon:</strong> Map</li></ul></td></tr></tbody></table>

#### Test

In a previous activity, the message `Night` is sent anytime the player enters an object's trigger volume. The banner will display the words "Banner Text," which is the value saved in the variable.

### Overwrite a Text Variable and Reuse It

Let's create a message with arguments to send different text to the Rules System depending on where the player is located in the game world.

<table data-card-size="large" data-view="cards"><thead><tr><th></th><th></th></tr></thead><tbody><tr><td><strong>Logic Asset</strong></td><td><p><strong>Trigger Volume</strong> component</p><ul><li><p>Enter Message:</p><ul><li>Message (with argument): <code>Area</code></li><li>Argument 1: Text</li><li>Argument name: Area Name</li><li>Argument value: Area 1</li></ul></li><li>Broadcast type: Rules</li></ul></td></tr><tr><td><strong>Logic Asset</strong></td><td><p><strong>Trigger Volume</strong> component</p><ul><li><p>Enter Message:</p><ul><li>Message (with argument): <code>Area</code></li><li>Argument value: Area 2</li></ul></li><li>Broadcast type: Rules</li></ul></td></tr></tbody></table>

Now create a rule to overwrite the variable's text with the message argument:

<table data-card-size="large" data-view="cards"><thead><tr><th></th><th></th><th></th></tr></thead><tbody><tr><td><strong>Rule:</strong> Set Text Value<br><strong>Type:</strong> Replace Variable Values</td><td>Required Message: <code>Area</code><br>Text variable: Area<br>New text: Message Argument, select argument name you created</td><td></td></tr></tbody></table>

#### Test

The banner rule doesn't use the text argument sent with the message, but still works. The rule to sets banner text uses the argument text included with the message.
{% endtab %}

{% tab title="Trigger Objects" %}

### Trigger: Keypress (to shoot projectiles)

Create a preset of the following asset. Delete the object after saving it as a preset.

<table data-card-size="large" data-view="cards"><thead><tr><th></th><th></th></tr></thead><tbody><tr><td><mark style="color:yellow;"><strong>[MP]</strong></mark><strong> Asset:</strong> Numpad Switch<br><strong>Invisible</strong></td><td><p><strong>Projectile</strong> behaviour</p><ul><li><strong>Start with Experience:</strong> True</li><li><strong>Message Required:</strong> none</li><li><strong>Projectile Force:</strong> 40</li><li><strong>Projectile Trajectory:</strong> 0</li><li><strong>Look at Direction:</strong> True</li><li><p><strong>Collision Target:</strong> SpecificTags</p><ul><li><mark style="color:green;"><strong>Tags to Collide With:</strong></mark> <em>artifact</em></li></ul></li><li><p><strong>Message on Collision:</strong> <mark style="color:blue;"><code>Health</code></mark></p><ul><li><strong>Health:</strong> - 100</li></ul></li><li><strong>Broadcast Type:</strong> CollidedActors</li></ul></td></tr></tbody></table>

**Note:** With these settings, the projectile will cause 100 damage if it hits an object that has the tag *artifact*, which was applied to objects in the previous activity.

Now create a pickable object with a child object that spawns the projectile preset:

<table data-card-size="large" data-view="cards"><thead><tr><th></th><th></th><th></th></tr></thead><tbody><tr><td><mark style="color:yellow;"><strong>[MP]</strong></mark><strong> Asset:</strong> Dragon Egg</td><td><p><strong>Visual FX</strong> Component</p><ul><li>Beverage wisp</li></ul></td><td><p><strong>Pickable</strong> component</p><ul><li><p>Offset:   <mark style="background-color:red;">x</mark><code>0</code>  <mark style="background-color:green;">y</mark><code>-0.5</code>  <mark style="background-color:blue;">z</mark><code>0</code></p><p>Note: This offset adjusts the object location when it is picked up.</p></li></ul></td></tr><tr><td><mark style="color:yellow;"><strong>[MP]</strong></mark><strong> Asset:</strong> Numpad Switch</td><td><strong>Location:</strong> Inside the dragon egg asset<br><strong>Child object</strong> of the dragon egg<br><strong>Invisible</strong></td><td><p><strong>Asset Spawner</strong> behaviour</p><ul><li><strong>Message Required:</strong> <code>1</code></li><li><strong>Use Preset:</strong> True</li><li><strong>Preset:</strong> Select your projectile preset</li><li><strong>Only once:</strong> False</li></ul></td></tr></tbody></table>

Add a Key Input rule to trigger the projectile to spawn whenever a key is pressed:

<table data-card-size="large" data-view="cards"><thead><tr><th></th><th></th></tr></thead><tbody><tr><td><strong>Rule:</strong> Key Input<br><strong>Type:</strong> Triggers</td><td><strong>Select Key:</strong> 1<br><strong>Message to send onKeyDown:</strong> <code>1</code></td></tr></tbody></table>

#### Test

Press **`TAB`** to test. Pressing the **`1`** key shoots the projectile from the dragon egg anytime. Pick up your magical dragon egg with **`E`** and shoot all ancient artifact assets to collect the treasure!

Since an object that is picked up can’t send or receive messages in the current version of Game Maker, we created a child object to be the asset spawner. The projectile behavior triggers as soon as the object it’s applied to is spawned.

{% hint style="info" %}
The Raycast component can detect objects from a distance and send messages, too!
{% endhint %}
{% endtab %}

{% tab title="Count Resources" %}

### Count Resources with a Number Variable

Place the treasure preset from the last activity. Modify its collectable component so it will send a message to the Game Rules system whenever it is collected. Save this preset over the old version. Delete the object after saving it as a preset.

<table data-card-size="large" data-view="cards"><thead><tr><th></th><th></th><th></th></tr></thead><tbody><tr><td><strong>Asset:</strong> Ancient Treasure A<br><strong>Tag:</strong> treasure</td><td><p><strong>Indicator</strong> component</p><ul><li><strong>Linked Quest ID:</strong> Treasure Hunt</li><li><strong>Quest cursors:</strong> True</li><li><strong>Objective:</strong> Treasure Hunter</li></ul></td><td><p><strong>Collectable</strong> component</p><ul><li><strong>Collect Sound:</strong> Generic Coin</li><li><strong>Message on Collect:</strong> <code>Treasure.Collect</code></li><li><strong>Broadcast Type:</strong> All</li></ul><p>Note: Each time this object is collected, the message is sent to all systems.</p></td></tr></tbody></table>

Modify all of the Ancient Artifact assets to drop the preset when destroyed:

<table data-card-size="large" data-view="cards"><thead><tr><th></th><th></th><th></th></tr></thead><tbody><tr><td>Modify <strong>Assets:</strong> Ancient Artifact<br><strong>Tag:</strong> artifact</td><td><p><strong>Health</strong> component</p><ul><li>Health: 10</li></ul></td><td><p><strong>Drop</strong> component</p><ul><li><strong>Use Preset:</strong> True</li><li><strong>Preset:</strong> Select your treasure preset</li></ul></td></tr></tbody></table>

Duplicate an Ancient Artifact asset until there are at least 20 in the game. Spread them out.

Create the following variable and rules to track and display money:

<table data-card-size="large" data-view="cards"><thead><tr><th></th><th></th></tr></thead><tbody><tr><td><strong>Variable</strong><br><strong>Type:</strong> Number</td><td><strong>Apply variable:</strong> Local<br><strong>Name:</strong> Money<br><strong>Value:</strong> 0</td></tr></tbody></table>

<table data-card-size="large" data-view="cards"><thead><tr><th></th><th></th><th></th></tr></thead><tbody><tr><td><strong>Rule:</strong> Math<br><strong>Type:</strong> Math</td><td><ul><li>Required Message: <code>Treasure.Collect</code></li><li>Operand 1: variable, Money</li><li>Operator: Add</li><li>Operand 2: Fixed value, 100</li><li>Result: Money</li></ul></td><td>Note: This amount replaces the variable.</td></tr><tr><td><strong>Rule:</strong> HUD<br><strong>Type:</strong> Game Screen</td><td><ul><li><strong>Required Message:</strong> <code>Playerjoined</code></li><li><strong>Select slot:</strong> Top center slot</li><li><strong>Variable:</strong> Money</li><li><strong>Name:</strong> Treasure</li><li>Choose icon and size</li></ul></td><td>Note: This displays the variable on the HUD from when a player enters the game.</td></tr></tbody></table>

#### Test

Press **`TAB`** to test. Now as you destroy the ancient artifacts and collect the treasure that drops, you'll see the amount of treasure collected (100 per treasure). Each time you collect the treasure, which is a preset, you’re sending a message to a custom location.&#x20;

In this case, it goes to All locations so it will reach the Rules system and increase your money when collected, which appears in the HUD slot you selected.
{% endtab %}

{% tab title="Use Resources" %}

### Make Variables Useful

Create an NPC to sell different types of items to players (equipment to wear, a consumable item, and a resource to be shown in the player HUD):

<table data-card-size="large" data-view="cards"><thead><tr><th></th><th></th></tr></thead><tbody><tr><td>Asset: Bazaar Merchant</td><td><p>Asker behaviour</p><ul><li>Question: "What would you like to buy today?"</li><li>A1: "Food"<br>A1 message: <code>ShopItem1</code></li><li>A2: "Equipment"<br>A2 message: <code>ShopItem2</code></li><li>A3: "Wood"<br>A3 message: <code>ShopItem3</code></li></ul></td></tr><tr><td></td><td></td></tr></tbody></table>

Create a preset object to spawn when purchased (delete after saving as a preset):

<table data-card-size="large" data-view="cards"><thead><tr><th></th><th></th></tr></thead><tbody><tr><td>Asset: Any food item</td><td><p>Collectable component</p><ul><li>Consumable: true<br>Note: Press <strong><code>C</code></strong> in Inventory to use</li><li>Message on consume: <mark style="color:blue;"><strong><code>Speed</code></strong></mark>, Percentage 20, Duration 10<br>Note: This gives a 10 second boost</li></ul></td></tr></tbody></table>

Create asset spawners for items to purchase and collect:

<table data-card-size="large" data-view="cards"><thead><tr><th></th><th></th></tr></thead><tbody><tr><td>Logic Asset</td><td><p>Asset Spawner behaviour</p><ul><li>Message required: <code>ShopItem1Purchased</code></li><li>Asset to spawn: any equipment asset</li><li>Component: Collectable</li></ul></td></tr><tr><td>Logic Asset</td><td><p>Asset Spawner behaviour</p><ul><li>Message required: <code>ShopItem2Purchased</code></li><li>Spawn preset (toggle): true</li><li>Preset: the food preset you created</li></ul></td></tr></tbody></table>

Add variables and rules for resources that will be tracked by number and displayed in the HUD:

<table data-card-size="large" data-view="cards"><thead><tr><th></th><th></th></tr></thead><tbody><tr><td><strong>Variable</strong><br><strong>Type:</strong> Number</td><td><strong>Apply variable:</strong> Local<br><strong>Name:</strong> Wood<br><strong>Value:</strong> 0</td></tr><tr><td><strong>Rule:</strong> Math<br><strong>Type:</strong> Math<br><strong>Name:</strong> +Wood</td><td>Required Message: <code>ShopItem3Purchased</code><br>Operand 1: Wood variable<br>Operator: Add<br>Operand 2: Fixed value, 5<br>Result: Wood variable<br>Note: This adds 5 to the wood variable</td></tr><tr><td>Rule: HUD<br>Type: Game Screen</td><td>Required Message: <code>Playerjoined</code><br>HUD slot: any bottom slot</td></tr></tbody></table>

Add a **Shop** rule preset to save time on the rest of the setup:

<figure><img src="https://331512196-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F83PS3g9zpWmq73xzlwFn%2Fuploads%2FeiWb6WwHIb4IEHH8PWEN%2Fimage.png?alt=media&#x26;token=7f3387c8-1f17-4e67-9940-0404d250cf47" alt=""><figcaption></figcaption></figure>

You already set up the Add Money, HUD, and Player Join rules earlier in this activity. You can remove the ones in this premade ruleset.

The remaining rules allow you to test if you have enough money to buy two types of items and pay from your money variable if you do have enough.

You have three items in your shop, so **select and duplicate** a Success, Fail, and Pay rule in this Ruleset to add what's needed for your third item.

Rename Item 1 rules "Purchase Food Success," "Purchase Food Fail," and "Pay Food." Rename Item 2 rules for equipment and item 3 rules for Wood to make it easier to track what each rule does in your custom setup.

Set the price of each item in the Success and Fail rules so it will check if you have enough and send a message if you don't, and use the same value in the "operand 2" value of the Pay rules to match and subtract an item's cost from your Money. Based on the number of treasures your game has, set food to cost 1, equipment to cost 4, and wood to cost 2, for example.

The messages sent by each rule matches the rest of the logic in the activity, but you can use custom messages to keep track of things more easily.

Finally, add a text variable and rule to inform players when they don't have enough money:

<table data-card-size="large" data-view="cards"><thead><tr><th></th><th></th><th></th></tr></thead><tbody><tr><td>Variable<br>Type: Text</td><td>Apply variable: Local<br>Name: Purchase Status<br>Value: You don't have enough money</td><td></td></tr><tr><td>Rule: Banner<br>Type: Game Screen</td><td><ul><li><strong>Required Message:</strong> <code>ShopPurchaseFailed</code></li><li><strong>Duration:</strong> Fixed value of 5 (seconds)</li><li><strong>Variable:</strong> Purchase Status</li><li><strong>Name:</strong> Purchase Status</li></ul></td><td></td></tr></tbody></table>

#### Test

If you destroyed some artifacts and collected treasure, you have money to spend with the merchant. You can set custom costs for each item in the Rules System and spawn any type of item you like or add resources for gameplay using variables.

Try opening your inventory to wear the equipment you purchased or consuming the food item that triggers a speed Power Up. The purpose of the items you buy are completely up to you.
{% endtab %}
{% endtabs %}

### Follow Up

The Game Rules System is a powerful visual scripting tool running behind the scenes to manage information and gameplay.

Other rule presets make it easy to track points, add time trials, set poison or hunger for players, and more. It's also possible to build a complex [**resource management system**](https://docs.sandbox.game/en/creator/game-maker/creative-guides/experience-guide/resource-management) where messages with arguments trigger amounts of resources to be traded or purchased.

With rules, you can even define what variables are for all players and which ones will be tracked differently for each player. We'll explore that in the next activity about multiplayer games.

***

### :heavy\_plus\_sign: Relevant Resources

<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-card-cover data-type="files"></th></tr></thead><tbody><tr><td><a href="../docs/game-rules">game-rules</a></td><td>Unpack all of the details of the Game Rules system to deepen understanding</td><td><a href="../docs/game-rules">game-rules</a></td><td><a href="https://331512196-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F83PS3g9zpWmq73xzlwFn%2Fuploads%2FmdgHTpRTB7fkZhDqoXhq%2Fhude%20examples2.gif?alt=media&#x26;token=149189dc-253b-41df-b36e-31d202407595">hud slot icons.gif</a></td></tr><tr><td><a href="broken-reference">Broken link</a></td><td>Find details on each rule (or right click a rule in Game Maker for this info)</td><td><a href="broken-reference">Broken link</a></td><td><a href="https://331512196-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F83PS3g9zpWmq73xzlwFn%2Fuploads%2FjuGAcOFEBgK0eWZ90ArZ%2Fimage.png?alt=media&#x26;token=05d9f6f1-3d79-4e05-8b1d-6ede17cfb01b">gm rule types menu.png</a></td></tr><tr><td><a href="../creative-guides/game-rules-guide">game-rules-guide</a></td><td>See logic templates to learn Game Rules and presets documentation</td><td><a href="../creative-guides/game-rules-guide">game-rules-guide</a></td><td><a href="https://331512196-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F83PS3g9zpWmq73xzlwFn%2Fuploads%2FVG1zhVpv5QpfS1pHjdMK%2Fimage.png?alt=media&#x26;token=c6c6feaf-f4c3-4b24-81f6-ca10d682be77">GM Template Tutorial Rules 101.png</a></td></tr><tr><td><a href="../creative-guides/experience-guide/resource-management">resource-management</a></td><td>Explore a whole new world of game variations using the Rules system to track player resources</td><td><a href="../creative-guides/experience-guide/resource-management">resource-management</a></td><td><a href="https://331512196-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F83PS3g9zpWmq73xzlwFn%2Fuploads%2FXBNCjGCjI8AvgpMzQiNo%2FGM%20Resource%20Managment%20template%20thumb.png?alt=media&#x26;token=d70b48bf-3507-4029-8e22-92203bab29af">GM Resource Managment template thumb.png</a></td></tr><tr><td><a href="../../creative-guides/logic-guide/equipment-based-logic-on-equip-nft-sensor#event-count-and-check">#event-count-and-check</a></td><td>Copy and paste this ruleset and integrate with objects to count anything</td><td><a href="../../creative-guides/logic-guide/equipment-based-logic-on-equip-nft-sensor#event-count-and-check">#event-count-and-check</a></td><td><a href="https://331512196-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F83PS3g9zpWmq73xzlwFn%2Fuploads%2F94prWv9PZxjqfKKvtY4d%2FGM%20Rules%20Export%20Import%20sm%20opt.gif?alt=media&#x26;token=45766112-2e70-4ab2-b29f-ada38701de60">GM Rules Export Import sm opt.gif</a></td></tr><tr><td><a href="../creative-guides/logic-guide/equipment-based-logic-on-equip-nft-sensor">equipment-based-logic-on-equip-nft-sensor</a></td><td>Use our counting ruleset for various events when the player equips an item</td><td><a href="../creative-guides/logic-guide/equipment-based-logic-on-equip-nft-sensor">equipment-based-logic-on-equip-nft-sensor</a></td><td><a href="https://331512196-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F83PS3g9zpWmq73xzlwFn%2Fuploads%2FTwQ3YEke6goVVUV4iYaU%2FClass%20System%20Horde%20Mode%20trim.gif?alt=media&#x26;token=efca89e3-2ec5-4f07-b879-712b685fd75d">GM Collectable On Equip Class System Horde.gif</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-7-set-up-mechanics/1842"><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-7-set-up-mechanics/1842">https://forum.sandbox.game/t/begin-game-maker-series-7-set-up-mechanics/1842</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-7-set-up-mechanics/1842">https://forum.sandbox.game/t/begin-game-maker-series-7-set-up-mechanics/1842</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>
