Checking event self-switch

Hello, I'm wonder if there is a way to check event self-switch? I only found this script call:
Code:
$gameSelfSwitches.value([mapId, eventId, switchId]);
Ok it's work but isn't possible to do it without eventId? In my case it must to be done with an event location (x,y).
In addition how we check many self-switch A,B,C,D in one time, or how we check if all the self-switch event are clean?
1) It would be easier to get the eventID from the coordinates by using get location info, then place the id in the script

2) you can't.
a comparison always has to be unique.
theoretically you could loop through a number of switches, but with only four possibilites any loop would need the about same number of commands as four single condition checks.
$gameMap.eventsXy(x, y) gives you an array of all events at position x and y. $gameMap.eventIdXy(x, y) gives you the id of the first event from that list.

So if you don't think you'll have multiple events at that location, or if you're fine getting the event with the lowest id in that location, you can use the following in a script call.

JavaScript:
var eventId = $gameMap.eventIdXy(x, y);  // Event must currently be on the map
var mapId = $gameMap.mapId();  // Map Id of the current map
var switchIds = ['A', 'B', 'C', 'D'];
var countTrue = 0;  // To see how many self-switches are true:
for (var i = 0, n = switchIds.length; i < n; i++) {
     var key = [mapId, eventId, switchIds[i]];
     var value = $gameSelfSwitches.value(key);
     countTrue += value ? 1 : 0;  // Add 1 to count if self switch is true
}

You don't say how you're going to use this information, so you have several options:

1) If you use the above in a script call you're going to have to put your other event commands in that script call as well (e.g., if (countTrue === 0) { do this }), but MV has a very limited space for script calls.

2) Save the information as $gameVariable.setValue(varId, countTrue) at the end of the script call and use that variable later.

3) If you'll be doing this multiple times throughout your game, you can modify the above to create a function in a plugin, then you can check it in a conditional branch using the "script" portion.
1) It would be easier to get the eventID from the coordinates by using get location info, then place the id in the script

2) you can't.
a comparison always has to be unique.
theoretically you could loop through a number of switches, but with only four possibilites any loop would need the about same number of commands as four single condition checks.
In my case, I do a board game where each map tiles have a self-switch event corresponding to the player who possess it.
I already use this to control self-switch thanks to their coordinates (x,y).
Code:
var key = [1, $gameMap.eventIdXy(x,y), 'A'];
$gameSelfSwitches.setValue(key, true);


1)So how you could get the eventId from their coordinates?
2)How you do it with a reasonably method when you will to apply it for all tiles?

$gameMap.eventsXy(x, y) gives you an array of all events at position x and y. $gameMap.eventIdXy(x, y) gives you the id of the first event from that list.

So if you don't think you'll have multiple events at that location, or if you're fine getting the event with the lowest id in that location, you can use the following in a script call.

JavaScript:
var eventId = $gameMap.eventIdXy(x, y);  // Event must currently be on the map
var mapId = $gameMap.mapId();  // Map Id of the current map
var switchIds = ['A', 'B', 'C', 'D'];
var countTrue = 0;  // To see how many self-switches are true:
for (var i = 0, n = switchIds.length; i < n; i++) {
     var key = [mapId, eventId, switchIds[i]];
     var value = $gameSelfSwitches.value(key);
     countTrue += value ? 1 : 0;  // Add 1 to count if self switch is true
}

You don't say how you're going to use this information, so you have several options:

1) If you use the above in a script call you're going to have to put your other event commands in that script call as well (e.g., if (countTrue === 0) { do this }), but MV has a very limited space for script calls.

2) Save the information as $gameVariable.setValue(varId, countTrue) at the end of the script call and use that variable later.

3) If you'll be doing this multiple times throughout your game, you can modify the above to create a function in a plugin, then you can check it in a conditional branch using the "script" portion.
It's work if I want to check specifically one kind self-switch, but how I do to check if all self-switch are off?
By "all self switch" do you mean all the self-switches for one event, or all the self-switches for ALL events?

The code I gave you above counts how many self-switches (A, B, C, D) for a particular event at location x, y are turned on. As seen in my point 1, check if countTrue === 0 to see if all of them are turned off.
By "all self switch" do you mean all the self-switches for one event, or all the self-switches for ALL events?

The code I gave you above counts how many self-switches (A, B, C, D) for a particular event at location x, y are turned on. As seen in my point 1, check if countTrue === 0 to see if all of them are turned off.
I mean all self-switch for 1 event.

Nope it doesn't work, if I put countTrue === 0 it will still check if one of the self-switch is on.
If I check on 1 event where I know self-swithes are off it's work. But if I want to check an event where a self-switch is on: it unable to check if self-switches are on/off
You should post your event page/script call so we can see what you're doing.

I checked my code in MV, and when no self switches for an event are turned on, countTrue is 0. If 2 self-switches are turned on, countTrue is 2, etc.

So if you want to see if at least one self-switch is on, use if (countTrue > 0), or you can use if (countTrue !== 0) to see if countTrue is NOT zero.

If this is not what you want, you may have to clarify further.
You should post your event page/script call so we can see what you're doing.

I checked my code in MV, and when no self switches for an event are turned on, countTrue is 0. If 2 self-switches are turned on, countTrue is 2, etc.

So if you want to see if at least one self-switch is on, use if (countTrue > 0), or you can use if (countTrue !== 0) to see if countTrue is NOT zero.

If this is not what you want, you may have to clarify further.
◆If:Script:var eventId = $gameMap.eventIdXy($gameVariables.value(X),$gameVariables.value(Y)); var mapId = $gameMap.mapId(); var switchIds = ['A']; var countTrue = 0; for (var i = 0, n = switchIds.length; i < n; i++) { var key = [mapId, eventId, switchIds]; var value = $gameSelfSwitches.value(key); countTrue += value ? 1 : 0;}
◆Text:None, Window, Bottom
: :switch A
◆
:End
◆If:Script:var eventId = $gameMap.eventIdXy($gameVariables.value(X),$gameVariables.value(Y)); var mapId = $gameMap.mapId(); var switchIds = ['B']; var countTrue = 0; for (var i = 0, n = switchIds.length; i < n; i++) { var key = [mapId, eventId, switchIds]; var value = $gameSelfSwitches.value(key); countTrue += value ? 1 : 0;}
◆Text:None, Window, Bottom
: :switch B
◆
:End
◆If:Script:var eventId = $gameMap.eventIdXy($gameVariables.value(X),$gameVariables.value(Y)); var mapId = $gameMap.mapId(); var switchIds = ['C']; var countTrue = 0; for (var i = 0, n = switchIds.length; i < n; i++) { var key = [mapId, eventId, switchIds]; var value = $gameSelfSwitches.value(key); countTrue += value ? 1 : 0;}
◆Text:None, Window, Bottom
: :switch C
◆
:End
◆If:Script:var eventId = $gameMap.eventIdXy($gameVariables.value(X),$gameVariables.value(Y)); var mapId = $gameMap.mapId(); var switchIds = ['D']; var countTrue = 0; for (var i = 0, n = switchIds.length; i < n; i++) { var key = [mapId, eventId, switchIds]; var value = $gameSelfSwitches.value(key); countTrue += value ? 1 : 0;}
◆Text:None, Window, Bottom
: :switch D
◆
:End
◆If:Script:var eventId = $gameMap.eventIdXy($gameVariables.value(X),$gameVariables.value(Y)); var mapId = $gameMap.mapId(); var switchIds = ['A', 'B', 'C', 'D']; var countTrue = 0; for (var i = 0, n = switchIds.length; i < n; i++) { var key = [mapId, eventId, switchIds]; var value = $gameSelfSwitches.value(key); countTrue === 0;}
◆Text:None, Window, Bottom
: :switch clear
◆
:End

I precise but all my events can only be have 1 self-switch (A,B,C or D) activate at once
You probably need this.
I think you're misunderstanding how to use a script call versus a script in the conditional branch. The code I gave you is a script call, something you write in the Script command (page 3 of the event commands).

To use the above code in a conditional branch, you need to write a function in a plugin and get it to return the value of countTrue. So for example, you can copy the below into a new plugin:

JavaScript:
function countSelfSwitches(x, y) {
     var eventId = $gameMap.eventIdXy(x, y);  // Event must currently be on the map
     var mapId = $gameMap.mapId();  // Map Id of the current map
     var switchIds = ['A', 'B', 'C', 'D'];
     var countTrue = 0;  // To see how many self-switches are true:
     for (var i = 0, n = switchIds.length; i < n; i++) {
          var key = [mapId, eventId, switchIds[i]];
          var value = $gameSelfSwitches.value(key);
          countTrue += value ? 1 : 0;  // Add 1 to count if self switch is true
     }
     return countTrue;
};

And use it in an event command like this (note that I used (7, 6) in the example below because those of the x,y coordinates of my event, but you should replace it with the x and y that you actually want to check).

1740625834099.png
I think you're misunderstanding how to use a script call versus a script in the conditional branch. The code I gave you is a script call, something you write in the Script command (page 3 of the event commands).

To use the above code in a conditional branch, you need to write a function in a plugin and get it to return the value of countTrue. So for example, you can copy the below into a new plugin:
First using a script call or using it in a conditional branch is the same thing, my evidence both work, the only difference is the readability.
And use it in an event command like this (note that I used (7, 6) in the example below because those of the x,y coordinates of my event, but you should replace it with the x and y that you actually want to check).
Secondly, yes it's possible to use variable to define a coordinate x or y, because the coordinate I want to check can change.

And finally, instead of using your code I used this as a reference
Code:
$gameSelfSwitches.value([mapId, eventId, switchId]);

And I find a way to check in the same way that in using the eventId and it's a lot more shorter that your code

Code:
var eventId = $gameMap.eventIdXy($gameVariables.value(x),$gameVariables.value(y));
$gameSelfSwitches.value([mapId, eventId, 'switchId']);

But I have always the same problem, I'm unable to check if a self-switch is off. It's possible to cheat it with conditional branch but it's not what I need.
in scripts, switches use the values true and false as opposed to ON/OFF.

to check if a switch is off in script is to check == false
I'm unable to check if a self-switch is off.
To check for only one value, use the ! symbol for your format (replace mapId, eventId, and switchId with the values you need):
JavaScript:
!$gameSelfSwitches.value([this._mapId, $gameMap.eventIdXy($gameVariables.value(6), $gameVariables.value(7)), 'A'])
In addition how we check many self-switch A,B,C,D in one time, or how we check if all the self-switch event are clean?
If your goal is to use the conditional branch (if-script) to check if the event has ALL of their selfswitches turned OFF, try this (I will use game variables 6 and 7 for the (x, y) values):
JavaScript:
['A', 'B', 'C', 'D'].every(letter => !$gameSelfSwitches.value([this._mapId, $gameMap.eventIdXy($gameVariables.value(6), $gameVariables.value(7)), letter]))

Be sure to change the variable IDs to the ones you are using for your game.
JavaScript:
!$gameSelfSwitches.value([this._mapId, $gameMap.eventIdXy($gameVariables.value(6), $gameVariables.value(7)), 'A'])
What you propose for checking 1 switch doesn't work but I already find that
JavaScript:
var eventId = $gameMap.eventIdXy($gameVariables.value(x),$gameVariables.value(y));
$gameSelfSwitches.value([mapId, eventId, 'switchId']);
However your script to check if self-switch are off work perfectly.
JavaScript:
['A', 'B', 'C', 'D'].every(letter => !$gameSelfSwitches.value([this._mapId, $gameMap.eventIdXy($gameVariables.value(6), $gameVariables.value(7)), letter]))

EDIT:
there is another way more compact to check if a switch is on
JavaScript:
$gameSelfSwitches.value([mapId, $gameMap.eventIdXy($gameVariables.value(x), $gameVariables.value(y)), 'switch'])

Original thread: https://forums.rpgmakerweb.com/threads/checking-event-self-switch.175841/