Is there a way to sum up variables?

Hi guys,
I wish to create conditional branch, that will apply if the sum of multiple variable, is bigger than a specific value.
How can I do it?
How many variables are we adding? Is it...variable?

This is infinitely expandable. Just put all the variables you want to add in the first line
Code:
var arr = [1, 2, 3, 4];//put the variable numbers here
var total = 0;
for (var i in arr) {
  total += $gameVariables.value(arr[i]);
}
6 different variables.
How many variables are we adding? Is it...variable?

This is infinitely expandable. Just put all the variables you want to add in the first line
Code:
var arr = [1, 2, 3, 4];//put the variable numbers here
var total = 0;
for (var i in arr) {
  total += $gameVariables.value(arr[i]);
}

6 different variables.
How many variables are we adding? Is it...variable?

This is infinitely expandable. Just put all the variables you want to add in the first line
Code:
var arr = [1, 2, 3, 4];//put the variable numbers here
var total = 0;
for (var i in arr) {
  total += $gameVariables.value(arr[i]);
}
Thank you!!!
I'll give it a try 🙂

(Please ignore the previous message, I didn't see you've added code before writing it).
Why can't you do this with the regular event commands?

What Aqua posted is fine if you're adding a ton of variables and you would have a hard time entering that many event commands...but for six variables, you just use Control Variables to add each one to a variable that's holding the sum.
Why can't you do this with the regular event commands?

What Aqua posted is fine if you're adding a ton of variables and you would have a hard time entering that many event commands...but for six variables, you just use Control Variables to add each one to a variable that's holding the sum.
Thing is, it might not work in my case. I'll explain -
I want the player to clear 6 different events, and only by clearing all of them, they can progress.

Problem is, in each event, they'll have multiple dialog choices, some of them are "good" choices, earning them points in a way that allows them to progress. And I want them to be able to repeat those events as many times as they want.

Now, my fear is that they will "spam" one event, choosing the same good dialog options (can be 3 in one event), thus add up to the sum endlessly.

So I thought of summing the variables in a separated parallel event, that is not triggered every time they repeat the same event again and again.

I hope I managed to explain it well enough
You can also create a common event if you need to do that multiple times. It is 6 lines of commands, nothing too complicated.
You can also create a common event if you need to do that multiple times. It is 6 lines of commands, nothing too complicated.
I'll check if it works, thanks!
Thing is, it might not work in my case.
I don't see how. Adding something is adding something, it doesn't matter whether you're doing it using an event command or the code function that event command calls inside the engine.

There's no way you can add things and get a different result.

Now, my fear is that they will "spam" one event, choosing the same good dialog options (can be 3 in one event), thus add up to the sum endlessly.

So I thought of summing the variables in a separated parallel event, that is not triggered every time they repeat the same event again and again.
Sorry, I don't understand. You want the player to be able to repeat the event as many times as they want, but you're afraid they will repeat it as many times as they want.

Only one of those things can be true 😛

And you mentioning a parallel event doesn't make any sense. You should make the variable increase when the player selects a choice that increases the variable, there's no reason to have a parallel event running in the background.
I don't see how. Adding something is adding something, it doesn't matter whether you're doing it using an event command or the code function that event command calls inside the engine.

There's no way you can add things and get a different result.


Sorry, I don't understand. You want the player to be able to repeat the event as many times as they want, but you're afraid they will repeat it as many times as they want.

Only one of those things can be true 😛

And you mentioning a parallel event doesn't make any sense. You should make the variable increase when the player selects a choice that increases the variable, there's no reason to have a parallel event running in the background.

First, I should add, if I'm making silly mistakes, sorry for that, I'm an RPG maker newbie.

Now, regarding my problem, I'll try to further explain:
Let's say, there is a dialog inside an event with choices in it.
Some choices, will provide important information to the player, and give him points (+1 to a variable), and getting enough points means the player got all the important details/clues, so he can progress with the story.

Now, I want the player to be able to explore those dialog options again and again if he want to get it figured out better, to understand the story better.
That's why I want them to trigger this dialog as many times as they want.

There are 6 different events with dialogs that can earn the player those "points".
I don't want them to repeat only one dialog to get all the "points".
I want to force them to explore these 6 events and choose the right dialog options/questions in each event, to get all the clues and only then allow him to progress with the story.

So to sum up, if I'll allow them to explore the same dialog again and again, and it always earns them more points, they can use only 1 out of 6 events, to get all the points to progress with the story.

Then I thought, instead of increasing points (variable) by +1 every time, I should set different variables, and with each right choice get a variable to =1, in each right dialog choice.
This way it won't add up when they repeat dialogs.

And that's why I thought of a separate event that will add up points from all the right dialog choices of those events.


Sorry for the mess, hope it's better explained now.
First, I should add, if I'm making silly mistakes, sorry for that, I'm an RPG maker newbie.

Now, regarding my problem, I'll try to further explain:
Let's say, there is a dialog inside an event with choices in it.
Some choices, will provide important information to the player, and give him points (+1 to a variable), and getting enough points means the player got all the important details/clues, so he can progress with the story.

Now, I want the player to be able to explore those dialog options again and again if he want to get it figured out better, to understand the story better.
That's why I want them to trigger this dialog as many times as they want.

There are 6 different events with dialogs that can earn the player those "points".
I don't want them to repeat only one dialog to get all the "points".
I want to force them to explore these 6 events and choose the right dialog options/questions in each event, to get all the clues and only then allow him to progress with the story.

So to sum up, if I'll allow them to explore the same dialog again and again, and it always earns them more points, they can use only 1 out of 6 events, to get all the points to progress with the story.

Then I thought, instead of increasing points (variable) by +1 every time, I should set different variables to =1, in each right dialog choice.
This way it won't add up when they repeat dialogs.

And that's why I thought of a separate event that will add up points from all the right dialog choices of those events.


Sorry for the mess, hope it's better explained now.
In this case, unless I am missing something, let the player rack up all the points they want and only add the presence of a value rather than the value itself.

Edit: That being said, I am certain event commands would be fine since it is only 6 variables and you're going to have to end up assigning the value to another game variable anyways to use it in a conditional branch.

Chaining off of Aqua's formula:
JavaScript:
const val = [2,4,6,7,8,11] // variable IDs
let total = 0;
for (const id of val) {
    total += $gameVariables.value(id) ? 1 : 0;
}
you could do it all in one variable and save 5 with bitwise operations

at the start of all of that, use the control variable window to set a variable to 0 to make sure its cleared

then have each choice will run similar commands with the control variables script box.
the first runs
Code:
$gameVariables.value(x) | 0b100000
the second runs
Code:
$gameVariables.value(x) | 0b010000
the third
Code:
$gameVariables.value(x) | 0b001000
and so on. just replace the x with the variable id

then at the place where you check if theyve done all the dialogues, check if the variable is equal to 63. if the player has done all of them, it will be 63. if not, they missed some choice(s)
In this case, unless I am missing something, let the player rack up all the points they want and only add the presence of a value rather than the value itself.

Edit: That being said, I am certain event commands would be fine since it is only 6 variables and you're going to have to end up assigning the value to another game variable anyways to use it in a conditional branch.

Chaining off of Aqua's formula:
JavaScript:
const val = [2,4,6,7,8,11] // variable IDs
let total = 0;
for (const id of val) {
    total += $gameVariables.value(id) ? 1 : 0;
}
There are 6 events, but 2-3 choices within each one that can earn a point.
Using the variable presence though sounds like a good approach, worth a try.
Thank you!
you could do it all in one variable and save 5 with bitwise operations

at the start of all of that, use the control variable window to set a variable to 0 to make sure its cleared

then have each choice will run similar commands with the control variables script box.
the first runs
Code:
$gameVariables.value(x) | 0b100000
the second runs
Code:
$gameVariables.value(x) | 0b010000
the third
Code:
$gameVariables.value(x) | 0b001000
and so on. just replace the x with the variable id

then at the place where you check if theyve done all the dialogues, check if the variable is equal to 63. if the player has done all of them, it will be 63. if not, they missed some choice(s)

OK, that's really interesting.
Can it work if a specific event/dialog can give me more than one point? for example, can I use the code like this:
Code:
$gameVariables.value(x) | 0b200000

Here for example, I want to indicate that in the first dialog, the player got 2 choices right.
Can it work if a specific event/dialog can give me more than one point? for example, can I use the code like this:
depends on what your points represent. I figured each one represented a bit of information the player's character gleaned from the events. if its that, then mentally "assign a detail" to each of those digits after the b and if a choice should give 2, youd do
Code:
$gameVariables.value(x) | 0b100010
or
$gameVariables.value(x) | 0b001001
or
$gameVariables.value(x) | 0b011000
whichever details should be given should have their corresponding digit be 1.

I assumed you had 6 details, but if you dont - however many details you have is however many digits you should have, and the number you check to make sure if the player has them all is 2^(number of details)-1
depends on what your points represent. I figured each one represented a bit of information the player's character gleaned from the events. if its that, then mentally "assign a detail" to each of those digits after the b and if a choice should give 2, youd do
Code:
$gameVariables.value(x) | 0b100010
or
$gameVariables.value(x) | 0b001001
or
$gameVariables.value(x) | 0b011000
whichever details should be given should have their corresponding digit be 1.

I assumed you had 6 details, but if you dont - however many details you have is however many digits you should have, and the number you check to make sure if the player has them all is 2^(number of details)-1
Ahhh, so if in total, I have 10 "points", and not 6, I can just add four digits, for ex: 0b0000000000.

I think I get it. Thank you 🙂
How many variables are we adding? Is it...variable?

This is infinitely expandable. Just put all the variables you want to add in the first line
Code:
var arr = [1, 2, 3, 4];//put the variable numbers here
var total = 0;
for (var i in arr) {
  total += $gameVariables.value(arr[i]);
}
This looks useful. How/where do you specify which variable will hold the sum of all of these variables?

In my case, I need to find the sum of over 30 variables, so I worry that using events to do it will cause lag.
You can add a line at the end of Aqua's example to store the result in the variable of your choice, e.g.
JavaScript:
var arr = [1, 2, 3, 4];
var total = 0;
for (var i in arr) {
  total += $gameVariables.value(arr[i]);
}
// Store result in game variable ID 123.
$gameVariables.setValue(123, total);
Or, equivalently, use Control Variables -> Script:
  • [1,2,3,4].reduce(function(total, id) { return total + $gameVariables.value(id); }, 0);
This is just a rearrangement of the previous example.

Personally I'd just use commands, even for 30 variables. I would certainly not expect any "lag" from doing so! To each their own, though~

Since this thread has been quiet for nearly a year, I think the OP's question was answered, so~

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

If you have further questions, feel free to start a new thread! 👋

Original thread: https://forums.rpgmakerweb.com/threads/is-there-a-way-to-sum-up-variables.170934/