How to get the number of the Variable that has the highest value

Hey guys! I'm doing a sort of card battle system in which the player battles the computer with 9 cards. However to write something of an AI I want to give the 9 cards that the enemy player has a probability value. The higher that value, the more likely the enemy will use that card first. There is more to it than that, but basically that's what I want to create. In any case, I will regularly have to check which of 9 variables has the highest value and then store the number of that variable into a 10th variable. This is something that is probably possible in Java script right? Unfortunately my expertise in Java script aren't good enough for this task 😮 In any case I'm really trying to avoid flooding my Common Events with enormous conditional branches, especially since I probably need to do this a few times.

Thanks in advanced for the help! 😀
PHP:
var value = [1,5,9,1,4,44,5];
var hightValue = Math.max(...value);
console.log(hightValue); // return 44 in this case
use ... for compute array
PHP:
var value = [1,5,9,1,4,44,5];
var hightValue = Math.max(...value);
console.log(hightValue); // return 44 in this case
use ... for compute array
Thanks, I think! But I don't understand enough of Java to understand what you mean with this. Especially "Use ... for compute array". I need to replace "..." with something?

And do [1,5,9,1,4,44,5] represent all the variables? Or are those just numbers and I need to replace the numbers with code for the variables I want checked?


So just in case. What I have is 9 variables, say Variable 81 through Variable 89. These will have a "chance number" in them depending on which enemy I encounter. Say I meet an enemy with these numbers:
V81: 20
V82: 25
V83: 55
V84: 5
V85: 102
V86: 120
V87: 56
V88: 12
V89: 87

Now because all these numbers can vary (remember, this is an example) I want a way to find out which Variable is the highest number and store the # of that variable into another variable (let's say V90). So in this case V90 would be set to 86 (cause that's the variable with the highest number). This is possible in javascript right? 😮
WOW, I did something similar few days ago. Not too sure if my is the best solution to your request, but it should solve the problem.

Create a control variable content, select the variable which you want to store the outcome, then put the following in the script collum:

Code:
Math.max.call(null,$gameVariables.value(X),$gameVariables.value(Y),$gameVariables.value(Z))

XYZ are the IDs fo the variables you want to compare. You can add as many as you like, just remember to separate them with commas.

I do recommend to edit this code with Textnote or something similar like Word, if you have a long list of variables you want to compare.
Because it gets harder to read the code from the script collum when the code becomes too long.

Edit: I also believe that there are better ways to write this code. But I am not a coder, so real have no idea how to do that.

Edit2: Just thought a way maybe able to simplfy the code. But I am not 100% sure, you have to test out.

Create a scripte content and put in the following:

Code:
var index;
var a = ["81", "82", "83","84"];
for (index = 0; index < a.length; ++index) {
$gameVariables.setValue(X,Math.max.call(null,$gameVariables.value(a[index]));
}

X being the ID of the variable you want the MAX number to be stored in.

var a = ["Y", "Z", "S"]
YZS are the variable IDs you want to compare, you can add as many as you like, just to separate then with commas and quotation marks.

Edit3:
Just thought another way to simplfy the code. You also have to test out if you want to use this.
NOTE: this version will only work if the variables IDs you want to compare are in sequenced order, like in your example above.

In that examole, the variables you want to compare is IDed from 81 to 89, then you can you the code bellow.

Create a scripte content and put in the following:

Code:
var a;
for (a= 81; a<90 ; ++a) {
$gameVariables.setValue(X,Math.max.call(null,$gameVariables.value(a));
}

X being the ID of the variable you want the MAX number to be stored in.

a = Y
Y being the smallest ID number in your sequence, which is 81 in this case.

a < Z
Z being the largest ID number in you sequence +1, in this case, it is 89+1 = 90.

WARNING: I can't guarantee the two simplfy version will work, you have to test out. But from what I know, they should.
Thank you for the reply AuraIPC!! I'm not quite there yet, though 🙂

WOW, I did something similar few days ago. Not too sure if my is the best solution to your request, but it should solve the problem.

Create a control variable content, select the variable which you want to store the outcome, then put the following in the script collum:

Code:
Math.max.call(null,$gameVariables.value(X),$gameVariables.value(Y),$gameVariables.value(Z))

XYZ are the IDs fo the variables you want to compare. You can add as many as you like, just remember to separate them with commas.

I do recommend to edit this code with Textnote or something similar like Word, if you have a long list of variables you want to compare.
Because it gets harder to read the code from the script collum when the code becomes too long.

This works in that it doesn't give me an error. However it gives me the highest value from those variables, and not the variable ID of the variable that contains the highest value, which is what I want. Honestly though, I could need this in the future, so it's really useful information nonetheless!! 😀

Edit: I also believe that there are better ways to write this code. But I am not a coder, so real have no idea how to do that.

Edit2: Just thought a way maybe able to simplfy the code. But I am not 100% sure, you have to test out.

Create a scripte content and put in the following:

Code:
var index;
var a = ["81", "82", "83","84"];
for (index = 0; index < a.length; ++index) {
$gameVariables.setValue(X,Math.max.call(null,$gameVariables.value(a[index]));
}

X being the ID of the variable you want the MAX number to be stored in.

var a = ["Y", "Z", "S"]
YZS are the variable IDs you want to compare, you can add as many as you like, just to separate then with commas and quotation marks.

Edit3:
Just thought another way to simplfy the code. You also have to test out if you want to use this.
NOTE: this version will only work if the variables IDs you want to compare are in sequenced order, like in your example above.

In that examole, the variables you want to compare is IDed from 81 to 89, then you can you the code bellow.

Create a scripte content and put in the following:

Code:
var a;
for (a= 81; a<90 ; ++a) {
$gameVariables.setValue(X,Math.max.call(null,$gameVariables.value(a));
}

X being the ID of the variable you want the MAX number to be stored in.

a = Y
Y being the smallest ID number in your sequence, which is 81 in this case.

a < Z
Z being the largest ID number in you sequence +1, in this case, it is 89+1 = 90.

WARNING: I can't guarantee the two simplfy version will work, you have to test out. But from what I know, they should.

When you say "create a scripte content" you mean type this into a Script Command (from the Event Command window, tab 3) right? Both of these gave me the same error I'm afraid.

Capture.PNG


99 is the ID of the variable I would like the information to go to, so... did I do something wrong? 😮

Thanks for your time so far though 🙂
Spoiler
Thank you for the reply AuraIPC!! I'm not quite there yet, though 🙂



This works in that it doesn't give me an error. However it gives me the highest value from those variables, and not the variable ID of the variable that contains the highest value, which is what I want. Honestly though, I could need this in the future, so it's really useful information nonetheless!! 😀



When you say "create a scripte content" you mean type this into a Script Command (from the Event Command window, tab 3) right? Both of these gave me the same error I'm afraid.

View attachment 91252

99 is the ID of the variable I would like the information to go to, so... did I do something wrong? 😮

Thanks for your time so far though 🙂

No, you did nothing wrong, it was my bad. Sorry.

The first one I give you is what I am currently using, so I am sure it will work, just I misunderstand your request.

And the error on the other two simplfy solution, is also my bad.

I gave you something I didn't really understand. I was to arrogant.....😢Really just want to bury myself, sorry to waste your time on that.

For make up to you, I can think of one way to help you locate the ID of the largest variable. And I am sure this will work, because we don't need script to do this.

Take the same example you give.
Variables 81 to 89.
Code:
Math.max.call(null,$gameVariables.value(X),$gameVariables.value(Y),$gameVariables.value(Z))
First, get the highest value from those variables, and store it into variable 99.

Then make few conditional branches below to check:
if variable x = variable 99

x being the variable ID of the variables you used to compare to get the max value.
In this case, they are 81 to 89.
So, basicly you will need to create 9 conditional branches for each of the variables in this case.
(Here, what we are doing with the conditonal branch is to check which variable has the same value of variable 99, which is the max value.)

Then, under each of the conditional branches, add a control variable content like below:
Control variable: variable 99 set x

Remeber? x is the ID numbers for the variables we used to compare to get the max value.
(x is just the ID number not the variable, so when you set up this control variable content, don't chose the variable, just type the ID number is the number colloum.)
upload_2018-6-2_1-9-18.png

Like above, make sure you put the corresponding x for each conditional branch.
So, this should give variable 99 the value of the ID number which contains the Max value.

I am a bit rush on my explaination, if there is anything I was not clear about, feel free to @ me, I will do my best to help.

And I am really really sorry about all the troubles I caused you with my last post.
Spoiler
No, you did nothing wrong, it was my bad. Sorry.

The first one I give you is what I am currently using, so I am sure it will work, just I misunderstand your request.

And the error on the other two simplfy solution, is also my bad.

I gave you something I didn't really understand. I was to arrogant.....😢Really just want to bury myself, sorry to waste your time on that.

For make up to you, I can think of one way to help you locate the ID of the largest variable. And I am sure this will work, because we don't need script to do this.

Take the same example you give.
Variables 81 to 89.
Code:
Math.max.call(null,$gameVariables.value(X),$gameVariables.value(Y),$gameVariables.value(Z))
First, get the highest value from those variables, and store it into variable 99.

Then make few conditional branches below to check:
if variable x = variable 99

x being the variable ID of the variables you used to compare to get the max value.
In this case, they are 81 to 89.
So, basicly you will need to create 9 conditional branches for each of the variables in this case.
(Here, what we are doing with the conditonal branch is to check which variable has the same value of variable 99, which is the max value.)

Then, under each of the conditional branches, add a control variable content like below:
Control variable: variable 99 set x

Remeber? x is the ID numbers for the variables we used to compare to get the max value.
(x is just the ID number not the variable, so when you set up this control variable content, don't chose the variable, just type the ID number is the number colloum.)
View attachment 91271
Like above, make sure you put the corresponding x for each conditional branch.
So, this should give variable 99 the value of the ID number which contains the Max value.

I am a bit rush on my explaination, if there is anything I was not clear about, feel free to @ me, I will do my best to help.

And I am really really sorry about all the troubles I caused you with my last post.

No, no, no! Please don't worry!! You were definitely not too arrogant! I didn't even think about using the thing you used for the other project and then just using conditional branches to get the Variable ID! That also means that if there are two numbers with the same height, it's always the first one that gets picked, which is absolutely fine! If anything I should apologize cause I should've known I could've figured it out with your first solution!

In any case, thank you very much for the help! It works!! And now I can continue on trying to figure out what are good reasons to change the different "chance numbers" to actually make it a sort of AI system 😀

Original thread: https://forums.rpgmakerweb.com/threads/how-to-get-the-number-of-the-variable-that-has-the-highest-value.95799/