Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

A simple pressure switch tutorial (Aka push the boulder on

Someone suggested that I post this here, so we'll see if it's any use to anyone.

Purpose

This tutorial will cover the creation of a Lufia 2-style pressure switch-- that is, a switch that opens a doorway only as long as the player, or another heavy object such as a boulder, is placed on it.  The key point here is that the doorway will close again if no heavy object is placed on the switch; however, it is simple to adjust the scripts to have the switch only need to be pressed once. 

Setup
BoulderPuzzle8.png

Here's our hero, trapped in a dark dungeon by the Evil Disco Demon, Lord Gibb.  Since Lord Gibb hates all that is natural and good, he can open the door by trampling on that one bit of grass in the dungeon, but the door will torment him by closing again the minute that he steps away from the grass.  But wait-- that boulder over there looks round enough to roll...

First, here's that boulder as an event.
BoulderPuzzle2.png

This is a fairly standard and simple boulder-- when the player walks into it, it slowly moves in the direction the player, using the nested conditional branches to determine the facing of the player.  If we change the event's trigger to "Action Button," the player will need to push the button to move the boulder, but I made it easier to discover this accidentally in this case.

The player can shove the boulder around as they like, although they'll be very sorry if they push it into a corner.

The important thing, of course, is to test if the player or the boulder winds up standing on the grass.  First, we'll track where the player is located on the screen.  On the map above, the event that does this is represented by a harpy.  The harpy will "watch" the player by continually setting a pair of variables to the player's Map X and Y.

BoulderPuzzle3.png

Note that the harpy is a Parallel Process event-- this is necessary for the event to continually update the player's location.  It is very important to note that the variables are being set to Map X and Y, not Screen X and Y-- the Map X and Y correspond to the location of events and tiles, not pixels.

Here's another Harpy, just like the first.  It's watching the boulder.

BoulderPuzzle4.png

Just the same as the other one, but tracking an event instead of a player.

As a side note, these events do not have to have graphics.  They don't have to be two different events, either-- they could easily be one single event checking four different variables.  I chose to do it this way on a whim.

Here's the door, by the way.  It's good and closed, and it tells you so.  However, note that there's another page.  We'll be looking at that second page later on.

BoulderPuzzle5.png

All right.  Now that we have all those variables being tracked, we need to do something with that information.  Thus, the Sea Serpent.  It's going to compare the X and Y of the player and the boulder to the X and Y of that patch of grass-- when they are the same, the boulder or player is on top of the grass, and it opens the door, both by changing the graphic and by turning a Door Open switch ON. 

(As a variation, if you want an event to detect when a player goes, for example, north of a given point, you can use just one X for a similar purpose.)

BoulderPuzzle6.png

Again, note that this is a parallel process event.  It's checking continually.  (Use too many of these on one map and it'll get laggy, though.)  Also note that there's a second page.

Both two-page events have the same switch-- Door Open.  Here's what the Sea Serpent event looks like when the door is open:

BoulderPuzzle7A.png


BoulderPuzzle7B.png


Two pictures, because the coding is longer.

Now, why did we do it this way?

Because a parallel process event will continue to check the same event.  If we didn't put a second page here, the door opening animation and sound would play continually.  Even if the switch is only triggered once, and doesn't reset, use the second page if you have any animations or sounds that you don't want to play continually.

This conditional branch is much more complicated, because it's checking for two different things-- it needs to see that neither the player nor the boulder leave the patch of grass, and that requires extra nesting of the conditional branches.  (This would be simpler if only the boulder could activate the switch, but I'm showing you the most complex version.) 

Note that the event, when triggered, shows the opening or closing animations, waits, and then flips the switch that will change the page of the event.  This prevents the animation from being abruptly cut off by the switch changing the page of the event, and is a technique worth remembering for other events.

Finally, here's what the door looks like when that same door switch is triggered:

BoulderPuzzle8.png

Which is fairly straightforward. 

Was this tutorial helpful?  Do I need to make any points clearer?
 
Why has nobody commented on this yet? It's a great tutorial and could help a lot of people.

You've explained things with a lot of detail, and more people should see this. I just have one question-why do we have to do that weird conditional branch nesting thing in the second last spoiler? It's a bit confusing if you ask me. Anyway, awesome tutorial, it cleared a lot of things up for me.
 
Dadevster, the second to last spoiler uses Conditional Branches to check if the player and the rock aren't on the switch, and close the gate.

There is a different method you could use, too. If you have 4 Player Touch events around the switch and have a Conditional Branch checking if the rock is still on, you can produce the same effects.

Also, I would suggest adding a "Wait: 1-5 frames" at the end of each Parallel Process event page if you have a large map or a lot of events going on, it helps reduce lag. And in the rock event, a shortcut would be "Set Move Route: Move Away From Player", so you don't need all those Conditional Branches.

Anyway, great tutorial. It should be useful to all those wondering about those complicated switch puzzles.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top