Want to be able to have switches on the ground detect objects on them?

  • Kazesui
  • 07/04/2011 05:44 PM
This tutorial will teach you how to have objects like boxes or boulders activate a switch on the ground when placed upon them. It's fairly simple so basic understanding of variables should be sufficient to follow it.

First we'll need a little map, and preferably some switches and boulders, like this one:

Make sure to create the Switches before you create the boulders!
After this, we can create an invisible event which will be set to run on parallel process. It's code will look something like this:

At first, we use the command "Store Event ID" and set the location to our first switch. the Store Event ID will check if there's an event on the tile it's checking and return the event ID number of that event to the variable you choose if there is. You can find the event's ID number by opening it's event editor and looking at the top left. If there are more events on the tile you're looking at, the Store Event ID will return the number of the event with the highest ID. This why it's important to create the boulders after the switches, as the switches are events themselves and will always return their own event ID if the boulders have a lower ID. It's also good practice to have the switches form Sequential Event ID's, and the same for boulders. This will make it easier to test for, and look more organized.

Since no events besides the boulders can move, you can have a branch check if there's any event with an ID higher than it's own ID to find out if there's something else standing on it (alternatively you could check against the ID against the highest switch ID and just copy paste it).

Once the game notices there being an event on the tile where you put the switch, there should be placed another branch to check if the switch has already been turned on, and if not, turn on the switch and any sound effect you might like as well as a variable counting the amount of switches you have turned on. Without doing so any sound effect would loop endlessly at high speed at all times, and variables values would also go awry.

In case there is no other event on the tile with the switch, we want to check if the switch is already on, indicating that an event has moved away from the switch. By adding an else statement to the branch and checking if the switch is on, we can turn off the switch, and remove 1 from the count of active switches.

Once this is done, all you have to do is basically copy the code for the amount of switches you have while changing the place to check with the store event ID command. At the end, you will check the value of the variable containing the value of active switches

Since we have 4 switches here, we check if the variable contains 4. If so, we'll activate whatever's supposed to happen when all switches are triggered.
And that's actually all you need to have switches check if there are events on top of them.

You might however want the hero to be able to step on the switches as well. If so, we'll have to add a little to the code.

We can have the switches activate on "touched by hero", and then turn switch on, add 1 to active switches variable and maybe a sound effect. And then have 4 events around the switch also on touched by hero which will deactivate the switches since the hero is obviously not standing on it anymore by that point. The only thing is that you need to check if there's another event on the switch first, since we don't want to deactivate the switch while there's another event on it.
This isn't very hard though

We use the same principle as that in the event running on parallel process, just that we now check if it's value is equal to that of the switch, or lower than the movable event with the lowest ID. If yes, then there's no other event on the switch, and we can assume the hero was the last on it if the switch is active, and thus we can safely deactivate it.

Also, we'll need to make sure that the parallel process event doesn't deactivate the switch while the hero is standing on it, since now it's only checking for events standing on it, and since the hero isn't an event, it will deactivate the switch if there are no events on it.

To fix this, we'll store the coordinates of the hero at the start of the parallel process event, and then when checking if it should deactivate a switch, it will first check that the hero is not standing on the same coordinates as the switch.
You can find the coordinates of an event by looking in the main editor at the bottom

The number before the comma is the X value, and the number after it is the Y value.
You will have to check the X coordinate of the hero to the X value at the point of the switch and likewise for the Y coordinate.

If the hero is not standing on the same X coordinate, and not standing on the same Y coordinate, the parallel process event can safely deactivate the switch.

And now you have switches which can check for both the hero and other events moving about the map.

Click here to download a rm2k3 project with sample code


Pages: 1
Max McGee
My name is Legion: for we are many.
I am embarrassed to say this, but I actually find this useful.
Max McGee
My name is Legion: for we are many.
I have been making RPG Maker games for fully 10 calendar years.
Dude please keep making tutorials, this one in particular is not new to me but it's good reference and all of your previous ones were awesome.
I'm curious. Although this tutorial is set to RM2k3, can this tutorial also be used in context of XP or VX?
Yeah, I still have topics I want to touch for new tutorials, so I'll probably keep making more at some point.

I'm curious. Although this tutorial is set to RM2k3, can this tutorial also be used in context of XP or VX?

If you can find a script to simulate the "get event ID" from rm2k3, that is to return the highest event ID of events given at a chosen tile, then yes. This is the case for XP at least. For all I know VX might have the command but I doubt it.

The get event ID is kind of necessary to avoid making a coordinate hell of detecting when something is on the switch, so this tutorial won't do without it, though it gives a push in the right direction.
puking up frothing vitriolic sarcastic spittle
That "switches" isn't capitalised in the name of this tutorial is really bugging me...

Anyway, yeah, good tutorial. Personally, I use a different method for doing this kind of thing, but this looks like it would work too. Good for people new to the engine(s)!
Rather than capitalizing switches, I suppose I meant to to not capitalize "on".
A tiny error on my side. Thanks for pointing it out
Again with the this being useful. Would have saved me some pain and suffering five years ago.
Guardian Angel of the Description Thread
I did it a bit differently in Matsumori Days, as I was only really dealing with one rock and one switch on the screen, but I think the general concept might be the same.

Information: The closest thing I see as far as VX's or XP's event menu is concerned is Control Variables->Character, but Event ID doesn't seem to be an option. If this is true, to replicate "Get Event ID", one would need a script, as Kazesui suggests.
puking up frothing vitriolic sarcastic spittle
>Add awesome script system.
>Remove previous, basic functionality.

Well played, Enterbrain. Well played.
Max McGee
My name is Legion: for we are many.
The balance must be maintained!
Pages: 1