How to pick a Variable with the highest amount?

Morpheus

Jack-of-Trades
Veteran
Joined
Mar 14, 2012
Messages
232
Reaction score
85
First Language
english
Primarily Uses
N/A
I'll try to make this sound not so complicated of what I'd like to accomplish.

Let's say I have Variables 23 through 31.
At the end of the game, which ever variable is the highest, I'd like to display a text message depending on the highest one.

is it possible to event this or use script/code to accomplish this? (Not plugins. But if plugins are the only way, let me know)
 

ScorchedGround

Blizzards most disappointed fan
Veteran
Joined
Apr 12, 2020
Messages
331
Reaction score
486
First Language
German
Primarily Uses
RMMV
Pick another variable and make a script call:

$gameVariables.setValue(X, Math.max(Y1, Y2, Y3, Y4, ...);

Replace X with the new variable, which will be the one used to display the message.
Replace the Y's with "$gameVariables.value(Z)"
-> Z will be the variable ID of each variable in your cycle, e.g. the 23 through 31.

So the script call would look like this for example: This is for the first 3 variables of your example.
You will of course need to go to 31.

$gameVariables.setValue(32, Math.max($gameVariables.value(23), $gameVariables.value(24), $gameVariables.value(25)));

^ After this script call Variable 32 is set to the highest number from variables 23 through 25.
 

Andar

Veteran
Veteran
Joined
Mar 5, 2013
Messages
31,434
Reaction score
7,713
First Language
German
Primarily Uses
RMMV
you'll basically need a sorting function - there are sorting scripts in every programming handbook and in some cases they were even implemented as base functions.
You only need to adapt them to whatever data you want as the original functions usually work on arrays and not on simple variables.

If you don't need to sort you can also make this an event sequence where you simply compare every variable with the current max one in event code with conditional branches - tedious but doable
 

 Masked 

Assistant
Veteran
Joined
Oct 28, 2015
Messages
90
Reaction score
261
First Language
Portuguese
Primarily Uses
RMMZ
It's quite simple to write a find max routine for this:

JavaScript:
let max = -1, maxId;
for (let i = 23; i <= 31; i++) {
  let val = $gameVariables.value(i);
  if (val > max) {
    max = val;
    maxId = i;
  }
}
// At the end, max and maxId have the maximum value and 
// the respective variableId. Use them as you need.
// e.g., putting the max on another variable:
$gameVariables.setValue(32, max);
No need for sorting algorithms or manually typing in all the variables.
 

Morpheus

Jack-of-Trades
Veteran
Joined
Mar 14, 2012
Messages
232
Reaction score
85
First Language
english
Primarily Uses
N/A
Pick another variable and make a script call:

$gameVariables.setValue(X, Math.max(Y1, Y2, Y3, Y4, ...);

Replace X with the new variable, which will be the one used to display the message.
Replace the Y's with "$gameVariables.value(Z)"
-> Z will be the variable ID of each variable in your cycle, e.g. the 23 through 31.

So the script call would look like this for example: This is for the first 3 variables of your example.
You will of course need to go to 31.

$gameVariables.setValue(32, Math.max($gameVariables.value(23), $gameVariables.value(24), $gameVariables.value(25)));

^ After this script call Variable 32 is set to the highest number from variables 23 through 25.
Thanks for the reply! I'm a bit confused on how exactly would I make the message depending on which one is highest?
 

ScorchedGround

Blizzards most disappointed fan
Veteran
Joined
Apr 12, 2020
Messages
331
Reaction score
486
First Language
German
Primarily Uses
RMMV
@Morpheus Well you would just put in the usual text code like \V[32] for example.
Or you know, whatever variable you put in for X.
 

Morpheus

Jack-of-Trades
Veteran
Joined
Mar 14, 2012
Messages
232
Reaction score
85
First Language
english
Primarily Uses
N/A
@Morpheus Well you would just put in the usual text code like \V[32] for example.
Or you know, whatever variable you put in for X.
Sorry, I meant, hypothetically speaking, let's say variable 24 is highest, then the message might say "you suck", but if 25 is highest, it might say "You tried."
 

Andar

Veteran
Veteran
Joined
Mar 5, 2013
Messages
31,434
Reaction score
7,713
First Language
German
Primarily Uses
RMMV
let's say variable 24 is highest, then the message might say "you suck", but if 25 is highest, it might say "You tried."
That is not covered by any solution so far, because you never mentioned in the original post as to what you wanted to do with the result.

and basically the solution is to store the ID of the highest variable into another variable, and then make a series of conditional branches "if X=1 show text 1, if X=2 showtext 2,.." and so on.
 

ScorchedGround

Blizzards most disappointed fan
Veteran
Joined
Apr 12, 2020
Messages
331
Reaction score
486
First Language
German
Primarily Uses
RMMV
@Morpheus In that case you would probably want to with @Andar 's suggestion.
I was not aware that you did not actually want to display the value of the variable itself.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Don't forget, aspiring writers: Personality isn't what your characters do, it is WHY they do it.
Hello! I would like to know if there are any pluggings or any way to customize how battles look?
I was thinking that when you start the battle for it to appear the eyes of your characters and opponents sorta like Ace Attorney.
Sadly I don't know how that would be possible so I would be needing help! If you can help me in any way I would really apreciate it!
The biggest debate we need to complete on which is better, Waffles or Pancakes?
rux
How is it going? :D
Day 9 of giveaways! 8 prizes today :D

Forum statistics

Threads
106,047
Messages
1,018,539
Members
137,834
Latest member
EverNoir
Top