MZ Help; Array on Game Variable

Hello guys, I hit some problem when using Array on game variable.
Basically, I want to check each my array per index while using loop via event.

Here's my event.
  1. The problem, the system only checking true/false once.
  2. Since this is on the loop, it should be checking every time till the loop is done.
  3. So, how I can solve this case?

Untitled.png


In this video, the system only checks my array once at the first index, then they don't check it.

There are two mistakes you're making here.

$gameVariables.value is a function that only takes one argument. To access a value from the list, the [] operator should go after the function that returns that list, not inside it. So in your case $gameVariables.value(34)[$gameVariables.value(7)].

Additionally, array indexes start at 0, i.e. array[1] will return the second element.

And while not a mistake per se, I highly recommend only using === and not ==, at least until you learn what the difference is.
I was gonna say all the stuff @Mac15001900 did, but he posted first 😉

I will add - you don't need to make two separate Conditional Branches when you're only checking two possibilities. Just make one Conditional Branch and check the Else box.
It's work!
Thank you guys, and...I'm forgot that array always start from 0 lol.

@ATT_Turan Ehehe, I knew it. I just want to make sure that condition work properly.
Since in the final product, this system should be check 3 or 4 condition to the index.

You guys save my life, THANKS!
as a headup, post the entire window of the event page, as if it's on parallel,
the loop is already looping, if action button, it's different.

pushing object/entries into the array is always after the last one, so make sure
you control this nicely to get the correct returned value.

array of [1,4,6,8] and you check push("4") it become [1,4,6,8,"4"] (5th entry here).
not entirely sure of your mechanic, but keep it in mind for the entire event window
to see if there other pages/action/parallel trigger/event touch etc, so we don't give
incorrect information as it can play a role ^^
array of [1,4,6,8] and you check push("4") it become [1,4,6,8,"4"] (5th entry here).
I'm not sure what you're warning against here - the array is initialized as empty on the very first line of the event.

So every element is getting added in order, the OP just wasn't correctly referencing the elements (and incremented the index at the wrong place).

Maybe I'm misunderstanding what you mean.
If you are able to use 0 and 1 as your false and true, that could save you from having to see if it is false (0) or true (1).

Assuming you need it to be 1 for true and 2 for false, you could assign it directly to variable 34 like this with 1 event command and another script call in a loop - control variable using the script portion. I like using the double exclamation (!!) to make it true/false:

Edit: (variable 7 is reset to 0):
JavaScript:
◇Control Variables : #0007 Number = 0
◇Control Variables : #0034 = [...Array(5)].map(x=>Math.randomInt(2)+1);
◇Loop
  ◇Script : const v = $gameVariables._data;
  :       : $gameMessage.add(`${!!(v[34][v[7]] - 2)}`);
  :       : this.setWaitMode('message');
  ◇Control Variables : #0007 Number += 1
  ◇If : Number = 5
    ◇Break Loop
    ◇
  : End
: Repeat Above
◇
Here are the scripts separately:
JavaScript:
[...Array(5)].map(x=>Math.randomInt(2)+1);
JavaScript:
const v = $gameVariables._data;
$gameMessage.add(`${!!(v[34][v[7]] - 2)}`);
this.setWaitMode('message');
Subtracting the value by 2 makes it -1 (which returns as true as boolean) or 0 (false).

This is assuming I understood what you were wanting since I couldn't see anything else in the event beyond what you shared (or I may have made a mistake and missed something), of course.

Original thread: https://forums.rpgmakerweb.com/threads/help-array-on-game-variable.173199/