RMMV Arrays - Getting Element index with highest Value

Status
Not open for further replies.

xabileug

Veteran
Veteran
Joined
Jul 1, 2014
Messages
327
Reaction score
94
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,917
Reaction score
3,153
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
327
Reaction score
94
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,917
Reaction score
3,153
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
327
Reaction score
94
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,685
Reaction score
1,799
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
327
Reaction score
94
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,976
Reaction score
2,299
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
327
Reaction score
94
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
327
Reaction score
94
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,976
Reaction score
2,299
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,917
Reaction score
3,153
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,144
Reaction score
15,535
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 Posts

Latest Profile Posts

I'm just so close to replacing LUK with a different stat, maybe DEX but we'll see.
Slapped together a title screen and working on some dungeon clutter + portraits for Dubhghaill and Einar. Usual CW for minor body horror wrt Einar
QVLJIgD.png

OaPcYUy.png

r55FiUJ.png

EIgO94y.png
cT1V4nb.png
>studying for months for an exam
>does exam
>waits 3 months for the results to come out
>me trying to login, incorrect password
>I get recovery password
>me trying to login, incorrect password

What the heck. As a son of a Karen, I'm going to file a few complaints.
Apparently Amazon Prime users get free Genshin Impact stuff like primogems and whatnot every month, but I'm not sure I ever want to fall into the abyss of F2P ever again.

Forum statistics

Threads
117,149
Messages
1,104,955
Members
153,221
Latest member
Kostec
Top