MZ Accessing an Array from more than one script call? Do I put the array in a plugin, or...?

I am super new to learning Javascript so please ELI5! ^_^

I made a script that I am extremely proud of, since I am a beginner. It uses an array. Right now, the entire script is within one script call in the editor, and it works great.

What I'd like to do is put the arrays that are already inside the script, to a central location, in order to access it from more than one event. Doing this will allow me to make edits to the array in one place instead of two (or three, or four, depending).

I tried to make it a plugin but that didn't work.
JavaScript:
(() => {


// setting coords to counter number
const counterCoords = [
{counter:1, coords:[8,6]},
{counter:2, coords:[9,6]},
{counter:3, coords:[10,6]}

];



})();

The script didn't see the array ("counterCoords" is not defined).

Is there anywhere else I can put it?

Thanks for the help!!
The best practice in RPG Maker land is to make use of an RPG Maker variable. For example:

JavaScript:
$gameVariables.setValue(42, counterCoords);
will save your counterCoords array as variable #0042. These are the same variables you use via a "Control Variables" event command.

To retrieve the variable:
JavaScript:
let counterCoords = $gameVariables.value(42);
This is considered best practice because $gameVariables is persistent. This means that if the player saves and exits the game, then the next time they load their save file, your array will still be around waiting inside variable #0042.

The rest of my post is extra information that you can skip if it gets too boring.



You're trying to expand the scope/context of your array from a script command to cover the entire game.

In most programming languages, each variable is only valid within a certain "scope" or "context". While scope and context are different, it's easier to think of them as the same thing in this case.

At school you probably had to solve lots of problems like:
Code:
x = 3 + 5
x = 6 + 7
x = 2 + 2
and so on. But hold on! Every single problem is about x. When the teacher asks: "what is x?", which x do they mean?

This is known as the "context". The teacher wants to know the x of whichever equation they're currently asking about. If they're asking about x = 3 + 5, then they mean the x that is 8. If they're asking about x = 6 + 7, they mean the x that is 13. Although they're all x, each equation has its own x, which is different from all the other x.

When you use a script call, your code is running inside an eval() function (the eval() function simply executes your JavaScript code; it's what allows you to use JavaScript in the RPG Maker editor without the need to create a plugin for that). As a result, any variable you create in a script call only exists inside that script call. When you use another script call, your code will be running in another eval() function, which has its own context.

In order to access your variable from within another script call, you need to use a higher context. The easiest way to achieve this is to use the global context, which is valid everywhere. Fortunately, $gameVariables, which is where RPG Maker stores its variables, just happens to be in the global context. Not only that, but when the player saves the game, the content of $gameVariables is also saved to the save file.

As for why your plugin didn't work, that's because it is running inside another function.
JavaScript:
() => {
  // code
}
is an arrow function. In other programming languages it is known as a "lambda function" or an "anonymous function". In other words, it is a function without a name. Since you created counterCoords inside of it, the array only exists within those {} brackets.

Plugin developers usually wrap their code within an anonymous function to avoid naming conflicts with other plugins, like when 2 plugins create a variable with the same name. They then wrap the function within round brackets () so they can use another pair of round brackets to actually run the function itself. That's why the wrapper looks like this:
JavaScript:
(() => {
  // code
})();

In other words, if you get rid of the surrounding arrow function wrapper, your plugin would work. You can try this as a learning exercise, but I still recommend using $gameVariables instead of a plugin for this particular case.
Yes. You can just put it in one of the existing game objects. I tend to use $gameVariables.

So just use $gameVariables.counterCoords and you can access it anywhere, and RPGMaker will even save it on a savegame.

@Nolonar has explained this in far better detail (I didn't see while typing), but I think it's still valuable to know that you don't have to use one of the existing variables and that you can just $gameVariables.anything.
Thanks @Nolonar and @Mike-Turtle !! I appreciate!

Nolonar: Very in-depth, I appreciate the response! It all makes sense. (Regarding the "arrows" in my plugin, I just grabbed what was in a document pinned here on how to make plugins, LOL, I wasn't sure what they were actually doing)

$gamevariables is very likely what I'm looking for, and what I use already for a lot of things, but I'm just a hair away from understanding fully. Note that the array is full of info that will never change. In this counterCoords example, counter "1" will always have "coords 8,6." Here is where I'm using them:

JavaScript:
const mouseCoordsX = $gameVariables.value(18);
const mouseCoordsY = $gameVariables.value(19);

let counterClick = counterCoords.find( ({coords}) => coords[0] === mouseCoordsX && coords[1] === mouseCoordsY)
let counterNum = counterClick.counter;

$gameVariables.setValue(14, counterNum);

And then I'd want to use them again in another script, because those coordinates are going to be used for more than one thing. (In this example, it's to find the counter number).

Questions:
  1. When I put the array into the gameVariable, would that be at the beginning of the game I suppose? Like setting another initial variable? Or any other autorun event I suppose
  2. How would the new script look in this case? Maybe like... Typing it out it actually makes a lot more sense. So really I can just grab it with let counterCoords = $gameVariables.value(42); and then the rest of the script is basically the same.
counterCoords.find and all that would still work?

Edit: ho it works!

Original thread: https://forums.rpgmakerweb.com/threads/accessing-an-array-from-more-than-one-script-call-do-i-put-the-array-in-a-plugin-or.148225/