If variable id of another variable's value = ...

Sorry if title makes little to no sense, but I wanna set the variable's id in the conditional branch to be the value of another variable.

Explaining the whole thing i'm trying to make might make it easier to understand. I have 7 variables with different values and I want to choose the biggest from them, but some of them might have the same number so if I just do a big if else statement, the one that comes up first with take priority. That's why i'm trying to set which comes first to be random.

Hope this makes more sense and thank you in advance!
Hello, try this:
Hello, try this:
Hi, thanks for the reply! Those do work but I still have the problem of if multiple variables have the same value, the one thats checked first, the conditional that comes first or the one earlier in the array, will always take priority. Is there a way to get around that?
Hi, thanks for the reply! Those do work but I still have the problem of if multiple variables have the same value, the one thats checked first, the conditional that comes first or the one earlier in the array, will always take priority. Is there a way to get around that?
JavaScript:
const ids = [1, 2, 3, 4, 5];
const values = ids.map(id => $gameVariables.value(id));
const max = Math.max(...values);
const matches = ids.filter(id => values[id] === max);
const randomId = matches[Math.randomInt(matches.length - 1)];
JavaScript:
const ids = [1, 2, 3, 4, 5];
const values = ids.map(id => $gameVariables.value(id));
const max = Math.max(...values);
const matches = ids.filter(id => values[id] === max);
const randomId = matches[Math.randomInt(matches.length - 1)];
Thanks for the answer, but how do you implement code into events? Is it via script?

I also want to make sure I understand what the code means first,
first line is to lay out the var ids for the ones I want to compare
second line is to use the ids from before to get the values with .map()
third line is to find the highest value (this I'm also kinda confused by ...values)
forth line is to filter which ids have to same value as the highest one and put it in a new var
fifth is just to choose a random one from the ids with highest value

There's also a few other code related things I wanna ask,
what is =>
the (...values)
why does the last line need a .length -1
Thanks for the answer, but how do you implement code into events? Is it via script?

I also want to make sure I understand what the code means first,
first line is to lay out the var ids for the ones I want to compare
second line is to use the ids from before to get the values with .map()
third line is to find the highest value (this I'm also kinda confused by ...values)
forth line is to filter which ids have to same value as the highest one and put it in a new var
fifth is just to choose a random one from the ids with highest value

There's also a few other code related things I wanna ask,
what is =>
the (...values)
why does the last line need a .length -1
JavaScript:
const ids = [1, 2, 3, 4, 5]; // The list of variable IDs being used
const values = ids.map(id => $gameVariables.value(id)); // .map runs a function against each element of ids; in this case, it's replacing the ID number with the value of the variable at that ID
const max = Math.max(...values); // Get the max value in the values array, using the spread operator to expand the array into separate arguments
const matches = ids.filter(id => values[id] === max); // Get all ids where the corresponding variable is equal to the max value in values
const randomId = matches[Math.randomInt(matches.length - 1)]; // Select a random ID from the matches

If you wanted to actually assign it to a variable, you'd then need something like $gameVariables.setValue(10, randomId); and then variable 10 would contain the ID of the variable selected at random.

=> is for arrow notation. It's basically a shorthand way of writing anonymous functions. Take the following example:

JavaScript:
let total = 0;
const myArray = [1, 2, 3, 4, 5];
myArray.forEach(function(element) {
 total += element;
};
console.log(total); // 15

Using arrow notation, you can make it more concise:

JavaScript:
myArray.forEach(element => total += element);

(notwithstanding that reduce would be better to use for that, it was just an example)

..., as I said above, is the spread operator. Before to use an array with Math.max, we had to use .apply, but now with the spread operator we can just pass an array to it directly.

The reason it's length - 1 is that array indexes start at 0. If we just did .length, it would be generating random indexes with a range 1 higher than the actual number of things it has available.
If you want to fully understand snippets of code, asking questions related to different topics and skill levels isn't a great approach, educationally.

You'd be better off working through a proper, organized tutorial (or course or book or whatever). I suggest w3schools, starting with the Statements chapter.

I was going to say that @Trihan's code still requires you to do something with the randomId at the end, but then he posted while I was writing this 😉
Small correction:
The reason it's length - 1 is that array indexes start at 0. If we just did .length, it would be generating random indexes with a range 1 higher than the actual number of things it has available.
Math.randomInt(max) returns a random integer from 0 to max - 1 inclusive. So I believe it should be Math.randomInt(matches.length) instead.
Small correction:

Math.randomInt(max) returns a random integer from 0 to max - 1 inclusive. So I believe it should be Math.randomInt(matches.length) instead.
Ah yeah, you're right.
JavaScript:
const ids = [1, 2, 3, 4, 5]; // The list of variable IDs being used
const values = ids.map(id => $gameVariables.value(id)); // .map runs a function against each element of ids; in this case, it's replacing the ID number with the value of the variable at that ID
const max = Math.max(...values); // Get the max value in the values array, using the spread operator to expand the array into separate arguments
const matches = ids.filter(id => values[id] === max); // Get all ids where the corresponding variable is equal to the max value in values
const randomId = matches[Math.randomInt(matches.length - 1)]; // Select a random ID from the matches

If you wanted to actually assign it to a variable, you'd then need something like $gameVariables.setValue(10, randomId); and then variable 10 would contain the ID of the variable selected at random.

=> is for arrow notation. It's basically a shorthand way of writing anonymous functions. Take the following example:

JavaScript:
let total = 0;
const myArray = [1, 2, 3, 4, 5];
myArray.forEach(function(element) {
 total += element;
};
console.log(total); // 15

Using arrow notation, you can make it more concise:

JavaScript:
myArray.forEach(element => total += element);

(notwithstanding that reduce would be better to use for that, it was just an example)

..., as I said above, is the spread operator. Before to use an array with Math.max, we had to use .apply, but now with the spread operator we can just pass an array to it directly.

The reason it's length - 1 is that array indexes start at 0. If we just did .length, it would be generating random indexes with a range 1 higher than the actual number of things it has available.
Thank you for your explanations, I've put it in my event but it returned 0 for me. And on closer look, am i suppose to change the "id" s?
This is a screenshot of the script:
Thank you for your explanations, I've put it in my event but it returned 0 for me. And on closer look, am i suppose to change the "id" s?
This is a screenshot of the script:
What are the values in those variables at the time?
What are the values in those variables at the time?
Typing it out mad it a bit hard to read so I screenshotted from my log, the script wasn't changed but I'll put it here as well just in case
Typing it out mad it a bit hard to read so I screenshotted from my log, the script wasn't changed but I'll put it here as well just in case
Oh sorry, that's my bad.

Change the line that sets matches to

JavaScript:
const matches = ids.filter(id => $gameVariables.value(id) === max)
Oh sorry, that's my bad.

Change the line that sets matches to

JavaScript:
const matches = ids.filter(id => $gameVariables.value(id) === max)
Thanks a bunch! It's working perfectly now, I do wanna ask why does it need to be changed into a gameVariables? Is it because on the second line, it was put into gameVariables.value(id)? Is that's the case why does it need to be a gameVariable?

Second thing, do you have any resource I can look into for scripting? I know very surface level of js and I think I could do a lot more with the game but the thing that's really confusing me is the rpgmaker elements? functions? not sure what they are called and how to use them. I tried looking it up but they all either seems to be for plugins.
Second thing, do you have any resource I can look into for scripting? I know very surface level of js and I think I could do a lot more with the game but the thing that's really confusing me is the rpgmaker elements? functions? not sure what they are called and how to use them.
Trihan actually has a resource about this, the bottom right graphic in his signature is his Jump Into JavaScript thread that goes through the game engine's files describing what the functions do.

My personal recommendation: do tutorials. Trihan's thread gives an explanation, but it's not at a "surface level" beginner place, in my opinion. If you don't know how to understand what JavaScript code does by reading it, or...y'know, what a function is...I don't think trying to jump into advanced work like that is going to do you any favors.

Of course, I already gave that recommendation, so it might not be worth much to you 😛 But, as a trained, experienced coder and as an educator, it's my best advice.
As previously suggested, I have found w3schools.com free online JS course to be very helpful. That, coupled with the MZ script call reference sheet and experimentation. And when I can't get something to work right in MZ that seems like it should work according to those resources, I post something here on the forum and someone can usually help to clarify my mistake pretty quickly.

This thread is a pretty good example of that.

Good luck!

Original thread: https://forums.rpgmakerweb.com/threads/if-variable-id-of-another-variables-value.166169/