Declaring arrays, variables and functions

TakaDynasty

Apologetic Lurker
Veteran
Joined
Dec 21, 2016
Messages
41
Reaction score
8
First Language
English
Primarily Uses
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:

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

 

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!
 

_Shadow_

Tech Magician Level:
Moderator
Joined
Mar 2, 2014
Messages
4,078
Reaction score
2,654
First Language
Greek
Primarily Uses
RMMZ
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.
 

Zalerinian

Jack of all Errors
Veteran
Joined
Dec 17, 2012
Messages
4,696
Reaction score
935
First Language
English
Primarily Uses
N/A
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").
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

Day 9 of giveaways! 8 prizes today :D
He mad, but he cute :kaopride:

Our latest feature is an interview with... me?!

People4_2 (Capelet off and on) added!

Just beat the last of us 2 last night and starting jedi: fallen order right now, both use unreal engine & when I say i knew 80% of jedi's buttons right away because they were the same buttons as TLOU2 its ridiculous, even the same narrow hallway crawl and barely-made-it jump they do. Unreal Engine is just big budget RPG Maker the way they make games nearly identical at its core lol.

Forum statistics

Threads
106,040
Messages
1,018,470
Members
137,821
Latest member
Capterson
Top