How to pick a Variable with the highest amount?

Morpheus

Jack-of-Trades
Veteran
Joined
Mar 14, 2012
Messages
231
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
481
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,367
Reaction score
7,676
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
231
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
481
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
231
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,367
Reaction score
7,676
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
481
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

People3_5 and People3_8 added!

so hopefully tomorrow i get to go home from the hospital i've been here for 5 days already and it's driving me mad. I miss my family like crazy but at least I get to use my own toiletries and my own clothes. My mom is coming to visit soon i can't wait to see her cause i miss her the most. :kaojoy:
Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.

Forum statistics

Threads
105,868
Messages
1,017,085
Members
137,585
Latest member
Reversinator
Top