RMMV Arrays - Getting Element index with highest Value

Status
Not open for further replies.

xabileug

Veteran
Veteran
Joined
Jul 1, 2014
Messages
311
Reaction score
87
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,910
Reaction score
3,133
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
311
Reaction score
87
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,910
Reaction score
3,133
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
311
Reaction score
87
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,673
Reaction score
1,796
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
311
Reaction score
87
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,957
Reaction score
2,291
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
311
Reaction score
87
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
311
Reaction score
87
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,957
Reaction score
2,291
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,910
Reaction score
3,133
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,078
Reaction score
15,498
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

Sizzle sizzle sizzle...
ffPfDhL.mp4
Sharing my past was a mistake. Will delete the post when I get access to a working internet connection again.
TFW you get a plugin working correctly with your other plugins ♡\( ̄▽ ̄)/♡

(CW for minor gore - Einar's Y incision is gone for his test portrait)

Cs5kAbZ.png
Doing some winter related edits for a cozy cottage!
I think it's the third time I am changing my skill info plugin, but as I get better at programming, I want to fix the trash I made earlier :p

Streaming while I am programming the plugin. Join in if you wish :)

Forum statistics

Threads
116,980
Messages
1,103,493
Members
152,844
Latest member
schuzz6
Top