RMMV Arrays - Getting Element index with highest Value

Status
Not open for further replies.

xabileug

Veteran
Veteran
Joined
Jul 1, 2014
Messages
333
Reaction score
98
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
333
Reaction score
98
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
333
Reaction score
98
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,692
Reaction score
1,805
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
333
Reaction score
98
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,986
Reaction score
2,304
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
333
Reaction score
98
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
333
Reaction score
98
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,986
Reaction score
2,304
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,200
Reaction score
15,547
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

aww man, color hex code #C4C4C4 doesn't work for the tint value in this scene I need something darker, which stinks just because I liked that one it was my favorite plastic explosive three times...#696969 (nice) should work better (WHY IS MY BRAIN LIKE THIS)
Tell it to me straight, doc. If I've been trying to piece together a "shorter" game to learn stuff outside of game jams... am I just running away from my WIP? :v
Well, I've got a party tomorrow, and my friends are coming over. I wanted to work a bit on the Fandom Scouts assets so I can be ready when that update arrives... in other news, Pizza Time Horror is getting a second teaser, and it shows Cabaret Helen's last performance before the location closed.
Also who used to be scared of Chuck E as a kid? I was! Now I love him!
The new cards and what they do:

(I am clearly not a youtuber... Haha)

Forum statistics

Threads
117,282
Messages
1,106,096
Members
153,456
Latest member
tomdavies
Top