MZ Change multiple variables value at once

Hi there,
Is there a script call that I can use to change the value of multiple variables at once? I know I can do it individually by using the "Control variable" command. But I was hoping if I could do the same thing more efficiently using a single script call. Thanks.
Uuh... what's your plan with multiple variables at once?
Don't get me wrong, scripts and plugins that help people to create stuff on their own instead of copying pre-built systems are a very great thing.

Correct me if I'm wrong, in RPGMV/MZ, the control-command should still allow you to choose a batch-range of variable-IDs as target for repeatedly executing the very same action with all of them.

If the opportunity was missed to order those variables in a sequence of incremental IDs to properly use this function, the mistake here might be bad planning.

RPG2000/2003 lack of an open re-programmable engine and have stuff like allowing you to e.g. specify a target-variable based on one or multiple pointers, but if stuff on that level is what you need for something in MV/MZ, some red flags are raising that you might wanna create something that is considered too complex and unstable to be ever handled by the event-interpreter and should be programmed in JS entirely instead.

~炬燵あ
But its allready in the Eventcommand:

Control Variables -> Range

The Scripting way could be a Loop that iterates through a Range of Variable Ids.
Or an Array that got individual Ids.

This Thread maybe helps, iam not good with Syntax and have no time at the moment to search the correct Scripture.
Control Variables -> Range won't work for me, as I am trying to set different values for different variables.
For example, let's say, I want the value of variable 2,3, 5, and 8 is 5, 3, 9 and 7. And I want to do all that with a single scriptcall to keep things tidy.
Control Variables -> Range won't work for me, as I am trying to set different values for different variables.
For example, let's say, I want the value of variable 2,3, 5, and 8 is 5, 3, 9 and 7. And I want to do all that with a single scriptcall to keep things tidy.
JavaScript:
const data = $gameVariables._data;
[data[2], data[3], data[5], data[8]] = [5, 3, 9, 7];
JavaScript:
const data = $gameVariables._data;
[data[2], data[3], data[5], data[8]] = [5, 3, 9, 7];
Thanks, friend. This is exactly what I was looking for. 🙂 Much appreciated.
Thanks, friend. This is exactly what I was looking for. 🙂 Much appreciated.
if you have a lot of values to set you can streamline it more by calling the identifier "d" instead of "data", then you can do like d[2] instead and it's more compact.

Alternatively, what you could do is something like

JavaScript:
const values = [[2, 5], [3, 3], [5, 9], [8, 7]];
for (const pair of values) {
  $gameVariables.setValue(pair[0], pair[1]);
}
Thanks. That really helps. And while I have you, is there any similar script call to do the same with self-switches? I wanted to change the self-switch A of events 4,5,6,7,8,9 of Map 16. I usually do it with this script:
var key = [mapId, eventId, A-D]
$gameSelfSwitches.setValue(key, true);
Is there any better way to change the value of multiple self-switch at once?
use the same syntax for key as the example for data several posts above.
this will not work with functions, but it will work for arrays whether they are const or var.

you would still have to call the setvalue function in a loop for every kay.
use the same syntax for key as the example for data several posts above.
this will not work with functions, but it will work for arrays whether they are const or var.

you would still have to call the setvalue function in a loop for every kay.
I tried that, but probably because of my minimal coding knowledge, I couldn't make it work. Can you please give me an example?
I'm not sure if pressing four unrelated-seeming different values into four different variables with only one line of code can be considered "kept tidy". But okay, as long as it's only you who has to find their way through it if something has to be replaced or adjusted later again.

~炬燵あ
Well, in one of the events of my game, I have to enter eight variables between each dialogue. I could have used a common event, but I also have to change the value of some variables as well. Which created a big mess. That's why I'm looking for this types of sulution.
I'm not sure if pressing four unrelated-seeming different values into four different variables with only one line of code can be considered "kept tidy". But okay, as long as it's only you who has to find their way through it if something has to be replaced or adjusted later again.

~炬燵あ

Well, in one of the events of my game, I have to enter eight variables between each dialogue. I could have used a common event, but I also have to change the value of some variables as well. Which created a big mess. That's why I'm looking for this types of sulution.
Thanks. That really helps. And while I have you, is there any similar script call to do the same with self-switches? I wanted to change the self-switch A of events 4,5,6,7,8,9 of Map 16. I usually do it with this script:
var key = [mapId, eventId, A-D]
$gameSelfSwitches.setValue(key, true);
Is there any better way to change the value of multiple self-switch at once?
Are you trying to set them all to the same value?
Are you trying to set them all to the same value?
Yes. I want all the self-switches A to turn ON/OFF
JavaScript:
const events = [4, 5, 6, 7, 8, 9];
for (const id of events) {
  $gameSelfSwitches.setValue([$gameMap.mapId(), id, 'A'], true);
}

or

JavaScript:
const d = $gameSelfSwitches._data;
const m = $gameMap.mapId();
d[[m, 4, 'A']] = d[[m, 5, 'A']] = d[[m, 6, 'A']] = d[[m, 7, 'A']] = d[[m, 8, 'A']] = d[[m, 9, 'A']] = true;
JavaScript:
const events = [4, 5, 6, 7, 8, 9];
for (const id of events) {
  $gameSelfSwitches.setValue([$gameMap.mapId(), id, 'A'], true);
}

or

JavaScript:
const d = $gameSelfSwitches._data;
const m = $gameMap.mapId();
d[[m, 4, 'A']] = d[[m, 5, 'A']] = d[[m, 6, 'A']] = d[[m, 7, 'A']] = d[[m, 8, 'A']] = d[[m, 9, 'A']] = true;
It worked! Thanks again for your help. 🙂
I'd just like to point out that doing this by using setValue inside a for loop is definitely better than modifying _data directly, since the latter won't call onChange, and might cause some weird issues with events using the wrong pages or plugins that respond to variables not noticing the change.

I guess you could just call onChange yourself when you're done, but it's probably safer to just stick to using setValue.
JavaScript:
const events = [4, 5, 6, 7, 8, 9];
for (const id of events) {
  $gameSelfSwitches.setValue([$gameMap.mapId(), id, 'A'], true);
}

or

JavaScript:
const d = $gameSelfSwitches._data;
const m = $gameMap.mapId();
d[[m, 4, 'A']] = d[[m, 5, 'A']] = d[[m, 6, 'A']] = d[[m, 7, 'A']] = d[[m, 8, 'A']] = d[[m, 9, 'A']] = true;
Hi there, sorry to bother you again, but what change should I make to this script call to ON/OFF the self-switches in a different Map? For example, I want to disable self-switch A of the multiple events of map 18 from map 25.
Hi there, sorry to bother you again, but what change should I make to this script call to ON/OFF the self-switches in a different Map? For example, I want to disable self-switch A of the multiple events of map 18 from map 25.
So the same event IDs but on different maps?

Original thread: https://forums.rpgmakerweb.com/threads/change-multiple-variables-value-at-once.152003/