Yanfly selfswitches help

As a precursor let me say that I'm a self-taught and very new coder who probably knows as little as somebody with 1 month of coding 101 under their belt.

I wrote this bit of code to try and get an event to flip itself and other nearby events:

Code:
if (($gameVariables.value(7) - $gameMap.event(this.eventId()).x <= 2) && ($gameVariables.value(7) - $gameMap.event(this.eventId()).x >= -2) && ($gameVariables.value(8) - $gameMap.event(this.eventId()).y <= 2) && ($gameVariables.value(8) - $gameMap.event(this.eventId()).y >= -2) && ($gameSwitches.value(187) == true)){

this.setSelfSwitchValue(2, this.eventId(), 101, true);

this.setSelfSwitchValue(2, 3, 101, true);

this.setSelfSwitchValue(2, 4, 101, true);

this.setSelfSwitchValue(2, 5, 101, true);

this.setSelfSwitchValue(2, 7, 101, true);

this.setSelfSwitchValue(2, 8, 101, true);

}else{

this.setSelfSwitchValue(2, this.eventId(), 101, false);

}

It's a parallel event and the first bit of code works fine. It's when I get to Yanfly's "Lunatic Script Calls" in the if statement itself that seem to have an issue. There are 6 events involved starting with event 3. They each have a variation of this code and some of the events can trigger the cascade before the others since the true/false boolean for the if statement is based on proximity to the event itself (grabbing x,y coords etc.). This code is pulled from event 6, and you'll notice it missing in the set self switch calls.

Here's my issue: when I trigger say event 6, it will run the code like this pseudo-code (run code for events <=6(this event) then stop) . That is to say, events 3,4,5 will run. But 7 and 8 won't. It's the same issue for the others, if I grab event 4 I'll get event 3 and 4 triggered but not 5,6,7,8. And with 8 I get them all.

Now for some of you real coders out there this is probably not a hard fix, but I have no idea how to do it. Any and all help would be great.

Now I have to go to bed but I will check this thread and if you'd like a demo I will upload one.

Thanks in advance.
I'm not sure what you're trying to do - if the variables 7 and 8 are with a distance of 2 of the current event's X and Y position, turn on all the self switches for all the events, otherwise turn off the current event's self switch? That's what I see, looking at your code.

Let's say event 3 is close but 8 is not. Event 3 will turn on all the events' self switches, including 8, but then event 8 will turn its self switch off again because it's not in range. That's what I see happening. So I'm guessing the events are all in a line.

Maybe you should show a screenshot of your map, with the event numbers highlighted (you'll have to add those manually), and explain what it is you're trying to accomplish.
I'm not sure what you're trying to do - if the variables 7 and 8 are with a distance of 2 of the current event's X and Y position, turn on all the self switches for all the events, otherwise turn off the current event's self switch? That's what I see, looking at your code.

Let's say event 3 is close but 8 is not. Event 3 will turn on all the events' self switches, including 8, but then event 8 will turn its self switch off again because it's not in range. That's what I see happening. So I'm guessing the events are all in a line.

Maybe you should show a screenshot of your map, with the event numbers highlighted (you'll have to add those manually), and explain what it is you're trying to accomplish.

Yes you understand mostly. I have to leave for work soon and when I get back I'll upload screenshots and maybe a video/demo whatever is easier. For now let me try to paint a picture of what's going on:

Each event has 2 pages, page #1 is a parallel event with the posted script here in minor variation to account for each event number. When the script is triggered it changes each event's switch 101 to true (that is to say events 3 through 8). This is not the native self switch (A - D), but rather Yanfly's plugin that allows self switches to be assigned to a global switch. So each event's page #2 has "Switch 101" checkmarked before it will run. This page (#2) is not parallel and does nothing outside of have a different graphic.

So say I get in range of event 3, it only turns the page to #2 on event 3 despite having script that should turn the page on all other events (note all other events have a higher number assigned to their ID). If I turn on event 4 because I'm in range (and not in range of any other event) then event 3 and 4 turn to page #2 etc. etc. So following the examples, if I say turn on event 7 because I'm in range events 3,4,5,6 & 7 turn to page #2 but not 8 because whatever issue is going on checks all numbers up to the current event number ID despite having code that should turn on switches for other event IDs higher numbered than itself. I'm guessing something in the code is trying to avoid an out of bounds exception or something? I dunno.

If I get in range of event 8 it will turn the page of all events up to 8 (in this particular case the highest event ID involved). So only event ID 008 is capable of doing what I want, hence this forum post.

Thanks for your help and interest in advance.

PS just to confirm yes variables 7 and 8 are a common event that is running that keeps track of the player's X & Y, that's all it does.
As I said, if all of your events turn on ALL of the self switches when THAT event is in range, but turns its own self switch off when it's not in range, then it's working exactly as you told it to.

What's happening is that if event 3 is in range, it is turning on the self switches for all the events. But because all of the events are doing their own checks to see if they are in range, if they are not, they're turning their own self switch off.

So event 3 is turning on the self switches for events 3, 4, 5, 6, 7 and 8. Then event 4 is turning its own self switch off, and event 5 is turning its own self switch off, and events 6, 7 and 8 are also turning their own self switches off.

The only way it's going to work the way you have it set up, is if ALL events are in range at the same time.

You need to rethink your conditions. They contradict each other. For example, for event 8, you're effectively saying "if event 3 is in range, then it will turn on my self switch, but if I am not in range, then I will turn off my self switch". Should it be on because event 3 is in range, or should it be off, because event 8 isn't?
Thanks for your help Shaz. I fixed the issue I was having by removing the else part of the if statement, but I still don't really understand the error. I made a video of what I'm doing if you're at all interested. Otherwise I'll leave it alone for now.

I think you are doing it in a really, really complicated way. Unless I misunderstand completely, this could be done with a single event for the images, with 2 pages and no script calls whatsoever. Even if you want to split it up into multiple events, you could still do it with two pages and no script calls. Just a global switch.

I'm not understanding what the variables you're comparing with (7 and 8?) are all about. What's setting them, what values do they have, what's the reason for comparing them with the X & Y values?


Should they all be switched to page 2 at the same time? What are the circumstances where you'd want some to be on page 2, and some to be on page 1? 'Cause that's what was happening when you had the Else statement.

If you want to do up a demo (keep it as small as possible) and send me, I can have a play and let you know what I find/suggest.


And ... I'm a girl 😉 but that's okay.

Original thread: https://forums.rpgmakerweb.com/threads/yanfly-selfswitches-help.104194/