How to trigger switch on/off by player stepping on/off an event?

Hi!

I've been thinking about how to do this cleanly and can't come up with any solution that isn't rather clunky. The idea is to have an event act as a giant switch, where if the player is on top of it, the switch is on, and when they walk off of it, to turn the switch off automatically.

I know I can do it by surrounding the event with a bunch of events that turn the switch off but that becomes incredibly cluttered, and in some cases, I can't physically do it due to a map/event layout.

If anyone has any thoughts please do let me know -- thanks!!
an alternative would be to have a parallel process using control variable and conditional branch to check the player position. if it is identical to the switch coordinates then turn it on, otherwise off.

just remember to include a wait(5) or so to reduce lag.
Here is my solution :
Spoiler: Image
1728122995299.png


1728123356280.png

Create the event set the priority to below player and the trigger to player touch
Enable the switch if the player touch the event
Create a page 2 that checks if the switch is set to ON (condition), which should run in parallel.
Then simply look for player input to turn it off, meaning on player input he will be moving away.

If you would like to check for mouse input aswell, then you would have to use @Andar's approach and set player position X and Y, then check if the position X and Y have changed, if so it means the player moved and it must be turned off.

Would look like this :
Spoiler: With mouse
1728124235410.png


1728124250671.png
If you have Yanfly plugins, you can use YEP_AdvancedSwVar for this. This allows you to put javascript formula's in a variable title. This variable will always contain the outcome of that variable. For switches it allows you to put a javascript IF statement in the title instead, and the switch always turns on when the IF statement is true.

So if the IF statement would check if the player is standing on a certain tile, the switch would be on if the player stands there and off otherwise.

You don't need to run that condition all the time with a parallel or something, it will always be updated.
(Not at a computer so can't fully test)

I like using distance from event when creating switches that the player can stand on (assuming you used player touch + below character for page 1 to self-switch to A for page 2 - parallel):
JavaScript:
const dist = Math.hypot($gameMap.deltaX(this.character(-1).x, this.character(0).x), $gameMap.deltaY(this.character(-1).y, this.character(0).y));
if (dist >= 1) $gameSelfSwitches.setValue([$gameMap.mapId(), this.eventId(), 'A'], false);
this.wait(6);

You should be able to do this as well since any movement from the player will probably move them away from the event (not foolproof though):
JavaScript:
if ($gamePlayer.isMoving()) {
  this.wait(2 ** (8 - $gamePlayer.moveSpeed()));
  $gameSelfSwitches.setValue([$gameMap.mapId(), this.eventId(), 'A'], false);
}
this.wait(6);

Edit to use conditional branch so you can nest with event commands easier and organize the wait command to execute at the appropriate time:

JavaScript:
◇If : Script : $gamePlayer.isMoving()
  ◇Script : this.wait(2 ** (8 - $gamePlayer.moveSpeed()))
  ◇Control Self Switch : A = OFF
  ◇
:End
◇Wait : 6 frames

(Edited above to compensate for move speed of player)
These are all perfect, so happy to see so many different ways of doing it.

Thanks all -- solved!
This thread is closed due to being reported as solved. If you need it reopened, report the thread to let us know. Thank you!

Original thread: https://forums.rpgmakerweb.com/threads/how-to-trigger-switch-on-off-by-player-stepping-on-off-an-event.172381/