Comparing an array element to a value

Hello, everyone! First time poster here - hope I hit the correct forum for this kind of thread 🙂

I'm having problems getting some code to work. I am an absolute beginner when it comes to using script blocks so I'm terribly sorry if my mistake is one that is totally obvious to you - definitely isn't to me ^^"

This is the part that is getting me headaches in RMMV:

Code:
* If: Player is facing Down
* * Script:

      if ($gameVariables.value(3)[$gameVariables.value(11)] == 0) {
        $gameMessage.add("Ping! Fehler!");
      } else {
        $gameTemp.reserveCommonEvent(2);
      }
  *
: End
*


By the time this event runs, the 11th element of Var[3] usually is not equal to 0, meaning "else" code should be carried out. But no matter what value I set my array element to before I activate the event, only the "then" code is carried out while "else" never takes place. Why does this happen? What have I been doing wrong? I tried using === instead of ==, but that does not seem to change the outcome.

Any help is greatly appreciated! Thanks in advance 🙂
Moved to: Learning Javascript
Looks like you're using variable #3 and variable #11, what are those values when the event runs?

Code:
if ($gameVariables.value(3)[$gameVariables.value(11)] == 0) {

Let's assume that:
  • Variable #3 is an array
  • Variable #11 has a value, let's say 5

Your formula would check the 6th element of the array.
Code:
if ($gameVariables.value(3)[$gameVariables.value(11)] == 0)
// if variable #11 is 5, then it's the same as this:
if ($gameVariables.value(3)[5] == 0)

Does that make sense?
Yes, that is how it works. Variable 3 is an array containing 81 elements with Variable 11 used to point to the element desired.

Thank you so much for making me remember I have to decrease var#11 by one before executing the code above! Through all the revisions I've had til now I am pretty sure I had the code do that with it still not working, but right now it appears to have been fixed! My problem is now solved 😀

Just in case other people suffer the same problem and are wondering how I altered my code, here is the working version:


Code:
* If: Player is facing Down
   * Script:
   * Control Variables: #0011 DNG_CURR_ROOM -= 1
      if ($gameVariables.value(3)[$gameVariables.value(11)] == 0) {
        $gameMessage.add("Ping! Fehler!");
      } else {
        $gameTemp.reserveCommonEvent(2);
      }
   * Control Variables: #0011 DNG_CURR_ROOM += 1
   *
: End
*

I am new to this board... do I have to mark this as solved now or anything of the sort? 😮
Right, array indexes start at zero, so array[4] is the 5th item.

If you wanted to make it a little shorter you could subtract 1 in the same line, without actually changing the value of variable 11
Code:
if ($gameVariables.value(3)[$gameVariables.value(11) - 1] == 0)

For solved threads, click the "report" button and ask the moderators to mark the thread as solved
Thank you very much for the additional information, that is very helpful to me! Will do that!

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.

Original thread: https://forums.rpgmakerweb.com/threads/comparing-an-array-element-to-a-value.108211/