lolshtar

Master of Magic thatknow nospell
Veteran
Joined
Apr 13, 2013
Messages
702
Reaction score
103
First Language
French
Primarily Uses
RMMV
I'm trying to get the highest variables out of these in a condition, how would l do that?


$gameParty.members()[0]._zxc = Math.randomInt(100 * $gameParty.members()[1].tgr)


$gameParty.members()[1]._zxc = Math.randomInt(100 * $gameParty.members()[1].tgr)


$gameParty.members()[2]._zxc = Math.randomInt(100 * $gameParty.members()[1].tgr)


if ("highest variable number === $gameParty.members()[0]._zxc) {


do this }


if ("highest variable number === $gameParty.members()[1]._zxc) {


do this }


if ("highest variable number === $gameParty.members()[2]._zxc) {


do this }
 

Astfgl66

Veteran
Veteran
Joined
Jan 5, 2016
Messages
734
Reaction score
601
First Language
French
Primarily Uses
Hi!


Most of the time code is just about putting your problem into words:


You want to know what the maximum value of a suite of numbers is: how would you do it yourself on paper?


Probably you'd start by looking at the first value and remembering it. Then you'd check the second, and if it is higher remember that one instead and that it's the second.


Then you'd repeat the process with as many values you have, progressing one by one.


In code you'd do the same thing.


I suggest trying to do it yourself following the steps I described above, you'll learn more this way.


In case you are stumped: below is a way to do it, generalized for any number of values.

var variable //define your array
var index = 0; //start: check index 0
var currentMax = variable[0]; //set maximum to the value at index 0

for (var i = 0; i <variable.length; i++) { //for the entire array
if (variable > currentMax) { //if the value of the checked index is > to the current maximum value
index = i; //new index
currentMax = variable; //new currentMaximum
}
}

// index will be the index of the max Value
// currentMax will be the actual value;
// Then you coud do a condition about the index:
if (index === 0) {do something} // if maximum value is inside variable[0] do thing




Hope I helped, and good luck with your coding!
 

Silverskin

Villager
Member
Joined
Jun 21, 2016
Messages
27
Reaction score
12
First Language
English
Primarily Uses
you can also compare variables to each other.


A possible condition would be: Var A > Var B && Var A > Var C


Hope I could help :)
 
Last edited by a moderator:

lolshtar

Master of Magic thatknow nospell
Veteran
Joined
Apr 13, 2013
Messages
702
Reaction score
103
First Language
French
Primarily Uses
RMMV
Yeah, I'm having a very hard time with array even after years of using rpg maker. Probably because l have no idea what i++ mean or how to write it.


For example, someone write this to me to determine which party member had the highest TP and I still have no idea how it even works


if ($gameParty.members().sort(function(b1,b2){


return (b1.tp > b2.tp) ? 1 : ( (b1.tp < b2.tp) ? -1 : 0 );


}).pop() === user) {


condition = true;


}


@silverskin


yeah, already did that. I still need arrays though when i'm going to use a lot more variables.
 
Last edited by a moderator:

Astfgl66

Veteran
Veteran
Joined
Jan 5, 2016
Messages
734
Reaction score
601
First Language
French
Primarily Uses
i++ means increase the value of i by 1;


It's a short way of writing i += 1;


You don't need to know every single thing in javascript to be able to do what you want. You can write a for loop without knowing what i++ means.


Below is an explanation, using my limited understanding and very little experience with sort, of how the function you pasted works:


$gameParty.members().sort(function) //this will sort the array $gameParty.members() in increasing order using the function provided.
//The function provided is:
function(b1,b2){
return (b1.tp > b2.tp) ? 1 : ( (b1.tp < b2.tp) ? -1 : 0 );
}
//a ? b : c is short for if(a) {b} else {c} So it could also be written
if (b1.tp > b2.tp) { //if member 1 tp > member 2 tp
1
} else {
if (b1.tp < b2.tp) { //if member 1 tp < member 2 tp
-1
} else { //if member 1 tp = member 2 tp
0}
}
//Explaining how sort works exactly is a little complicated, you can take a look at this page: it explains it well
// http://www.w3schools.com/js/js_array_sort.asp

//So now we have sorted your array in increasing order
array.pop() //removes the last thing in an array and returns it.
//So our expression
$gameParty.members().sort(function(b1,b2){return (b1.tp > b2.tp) ? 1 : ( (b1.tp < b2.tp) ? -1 : 0 );}).pop()
//will return the party member with the maximum tp.

//The next line is just a basic conditional to check if the expression returns the user, and if it does, make a condition true.
if (expression === user) {condition = true;}


I hope this clarified things for you.


You could have written this using a loop just as I did above, or in many other ways probably.


Edit: here is a solution using sort for your problem, untested but it should work:


$gameParty.members().sort(function(a, b){return a._zxc - b._zxc});


This will sort the party members by the value of their "_zxc" property.
You'll then be able to use the array.pop() or array.shift() function to return the first or the last element.
 
Last edited by a moderator:

Latest Threads

Latest Profile Posts

Hi everyone! Enjoying this nice long weekend. How are you all doing? :)
I just successfully tested having events check if specific actors are in a particular party slot and then changing image to match. This will let me make scenes take into account who is in the party at the time and arrange them accordingly - this is important since I'll have extra actors beyond current party. The script used is the same from MV and is compatible with my current core plugin setup in MZ. Thank goodness.
That awkward moment when you're talking about thinking about writing a limited shop stock plugin for MZ and then find out you already wrote one...last May. My memory is so bad.
When I open my project and I'm like, hell yeah.
Some drawings I made in Lapis, one day I intend to involve them in some RPG Maker project.
* maybe two wall frames
*or some sprite flower tiara, or maybe it inspires someone to do something.
bdd19b62e539d17b3b00cc3d63034f7d.jpg

fcca5c8471b34483da8e032cfe579275.jpg

Forum statistics

Threads
118,424
Messages
1,115,848
Members
155,376
Latest member
L4TNG
Top