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,138
Reaction score
15,532
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

"Yeah. It's been a rough road lately. But that's the thing about the Rangers. It doesn't matter how hard it gets, or how many of them fall. They never stop fighting. The Rangers NEVER stop fighting".

Angela Deth, you are a hero of mine. Sorry for my first run of Wasteland 3.
We must press on. For all those we couldn't save. For all those we might yet save.
FirestormNeos wrote on DarkPlasmaBall's profile.
asking this here instead of the thread in question to avoid derailing it, but I'm curious; is there a particular reason you'd include a "Terms of Use" for an RPG Maker game? Like, are there multiplayer features for the game you're working on?
The holidays are upon us! Everyone search for safety from the grocery store music!
I don't understand people sometime. Walking to work today came across someone walking the opposite way in a nice suit, I politely move to the side into the shrubbery so he doesn't have to get his suit dirty and say "good morning" as he passes me, he turns around glaring daggers and asks "why are you talking to me?" like he's bloody offended by me wishing him a good morning or something. Just, why?

Forum statistics

Threads
117,138
Messages
1,104,892
Members
153,202
Latest member
EnderMon
Top