How to find Variable with Highest Value?

SumRndmDde

Follower of RNGesus
Veteran
Joined
Jul 30, 2013
Messages
225
Reaction score
423
First Language
English
Primarily Uses
RMMV
Pretty much, let's say I have a bunch of variables, and I wish to find out which one is the highest, how would I do so?

I created this:

var maxValue = Math.max($gameVariables[1], $gameVariables[2], $gameVariables[3], $gameVariables[4], $gameVariables[5]); if($gameVariables[1] == maxValue)$gameVariables[6] = 1;if($gameVariables[2] == maxValue)$gameVariables[6] = 2;if($gameVariables[3] == maxValue)$gameVariables[6] = 3;if($gameVariables[4] == maxValue)$gameVariables[6] = 4;if($gameVariables[5] == maxValue)$gameVariables[6] = 5;but it has failed to work. 

Can someone explain what I need to do?
 
Last edited by a moderator:

kiriseo

Veteran
Veteran
Joined
Oct 27, 2015
Messages
245
Reaction score
82
First Language
German
Hmm... when I look at the Game_Variables class, you have to use $gameVariables.value(1) to get the value of the first variable.
 

Bex

Veteran
Veteran
Joined
Aug 2, 2013
Messages
1,492
Reaction score
408
First Language
German
Primarily Uses
RMMV
end_variable_id = 0help_value = 0for id in (1..50) #<--Variable_Idsif $game_variables[id] >= help_valueend_variable_id = idhelp_value = $game_variables[id]endendNow somehow using the id safed in end_variable_id somehow for your stuff, or exchange end_variable_id with a $game_variables[?]Iam not a Scripter but maybe this somehow helps.
 

SumRndmDde

Follower of RNGesus
Veteran
Joined
Jul 30, 2013
Messages
225
Reaction score
423
First Language
English
Primarily Uses
RMMV
Hmm... when I look at the Game_Variables class, you have to use $gameVariables.value(1) to get the value of the first variable.
Oh my god!

That makes so much more sense.

Everything works now.

I also switched my code to this:

var maxValue = 0;for(i = 0; i < 5; i++){  if($gameVariables.value(i) > maxValue)  {    maxValue = $gameValue.value(i); $gameVariables.setValue(6, i);  }}so for anyone reading this later, that's how you do it.
 
Last edited by a moderator:

Bex

Veteran
Veteran
Joined
Aug 2, 2013
Messages
1,492
Reaction score
408
First Language
German
Primarily Uses
RMMV
Nice....but thats to high for me at the moment as starter...
 

kiriseo

Veteran
Veteran
Joined
Oct 27, 2015
Messages
245
Reaction score
82
First Language
German
Oh my god!

That makes so much more sense.

Everything works now.

I also switched my code to this:

var maxValue = 0;for(i = 0; i < 5; i++){  if($gameVariables.value(i) > maxValue)  {    maxValue = $gameValue.value(i); $gameVariables.setValue(6, i);  }}so for anyone reading this later, that's how you do it.
Hmm... are you sure that it works as intended?

You start at $gameVariables.value(0) and end at $gameVariables.value(4).

As far as I know, MV don't save anything from the database (items, classes, variables) at array index 0.

And in your code before, you took the indexes 1 'tll 5.

Nice....but thats to high for me at the moment as starter...
It is, to some extent, the same method you described.

But it is simpler in regards to only use one variable, not two :D
 

SumRndmDde

Follower of RNGesus
Veteran
Joined
Jul 30, 2013
Messages
225
Reaction score
423
First Language
English
Primarily Uses
RMMV
Hmm... are you sure that it works as intended?

You start at $gameVariables.value(0) and end at $gameVariables.value(4).

As far as I know, MV don't save anything from the database (items, classes, variables) at array index 0.

And in your code before, you took the indexes 1 'tll 5.
Oh wow, that's embarrassing.

Yeah, you're right, I made a little typo.

It's actually:

var maxValue = 0;for(i = 1; i <= 5; i++){  if($gameVariables.value(i) > maxValue)  {    maxValue = $gameValue.value(i); $gameVariables.setValue(6, i);  }}Thanks for pointing that out.

When I wrote it here, I did it by memory and I wrote the for-loop the way I usually do instead of the altered method needed for this.
 
Last edited by a moderator:

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

Latest Threads

Latest Posts

Latest Profile Posts

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.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??

Forum statistics

Threads
105,864
Messages
1,017,056
Members
137,573
Latest member
nikisknight
Top