Check multiple switches and variables for specific values

I have 100 switches with 100 corresponding variables and I need someway to check all of em efficiently.

I have 100 variables, each corresponding to a specific character that increments every time a "day" passes in my game, so during the event that switches to the next day all 100 variables are incremented by 1. I want to have certain events activate when any of these values reaches 10, however I only want that to happen if the character in question is relevant at that moment in game. I have 100 switches that each activate when a character becomes relevant, and deactivate when they stop being relevant. The way the game is set up any combination of these 100 characters is possible at any given time. Basically, I can set it up like this:
If switch(character1) = true, then variable(character1) += 1;
if variable(character1) == 10, then event;
the problem is this would require me to write literally 100 if statements, which I do not want to do.
Is there a better way of doing this? I'm open to JS and/or plugins is I'm lucky enough that those solutions exist.
The second I hit post I might have thought up a solution:
On day change, teleport player to an empty room with 1 event in it for each character, that has their relevant switch as their condition. The events then increases their variable by 1, then checks if that value is now 10. If its 10; play event, if its not; "delete event".
This way, I only have to make one general event, that I can then copy 99 times and simply tweak the variables in each event, saving a lot of time.
Leaving this thread as unanswered as I have no idea if this will work, and someone might post a solution that does.
Yes, it's done easily.

I don't understand from your explanation why you need 100 variables. It seems like it should be sufficient to increment the day one time, then check for any relevant actors.

(for that matter, are the 100 switches entirely necessary? You can't just check whether they're in your party, or something? There's a difference between "in the party" and "relevant"?)

But this will do what you need. It presumes your variables and switches are numbered 1-100, and that the switch for an actor is the same number as their variable. If that's not the case, you'll need to add some math.

Code:
[...Array(100).keys()].map(x => ++x).forEach(index => {if ($gameSwitches.value(index) $gameVariables._data[index]++;});

Wait, I might have misunderstood. This will only increment the variable if the switch is on...did you want to say "execute this event after incrementing the variable, but only if their switch is on"?

That would require some kind of data structure for you to have these events associated with the variables and switches.
the problem is this would require me to write literally 100 if statements, which I do not want to do.
Or you could just write a javascript for loop, but I cant help but think your entire setup with 100 variables and switches is way more convoluted than it needs to be and it can all be replaced with a few lines of code.
Yes, it's done easily.

I don't understand from your explanation why you need 100 variables. It seems like it should be sufficient to increment the day one time, then check for any relevant actors.
I need 100 variables as I want each character to do something every 10th day after they become relevant to the plot.
(for that matter, are the 100 switches entirely necessary? You can't just check whether they're in your party, or something? There's a difference between "in the party" and "relevant"?)
The characters are not in the players party (tbh i didn't know you could have 100 in your party, that might make things easier).
But this will do what you need. It presumes your variables and switches are numbered 1-100, and that the switch for an actor is the same number as their variable. If that's not the case, you'll need to add some math.

Code:
[...Array(100).keys()].map(x => ++x).forEach(index => {if ($gameSwitches.value(index) $gameVariables._data[index]++;});

Wait, I might have misunderstood. This will only increment the variable if the switch is on...did you want to say "execute this event after incrementing the variable, but only if their switch is on"?
If the switch is on, increment variable. After this, if the new value of the variable is 10, run the relevant event. After that, set variable value to 0. There is one unique event for each of the 100 characters.

Or you could just write a javascript for loop, but I cant help but think your entire setup with 100 variables and switches is way more convoluted than it needs to be and it can all be replaced with a few lines of code.
I would love a suggestion for how to replace this with code, unfortunately my coding knowledge stops at simple objects.

I'm thinking it would be a lot easier to replace the switch with a check if the variable value is >0, and instead have the variable increment by 1 when the character become relevant, and then rather have the event play when the value reaches 11.
The characters are not in the players party (tbh i didn't know you could have 100 in your party, that might make things easier).
You put as many actors into your party as you like. The player uses the Formation command (or a party plugin or some evented system) to determine who is actually going into battle - by default, the first 4 actors in the party do.

I need 100 variables as I want each character to do something every 10th day after they become relevant to the plot.
So that sounds like I did give you the correct code - you only want each actor's variable to increment if they are relevant, with relevance established by their switch being on.

After that, you can use the regular event commands to check the variables for Mod 10 == 0 which will tell you the value is a multiple of 10.

I'm thinking it would be a lot easier to replace the switch with a check if the variable value is >0, and instead have the variable increment by 1 when the character become relevant, and then rather have the event play when the value reaches 11.
That's a logical idea. Here's a change to the code I gave you before:
Code:
[...Array(100).keys()].map(x => ++x).forEach(index => $gameVariables._data[index] += $gameVariables._data[index]>0 ? 1 : 0);
Will increment variables 1-100 IFF they are greater than 0.
You put as many actors into your party as you like. The player uses the Formation command (or a party plugin or some evented system) to determine who is actually going into battle - by default, the first 4 actors in the party do.


So that sounds like I did give you the correct code - you only want each actor's variable to increment if they are relevant, with relevance established by their switch being on.

After that, you can use the regular event commands to check the variables for Mod 10 == 0 which will tell you the value is a multiple of 10.


That's a logical idea. Here's a change to the code I gave you before:
Code:
[...Array(100).keys()].map(x => ++x).forEach(index => $gameVariables._data[index] += $gameVariables._data[index]>0 ? 1 : 0);
Will increment variables 1-100 IFF they are greater than 0.
Thank you for the code, I really appreciate it!
However, I'm not entirely sure on how to implement it:
I assume changing the "index" in
$gameVariables._data[index]
to "n", where n is the relevant variable, but since n is a range (301 to 400) I'm not entirely sure on how to implement your code.

Heres one solution i thought up while we were chatting:
One array is binary, 1 if character is relevant, 0 if not.
Second array is how many days since character became relevant.
simplified code:
loop start
if array1[n] = 1, then array2[n] += 1
n += 1
if n < 99, then loop
else
n == 0
loop start
if array2[n] = 10, play event(n)
n += 1
if n < 99, then loop
else, finish
I would do it with two arrays, i.e.
var activeChars = [['Bob', 2], ['Jenna', 3], ['Mia', 0]];

var inactiveChars = [everyone else];

Push a character into the active array as they become relevant, move them into the second array when not active.

But it would require you to learn basic Javascript array manipulation.
Thank you for the code, I really appreciate it!
However, I'm not entirely sure on how to implement it:
You paste it into a Script box.

If you aren't using 1-100 but 301-400, you just change each instance of [index] to [index+300]

Heres one solution i thought up while we were chatting:
I don't see how two arrays makes it anything but more complicated. It doesn't give you any benefit.

The only way that makes sense is if your actors can go in and out of relevance, but then it's easier to just use the original code I supplied with the switches. You have plenty of switches available.

And if you really wanted to go more hardcore with coding it, I would put a boolean (switch) onto each actor before I used a second array.
. I have 100 switches that each activate when a character becomes relevant, and deactivate when they stop being relevant.
The only way that makes sense is if your actors can go in and out of relevance,
That's how I read the requirements, which is why I would do two arrays, so you would only have to run commands to increment variables on the first one. You still need somewhere to store the data for inactive characters.
You paste it into a Script box.

If you aren't using 1-100 but 301-400, you just change each instance of [index] to [index+300]
That is very convenient haha
I don't see how two arrays makes it anything but more complicated. It doesn't give you any benefit.

The only way that makes sense is if your actors can go in and out of relevance, but then it's easier to just use the original code I supplied with the switches. You have plenty of switches available.
The code has to support the actors switching between relevance and non-relevance, and also any possible combination of relevant and non-relevant actors
And if you really wanted to go more hardcore with coding it, I would put a boolean (switch) onto each actor before I used a second array.
I'm interested in why this would be a better, wouldn't an array with 100 values be essentially the same as 100 booleans, given that each value in the array is tied to a specific actor?
You still need somewhere to store the data for inactive characters.
I should probably have stated that when an actor becomes irrelevant, their "counter" resets to 0
That's how I read the requirements, which is why I would do two arrays, so you would only have to run commands to increment variables on the first one. You still need somewhere to store the data for inactive characters.
The OP needs to clarify. When he says "I want each character to do something every 10th day after they become relevant," that tells me that becoming irrelevant resets the count. (i.e. makes their variable 0)

If that's not correct, then you are right and storing data while they're irrelevant is necessary.

I'm interested in why this would be a better, wouldn't an array with 100 values be essentially the same as 100 booleans, given that each value in the array is tied to a specific actor?
It's the same in terms of the amount of data. It's easier, syntactically, to turn a boolean on and off than it is to shuffle actors between arrays.
I should probably have stated that when an actor becomes irrelevant, their "counter" resets to 0
Then you only need one array and just push and pop entries into and out of it as characters go in and out of relevance.
Then you only need one array and just push and pop entries into and out of it as characters go in and out of relevance.
Going back to your previous post:
var activeChars = [['Bob', 2], ['Jenna', 3], ['Mia', 0]];
Here, the number refers to the "counter" of each actor I assume. Moving data in and out of arrays I can do, but I'm unsure of how to increment that value when the position also contains a string.
Going back to your previous post:
var activeChars = [['Bob', 2], ['Jenna', 3], ['Mia', 0]];
Here, the number refers to the "counter" of each actor I assume. Moving data in and out of arrays I can do, but I'm unsure of how to increment that value when the position also contains a string.
JavaScript:
var activeChars = [['Bob', 2], ['Jenna', 3], ['Mia', 0]];

for (i=0; i<activeChars.length; i++){
activeChars[i][1]++;
}
Or, using JavaScript functions to be a bit more succinct:
Code:
[['Bob', 2], ['Jenna', 3], ['Mia', 0]].forEach(character => character[1]++);

But doing something like that puts a bit more onus on you to learn/use coding commands because you're going to have to create those objects in the array every time you make a character ir/relevant. Unless Aqua has something in mind that I'm missing, I don't think that adds anything in terms of functionality that you couldn't do with your existing list of game variables.

It does allow you to not have to use so many of them (but, frankly, you should have access to way more than you need 😉)
JavaScript:
var activeChars = [['Bob', 2], ['Jenna', 3], ['Mia', 0]];

for (i=0; i<activeChars.length; i++){
activeChars[i][1]++;
}
Or, using JavaScript functions to be a bit more succinct:
Code:
[['Bob', 2], ['Jenna', 3], ['Mia', 0]].forEach(character => character[1]++);

But doing something like that puts a bit more onus on you to learn/use coding commands because you're going to have to create those objects in the array every time you make a character ir/relevant. Unless Aqua has something in mind that I'm missing, I don't think that adds anything in terms of functionality that you couldn't do with your existing list of game variables.

It does allow you to not have to use so many of them (but, frankly, you should have access to way more than you need 😉)
Thanks all, I really appreciate all the help, gonna spend some time trying this out and seeing if I can get it working!

Original thread: https://forums.rpgmakerweb.com/threads/check-multiple-switches-and-variables-for-specific-values.161382/