RMMV Arrays - Getting Element index with highest Value

Status
Not open for further replies.

xabileug

Veteran
Veteran
Joined
Jul 1, 2014
Messages
330
Reaction score
95
JavaScript:
$gameVariables.setValue(159, []);
for(var i = 141; i <= 152; i++) {
 $gameVariables.value(159).push($gameVariables.value(i));
} $gameVariables.setValue(160, Math.max($gameVariables.value(159)));

I have 12 variables ID 141 to 152. I want to add them to an array game Var ID 159 to use the Math.max function. Then i will store the index (not the value) to var ID 160. But when I check ,I always get 0.

I mean I produced random numbers for the 12 variables.
1635419731063.png
 

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
3,932
Reaction score
3,161
First Language
English
Primarily Uses
RMMZ
You have two problems here:

1. setValue clamps the return value to an integer, so what you're currently doing will always result in the variable being 0 after you "store" the array in it. You need to use $gameVariables._data[159] = []; instead.

2. Math.max doesn't work on an array like that. You need to preface it with the spread operator notation (...) to expand the array into a list before applying the function to it:

JavaScript:
$gameVariables.setValue(160, Math.max(...$gameVariables.value(159)));
 

xabileug

Veteran
Veteran
Joined
Jul 1, 2014
Messages
330
Reaction score
95
You have two problems here:

1. setValue clamps the return value to an integer, so what you're currently doing will always result in the variable being 0 after you "store" the array in it. You need to use $gameVariables._data[159] = []; instead.

2. Math.max doesn't work on an array like that. You need to preface it with the spread operator notation (...) to expand the array into a list before applying the function to it:

JavaScript:
$gameVariables.setValue(160, Math.max(...$gameVariables.value(159)));
thanks! i tried it, it works i got the highest value..

JavaScript:
$gameVariables._data[159] = [];
for(var i = 141; i <= 152; i++) {
 $gameVariables.value(159).push($gameVariables.value(i));
} $gameVariables.setValue(160, Math.max(...$gameVariables.value(159)));

but now i need the index id with the highest value in the array, not the value.
 

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
3,932
Reaction score
3,161
First Language
English
Primarily Uses
RMMZ
thanks! i tried it, it works i got the highest value..

JavaScript:
$gameVariables._data[159] = [];
for(var i = 141; i <= 152; i++) {
 $gameVariables.value(159).push($gameVariables.value(i));
} $gameVariables.setValue(160, Math.max(...$gameVariables.value(159)));

but now i need the index id with the highest value in the array, not the value.
$gameVariables.setValue(160, $gameVariables.value(159).findIndex(Math.max(...$gameVariables.value(159))))

At this point I'd consider assigning your variables to shorter temporary identifiers for easier reading but that's entirely your call.
 

xabileug

Veteran
Veteran
Joined
Jul 1, 2014
Messages
330
Reaction score
95
JavaScript:
$gameVariables._data[159] = [];
for(var i = 141; i <= 152; i++) {
 $gameVariables.value(159).push($gameVariables.value(i));
}
var arr = $gameVariables.value(159);
//$gameVariables.setValue(160, Math.max(...arr));
var res = arr.findIndex(Math.max(...arr));
$gameVariables.setValue(160, res);

It's not working. I get typeerror " <max value> is not a function"
 
Last edited:

ShadowDragon

Realist
Veteran
Joined
Oct 8, 2018
Messages
4,686
Reaction score
1,801
First Language
Dutch
Primarily Uses
RMMV
it's not working because your variables is outside the function,
you should place them inside the function to read them, probably
before the "for(var)...code" part.
 

xabileug

Veteran
Veteran
Joined
Jul 1, 2014
Messages
330
Reaction score
95
it's not working because your variables is outside the function,
you should place them inside the function to read them, probably
before the "for(var)...code" part.
JavaScript:
var arr = $gameVariables.value(159);
$gameVariables._data[159] = [];
for(var i = 141; i <= 152; i++) {
 $gameVariables.value(159).push($gameVariables.value(i));
}
var mm = Math.max(...arr);
var res = arr.findIndex(mm);
$gameVariables.setValue(160, res);
//$gameVariables.setValue(160, Math.max(...arr));

made no difference.. still error
 

caethyril

^_^
Global Mod
Joined
Feb 21, 2018
Messages
2,979
Reaction score
2,303
First Language
EN
Primarily Uses
RMMZ
findIndex expects a function, details here:
Try indexOf instead, e.g.
JavaScript:
var arr = $gameVariables.value(159);
var max = Math.max(...arr);
var res = arr.indexOf(max);
$gameVariables.setValue(160, res);
 

xabileug

Veteran
Veteran
Joined
Jul 1, 2014
Messages
330
Reaction score
95
findIndex expects a function, details here:
Try indexOf instead, e.g.
JavaScript:
var arr = $gameVariables.value(159);
var max = Math.max(...arr);
var res = arr.indexOf(max);
$gameVariables.setValue(160, res);
thanks it works.. last, how can i get the variable name into the message box?
I have the var res as index + 141.. so i just need a new var ID like 161 to contain the variable name.
1635429645190.png
 

BurningOrca

Veteran
Veteran
Joined
Apr 14, 2019
Messages
243
Reaction score
211
First Language
German
Primarily Uses
RMMV
$dataSystem.variables contains the variable names, so try
$gameVariables._data[161] = $dataSystem.variables[res];
 

xabileug

Veteran
Veteran
Joined
Jul 1, 2014
Messages
330
Reaction score
95
Thanks everyone.. here's the final working code
JavaScript:
$gameVariables._data[159] = [];
for(var i = 141; i <= 152; i++) {
 $gameVariables.value(159).push($gameVariables.value(i));
}
var arr = $gameVariables.value(159);
var max = Math.max(...arr);
var res = arr.indexOf(max);
$gameVariables.setValue(160, res+141);
$gameVariables.setValue(161, $dataSystem.variables[res+141]);
 

caethyril

^_^
Global Mod
Joined
Feb 21, 2018
Messages
2,979
Reaction score
2,303
First Language
EN
Primarily Uses
RMMZ
@xabileug: great! If you want the thread closed, you can report the first post with a reason like "Solved". :kaohi:


Also, @Trihan: $gameVariables.setValue(159, []) should be OK because setValue checks if the value type before applying rounding:
JavaScript:
Game_Variables.prototype.setValue = function(variableId, value) {
    if (variableId > 0 && variableId < $dataSystem.variables.length) {
        if (typeof value === 'number') {
            value = Math.floor(value);
        }
        this._data[variableId] = value;
        this.onChange();
    }
};
It doesn't make much difference here but I thought I'd mention it anyway~
 

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
3,932
Reaction score
3,161
First Language
English
Primarily Uses
RMMZ
@xabileug: great! If you want the thread closed, you can report the first post with a reason like "Solved". :kaohi:


Also, @Trihan: $gameVariables.setValue(159, []) should be OK because setValue checks if the value type before applying rounding:
JavaScript:
Game_Variables.prototype.setValue = function(variableId, value) {
    if (variableId > 0 && variableId < $dataSystem.variables.length) {
        if (typeof value === 'number') {
            value = Math.floor(value);
        }
        this._data[variableId] = value;
        this.onChange();
    }
};
It doesn't make much difference here but I thought I'd mention it anyway~
It doesn't work though. If you try $gameVariables.setValue(159, []) and then do $gameVariables.value(159), it returns 0.

Edit: Turns out it's because the project I was using didn't have a high enough number in $dataSystem.variables, so it was bypassing the if statement entirely. If you use a variable that's been defined there, it works.
 
Last edited:

Shaz

Global Moderators
Global Mod
Joined
Mar 2, 2012
Messages
44,170
Reaction score
15,542
First Language
English
Primarily Uses
RMMV

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

 
Status
Not open for further replies.

Latest Threads

Latest Profile Posts

Happy Saint Nicholas Day! May there nice stuff in your shoes when you wake up!
Choosing Beggars are the best source of entertainment.
DavidUchina wrote on caethyril's profile.
You chad, thanks a lot for your plugins. You helped me costumize the buttons in my game.
I created the first teaser for my game! I decided to make an animation of Cabaret Helen advertising the Pizza Time Theater, set to an actual radio commercial. But this is no ordinary advertisement...
"Somber melody and stunning visuals takes one through the bittersweet tale of False Rebirth" ~ Marmalade
https://rpgmaker.net/games/12069/reviews/7576/

Got a first review on my game, on RPG Maker dot net. :kaothx:

Forum statistics

Threads
117,216
Messages
1,105,543
Members
153,362
Latest member
Luxxusburger
Top