Declaring arrays, variables and functions

I seem to be having a little unexpected trouble here. I'm trying to create a system that requires new arrays and, ideally, functions. The problem is that no matter how I code the declaration of these things, the game returns "ReferenceError: x is not defined". Here's my current code for example:

Spoiler
var testArray = newArray('apples', 'oranges', 'pears', 'strawberries');


$gameMessage.setBackground(0)


$gameMessage.setPositionType(3)


$gameMessage.add("Your index selection is " + testArray.members() [2] + ".")


The idea was to test if the "members" index function worked on normal arrays, in hopes of bypassing the need to code a function to index a specific array. At that time I thought only my functions weren't getting defined, but unfortunately, it appears to apply to variables as well. I've also tried it as...

 

Spoiler
var testArray = ['apples', 'oranges', 'pears', 'strawberries'];


var testArray = new Array('apples', 'oranges', 'pears', 'strawberries');



Every time I'm met with the same result. My guess its that I'm unaware of a very specific way RMMV reads the code that runs a little different to what I picked up in basic Javascript tutorials.
If anyone knows what I'm doing wrong, I'd appreciate a bump in the right direction!
I assume that the real question is, can you pass an array to the gameMessage.add( ) method as a parameter or not?


What parameters does this method take?


You should take a look at that.


I unfortunately have no time to dig into the code, but a good practice (so you will learn JS better) is taking a look at the code that you will call, so you can understand how it works.
Ok, a few things:


1.

"ReferenceError: x is not defined".



var testArray = new Array('apples', 'oranges', 'pears', 'strawberries');
$gameMessage.setBackground(0)
$gameMessage.setPositionType(3)
$gameMessage.add("Your index selection is " + testArray.members() [2] + ".")

There is no "x" in the code above. Did you replace the actual error with an x, or is it actually saying x is not defined?


2.


Arrays don't have a function called "members", you just index into it. I'm going to assume you're new to programming and are just trying to follow the way it works with getting an actor from $gameParty.members(), but the difference is that $gameParty.members() is a function that returns an array. The array will be different depending on who is in the party, so it gets created on the fly when the function is called.


The reason it typically looks like $gameParty.members()[number] is because of the order in which code runs. It's similar to reading a book (left to right in almost every language), combined with the mathematical order of operations, but it can also jump a little bit. the code is pretty much executed in 3 steps:

  1. $gameParty -> Gets the object stored in $gameParty. This is an instance of the Game_Party class, and it contains a bunch of functions and a few variables.
  2. .members() -> Calls the "members" function, with no arguments, on whatever was returned from step 1.
  3. [number] -> Indexes into whatever was returned by step 2.



3.


I'm assuming you're testing these calls in script boxes in an event. When you say your variables aren't being stored, are you using separate script boxes to run your functions? I might be remembering incorrectly here, but I believe separate boxes will not share local variables (variables created with "var").

Original thread: https://forums.rpgmakerweb.com/threads/declaring-arrays-variables-and-functions.72525/