#### Myers & Sparks

##### Veteran
Been using RPG maker for a while, but let's just saying JS isn't quite my forte.

In any case, what would be the best method to look a batch of variables. and return the highest variable?

for example I have 3 variables. 1,2,3 with respective values of 5,8,2,

What would be the best method of indexing this variable and storing it to a new variable?

I think it would have something to do with math.max but again. JS isn't my strong suit.
Been stumped on this all day. any advice is appreciated.

FAST EDIT

I found this snippet for @Sumrnddde but again. My JS is terrible so if someone can explain if this is in the right direction.

var maxValue = 0;for(i = 197; i < 200; i++){ if(\$gameVariables.value(i) > maxValue) { maxValue = \$gameValue.value(i); \$gameVariables.setValue(6, i); }}

#### MushroomCake28

##### KAMO Studio
Code:
``````var checkVar = [1, 2, 3]; // Put all the variable ID you want to check inside the array
var max = 0; // this represents the highest value
var nid = 5; // put the id of the variable in which you want to store the value.

for (var i = 0; i < checkVar.length; i++) {
var va = \$gameVariables.value(checkVar[i]);
if (va > max) {
max = va;
}
}

\$gameVariables.setValue(nid, max);``````

EDIT: Basically the same thing as SRD.

#### Myers & Sparks

##### Veteran
First thanks for the help. It worked, however, upon testing this, this only give me the value of the the highest variable.

I was looking for it to report the variable ID itself, if that makes any sense? Is this possible?

#### MushroomCake28

##### KAMO Studio
Here's the code to store the variable id of the variable with the highest value inside variable #nid (see in the code line 4).

Code:
``````var checkVar = [1, 2, 3]; // Put all the variable ID you want to check inside the array
var max = 0; // this represents the highest value
var vid = 0; // this represents the id of the variable with the highest value
var nid = 5; // put the id of the variable in which you want to store the value.

for (var i = 0; i < checkVar.length; i++) {
var va = \$gameVariables.value(checkVar[i]);
if (va > max) {
max = va;
vid = checkVar[i];
}
}

\$gameVariables.setValue(nid, vid);``````

#### Myers & Sparks

##### Veteran
Here's the code to store the variable id of the variable with the highest value inside variable #nid (see in the code line 4).

Code:
``````var checkVar = [1, 2, 3]; // Put all the variable ID you want to check inside the array
var max = 0; // this represents the highest value
var vid = 0; // this represents the id of the variable with the highest value
var nid = 5; // put the id of the variable in which you want to store the value.

for (var i = 0; i < checkVar.length; i++) {
var va = \$gameVariables.value(checkVar[i]);
if (va > max) {
max = va;
vid = checkVar[i];
}
}

\$gameVariables.setValue(nid, vid);``````

Worked like a charm. Thank you so much.

You sir/ma'am have just earned yourself a free copy of the game once completed as this was a large part of a system I'm crafting.

Thanks again

#### Myers & Sparks

##### Veteran
@BrightIdeaProduction Thanks a lot! Glad I could help!

Sry to bug again.

So i reverse engineered this and made a lowest score as well. but i have a small question. Even though I have the min set to 0 it will still find values below 0 . I.e -1, 2 etc.

Is there a way to ensure that it only calculates positive integers?

I ask because I would like to "remove" people who were eliminated from future runnings and the easiest way i can think to do it is set the variables lower so the script wont want to calculate them.

var checkVar = [100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121]; // Put all the variable ID you want to check inside the array

var min = 0; // this represents the highest value

var vid = 95; // this represents the id of the variable with the highest value

var nid = 96; // put the id of the variable in which you want to store the value.

for (var i = 0; i < checkVar.length; i++) {

var va = \$gameVariables.value(checkVar);

if (va < min) {

min = va;

vid = checkVar;

}

}

\$gameVariables.setValue(nid, vid);

#### MushroomCake28

##### KAMO Studio
Wait, you want to completely exclude negative values, consider them as 0, or do an absolute value (-3 would become 3) ?

#### Myers & Sparks

##### Veteran
Wait, you want to completely exclude negative values, consider them as 0, or do an absolute value (-3 would become 3) ?

Ignore them completely. Also upon inspection the snippet won't return the lowest value for some reason. It only returns 95 for the value which is the variable itself..

#### MushroomCake28

##### KAMO Studio
This code will return the lowest value of all variables in the checkVar array. It will return the value, not the variable id.

Code:
``````var checkVar = [1, 2, 3]; // Put all the variable ID you want to check inside the array
var min = 0; // this represents the lowest value
var vid = 0; // this represents the id of the variable with the lowest value
var nid = 5; // put the id of the variable in which you want to store the value.

for (var i = 0; i < checkVar.length; i++) {
var va = \$gameVariables.value(checkVar[i]);
if (va < min && va >= 0) {
min = va;
vid = checkVar[i];
}
}

\$gameVariables.setValue(nid, min);``````

#### Myers & Sparks

##### Veteran
This code will return the lowest value of all variables in the checkVar array. It will return the value, not the variable id.

Code:
``````var checkVar = [1, 2, 3]; // Put all the variable ID you want to check inside the array
var min = 0; // this represents the lowest value
var vid = 0; // this represents the id of the variable with the lowest value
var nid = 5; // put the id of the variable in which you want to store the value.

for (var i = 0; i < checkVar.length; i++) {
var va = \$gameVariables.value(checkVar[i]);
if (va < min && va >= 0) {
min = va;
vid = checkVar[i];
}
}

\$gameVariables.setValue(nid, min);``````

I could only get a value of 0 on the return.

EDIT: sorry for not being specific fatigue My lowest value will be anything but 1. (2,3. etc.) and it will only return 0

Last edited:

#### MushroomCake28

##### KAMO Studio
It's returning 0 because 0 is the lowest value of the variables you input. The code currently sets a threshold and doesn't check any value below that threshold (threshold is 0 in the previous code). If you want a different threshold, then change this line:

Code:
``if (va < min && va >= 0)``

Replace the 0 by the value you want. Don't forget that it's greater or equal, so if you put 2, it's going to start taking into accounts numbers that are greater or equal to 2.

#### Myers & Sparks

##### Veteran
It's returning 0 because 0 is the lowest value of the variables you input. The code currently sets a threshold and doesn't check any value below that threshold (threshold is 0 in the previous code). If you want a different threshold, then change this line:

Code:
``if (va < min && va >= 0)``

Replace the 0 by the value you want. Don't forget that it's greater or equal, so if you put 2, it's going to start taking into accounts numbers that are greater or equal to 2.

I don't think it's the threshold, Currently my values for variables are set to 1, 9, 1 but it still dumps 0 into the lowest score.

#### MushroomCake28

##### KAMO Studio
Ohh sorry, my mistake! Here's the new code:

Code:
``````var checkVar = [1, 2, 3]; // Put all the variable ID you want to check inside the array
var min = \$gameVariables.value(checkVar[0]); // this represents the lowest value
var vid = 0; // this represents the id of the variable with the lowest value
var nid = 5; // put the id of the variable in which you want to store the value.

for (var i = 0; i < checkVar.length; i++) {
var va = \$gameVariables.value(checkVar[i]);
if (va < min && va >= 0) {
min = va;
vid = checkVar[i];
}
}

\$gameVariables.setValue(nid, min);``````

Problem was the min variable was set to 0 at initialization, and 0 would always be lower than your other variables lol.

#### Myers & Sparks

##### Veteran
Ohh sorry, my mistake! Here's the new code:

Code:
``````var checkVar = [1, 2, 3]; // Put all the variable ID you want to check inside the array
var min = \$gameVariables.value(checkVar[0]); // this represents the lowest value
var vid = 0; // this represents the id of the variable with the lowest value
var nid = 5; // put the id of the variable in which you want to store the value.

for (var i = 0; i < checkVar.length; i++) {
var va = \$gameVariables.value(checkVar[i]);
if (va < min && va >= 0) {
min = va;
vid = checkVar[i];
}
}

\$gameVariables.setValue(nid, min);``````

Problem was the min variable was set to 0 at initialization, and 0 would always be lower than your other variables lol.

Ah, understood. I made all the fixes and now I can track the highest and lowest properly.

Thanks again for your help. I'll let you know when the demo is out

#### Myers & Sparks

##### Veteran
Okay, I promise this is the last request. So maybe to break down how I'm running things. 4 different common events.

1 to calculate highest score.
2 Compare scores to "high score"
3 Calculate Lowest score.
4 Compare scores "lowest"

So far with your help I got the first 3 functional with your help and some stress
I tried to fit the snippet from comparing high scores to low scores adjusting it in a ad hoc manner. Like so:

var checkVar = [100, 101, 102];

var min = \$gameVariables.value(checkVar[0]); // this represents the lowest value

var vid = 95; // this represents the id of the variable with the lowest value

var nid = 96; // put the id of the variable in which you want to store the value.

for (var i = 0; i < checkVar.length; i++) {

var va = \$gameVariables.value(checkVar);

if (va < min && va >=0) {

min = va;

nid = \$gameVariables.value(checkVar[0]);

}

}

\$gameVariables.setValue(nid, vid);

However it will only log the actual variable value (95 in this case)
and not the lowest scorer. I tried multiple iterations of logging and storing the values, to avoid being a hassle, but again. I'm not a coder, I just play one on TV

Any help appreciated as always

#### MushroomCake28

##### KAMO Studio
I don't understand... you want to get the lowest value of all variables in the array right? Isn't that what the previous code I sent do? (btw no need to touch the min and the vid line. You just need to put all the variable id in the array and change the nid, which is the id of the variable in which to store the result).

Code:
``````var checkVar = [1, 2, 3]; // Put all the variable ID you want to check inside the array
var min = \$gameVariables.value(checkVar[0]); // this represents the lowest value
var vid = 0; // this represents the id of the variable with the lowest value.
var nid = 5; // put the id of the variable in which you want to store the value.

for (var i = 0; i < checkVar.length; i++) {
var va = \$gameVariables.value(checkVar[i]);
if (va < min && va >= 0) {
min = va;
vid = checkVar[i];
}
}

\$gameVariables.setValue(nid, min);``````

#### Myers & Sparks

##### Veteran
I don't understand... you want to get the lowest value of all variables in the array right? Isn't that what the previous code I sent do? (btw no need to touch the min and the vid line. You just need to put all the variable id in the array and change the nid, which is the id of the variable in which to store the result).

Code:
``````var checkVar = [1, 2, 3]; // Put all the variable ID you want to check inside the array
var min = \$gameVariables.value(checkVar[0]); // this represents the lowest value
var vid = 0; // this represents the id of the variable with the lowest value.
var nid = 5; // put the id of the variable in which you want to store the value.

for (var i = 0; i < checkVar.length; i++) {
var va = \$gameVariables.value(checkVar[i]);
if (va < min && va >= 0) {
min = va;
vid = checkVar[i];
}
}

\$gameVariables.setValue(nid, min);``````

when I run the Score high and High player common event it works, logging both values of the highest score and logging the variable of said highest value into the new variable (as you well know)

However when I try to duplicate this in the lowest value as shown below, I get the lowest number, but not the variable's ID itself.

When I run both, I only get 95 as a return. Below is a shot after running all 4 events and the results.

As you see it properly logs Highest score, Highest player, as well as lowest score. but for some reason it just dumps 95 for the 1st calculation. Afterwards it just starts to dump random numbers. I think calculations of the expression listed??

Edit: I hope this makes any sense

Last edited:

### Latest Profile Posts

Draw something
Me: No problemo! ''draws something''
Now draw the same thing from a different angle
Me: So....You have discovered my weakness...
I got a new PC... Nothing wrong with my lil laptop at all! Still works great, and I'll be using it for school. But, the specs for gaming were falling off fast. So I was selfish and bought myself a beast of a PC ^-^

Held hostage in my own house half the day by an old man that cant take hints or read body language, STILL manage to make some pixels! booyah! I should get a promotion!....No wait...I should get a job
Potions are to be hoarded, not used...
Moar screenshots!!!