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 }
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 🙂
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.
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.