Script simple math from weighted percentage sum from variable

joket

Veteran
Veteran
Joined
Apr 24, 2021
Messages
231
Reaction score
58
First Language
English
Primarily Uses
RMMV
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
Veteran
Joined
Nov 13, 2020
Messages
82
Reaction score
64
First Language
English
Primarily Uses
RMMV
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
Veteran
Joined
Feb 18, 2018
Messages
390
Reaction score
544
First Language
French, German
Primarily Uses
RMMZ
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
Veteran
Joined
Apr 24, 2021
Messages
231
Reaction score
58
First Language
English
Primarily Uses
RMMV
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 Threads

Latest Profile Posts

It's not about your writing, it's about your mapping.
Newbie, please be gentle
reworkMZ.png
To keep myself motivated to make the MZ SF tiles normal and usable modern tiles I like to make tiny maps and toggle between the options :3
Character selection in games be like
Special Tractor Operation! :kaojoy:

Forum statistics

Threads
122,083
Messages
1,146,350
Members
160,368
Latest member
slotgacorunsurtoto
Top