# Script simple math from weighted percentage sum from variable

#### joket

##### Veteran
Hello I'd like to show a percentage result from a weighted calculation of variables.
100% should be the total sum of variable 1, 2, 3 and 4 weighted by different % each.
Let's say:
variable 1 value is 10, weight is 20%
variable 2 value is 3, weight is 33%
variable 3 value is 50, weight is 10%
variable 4 value is 20, weight is 37%

How can I make these calcs on a script call?
tried: but doesn't work -->

\$gameVariables.setValue(5, \$gameVariables.value(\$gameVariables.value(1)/20*100+ \$gameVariables.value(2)/33*100 + \$gameVariables.value(3)/10*100 + \$gameVariables.value(4)/20*100))

#### autodidact

##### Veteran
Hm, maybe I missed something, but if you want 20% shouldn't you be doing 20/100.

For example, you could write:
`\$gameVariables.value(4)/(20/100)`
Or just do:
`\$gameVariables.value(4)/0.2`

#### Nolonar

##### Veteran
I think it's better to use only 1 variable, which will contain all values including their weights:
JavaScript:
``````\$gameVariables.setValue(13, [
{ value: 10, weight: 0.2 },
{ value: 3, weight: 0.33 },
{ value: 50, weight: 0.1 },
{ value: 20, weight: 0.37 }
]);``````
This will store your data in variable #13.

From there, it's as simple as calculating `value * weight / sum_of_all_weights`. In this case, the `sum_of_all_weights` just happens to be 1 (100%), but let's use it anyway, so we can use arbitrary weights without having to calculate their percentage first.

JavaScript:
``````var data = \$gameVariables.value(13);
var sumOfWeights = data.map(function(d) { return d.weight }).reduce(function(a, b) { return a + b; }, 0);

\$gameVariables.setValue(14, data.map(function(d) { return d.value * d.weight / sumOfWeights; }));``````
This will store the weighted values from variable #13 into variable #14 (you could also store it back into variable #13 instead, if you want).

You can retrieve each data point like this:
JavaScript:
``\$gameVariables.setValue(15, \$gameVariables.value(14)[0]);``
This will store the first element from variable #14 into variable #15. In this case, the value is 2 (because `10 * 20% = 2`).
JavaScript:
``\$gameVariables.setValue(16, \$gameVariables.value(14)[1]);``
This will store the second element from variable #14 into variable #16. In this case, the value is 0.99 (because `3 * 33% = 0.99`).

And so on. Just remember that arrays (what we use to store multiple data into a single variable) are 0-based, so the first element has index 0, the second has index 1, etc. By contrast, RPG Maker variables and switches are 1-based, so the first variable/switch has index 1 instead.

Last edited:

#### joket

##### Veteran
Thank you for the very exstensive reply.
The thing with using only one variable is that I do not have absolute static values for those variables, but the are dynamic. The main goal here is to make a variable showing a % of game completion.
Hence, each of the 4 variables represent game achievements (let's say enemies, powerups, areas, levels and so on). I do not have a maximum number for them but I'll keep track of the values by increasing their relative nr. count variable. I'd like to get a final other variable value I can show anytime it's called, resulting in the % of completion. The average of the percentage is an additional plus, where unlock areas should be weighted more than enemies or powerups!

### Latest Profile Posts

Marketing seems to be about reducing dependence on luck to sell a product, as well as expanding the number of avenues in which luck can play a role in selling a product.
So, I've been wanting to do this randomized logic puzzle in my game for years now. I finally figured out how to do it by using arrays and a few small script calls. It's based on the Einstein Fish Puzzle! Though, I have no idea how to actually have the player put in answers.... might start a thread about it.

The projectiles shown in this video are as follows:
1, Basic Triple
2, 30-Degree Arc
3, Randomized Blast Wave
4, 8-way
5, 8-way Double
6, 8-way Slowness
7, 120-Degree Arc Repetitive
8, Projective Triple
9, Split 8-way
10, 2-way Split Randomized Arc
11, HP-MP Split Arc
12, Split Laser
13, Shock
14, Cosine
Drop shadows and water reflections tests in RPG Maker VX Ace

I'm doing a fairy tale project Little Red Riding Hood and the Woodsman. I'm almost done with the demo in my native language and I intend to make a demo version in English as well. I'm just going to use the translator because I don't know the pronunciations very well, I only know how to say "Hey" and nothing else..