How to use Objects in an Array in MZ

Ok, so I admit defeat. I have been trying to figure out how to use Objects in an Array in MZ for days now and nothing that I try works. I have tried every suggestion I have found for creating an Object, adding Objects to an Array, retrieving Objects from an Array, removing Objects from an Array, etc. I have searched for every option I can think of an I even tried to follow the simple steps that are in the book Java Script For Kids that I am learning from.

I know how to use Arrays, but the Objects part is clearly eluding me.

And even though the Java Script that I have found seems easy enough, I just can't make it work in MZ. I must be missing something painfully obvious.

Would anyone be so kind as to provide a very simple example of how to add Objects to an Array, use the keys/values pairs from the Array, add new Objects or key/value pairs, change the values of keys, and removing objects or key/value pairs?

For instance, say I wanted to create an Array that stores the attributes of characters in Objects within the Array. So I have Characters 1-4, each with 4 attributes that have numerical values. Like STR: 16, INT: 14, DEX: 12, CON: 17.

I want to be able to add new characters to the Array, change values to appropriate Objects in the Array, and then I want to access those attributes for any given character to display.

I am either missing something really simple or maybe this isn't as easy in MZ as I think.

I would post my trials, but they are too numerous and I have no idea which ones were close and which ones were so far off it isn't funny.

Any help is greatly appreciated since clearly I am not going to be able to figure this out on my own.

And maybe using Objects in Arrays isn't that useful. I sort of thought it would be, but maybe not.

The Objects are hurting my brain.

Thanks!
Not really sure what you're looking for here in terms of examples...
JavaScript:
// define array of two objects
let arr = [
  { name: "Alex", str: 16, int: 14, dex: 12, con: 17 },
  { name: "Billie", str: 10, int: 17, dex: 17, con: 12 }
];
// add a couple more
arr.push({ name: "Cheese", str: 20, int: 20, dex: 20, con: 20 });
arr.push({ name: "Deadbeef", str: 5, int: 5, dex: 5, con: 5 });
// change name property of index 3 (fourth entry)
arr[3].name = "Imposter";
Note that if you want to use plugin parameters, those are all string values. For an array of objects, you need to do something like:
JavaScript:
let arrOfObjects = JSON.parse(param).map(e => JSON.parse(e))
Even then, all the object values will be strings.

In case it helps: you can run code and check values via the Console tab of the dev tools window (press F8 during playtest).
Not really sure what you're looking for here in terms of examples...
JavaScript:
// define array of two objects
let arr = [
  { name: "Alex", str: 16, int: 14, dex: 12, con: 17 },
  { name: "Billie", str: 10, int: 17, dex: 17, con: 12 }
];
// add a couple more
arr.push({ name: "Cheese", str: 20, int: 20, dex: 20, con: 20 });
arr.push({ name: "Deadbeef", str: 5, int: 5, dex: 5, con: 5 });
// change name property of index 3 (fourth entry)
arr[3].name = "Imposter";
Note that if you want to use plugin parameters, those are all string values. For an array of objects, you need to do something like:
JavaScript:
let arrOfObjects = JSON.parse(param).map(e => JSON.parse(e))
Even then, all the object values will be strings.

In case it helps: you can run code and check values via the Console tab of the dev tools window (press F8 during playtest).
Thank you, @caethyril ! Your script is similar to other things I found and tried.

Apparently, what I seem to be missing is how to access the information to display or use it.

I copied and pasted the first part and then tried getting a value from it.

For instance, I tried using the following:

$gameVariables.value(395) = arr[1].name

But I get an error.

Previously, I tried using the $gameVariables.value instead of the "let" function, but I couldn't do that either.

If you are using scripts to work with the array and objects, how do you set any of the values to a variable so it can be used?

Thanks!
Thank you, @caethyril ! Your script is similar to other things I found and tried.

Apparently, what I seem to be missing is how to access the information to display or use it.

I copied and pasted the first part and then tried getting a value from it.

For instance, I tried using the following:

$gameVariables.value(395) = arr[1].name

But I get an error.

Previously, I tried using the $gameVariables.value instead of the "let" function, but I couldn't do that either.

If you are using scripts to work with the array and objects, how do you set any of the values to a variable so it can be used?

Thanks!
.value doesn't set, it's a getter. You want setValue:

$gameVariables.setValue(395, arr[1].name);

The reason you're getting an error there is that .value() just returns the value in the provided variable ID, and you can't set a string or number to equal a different string. It's like trying to say cheese = crackers, the interpreter thinks it's nonsense because cheese is just cheese and can never be anything but cheese.
.value doesn't set, it's a getter. You want setValue:

$gameVariables.setValue(395, arr[1].name);

The reason you're getting an error there is that .value() just returns the value in the provided variable ID, and you can't set a string or number to equal a different string. It's like trying to say cheese = crackers, the interpreter thinks it's nonsense because cheese is just cheese and can never be anything but cheese.
@Trihan Thanks! I tried setValue in my experiments, but I had the wrong format.

Now, I will continue to show how little I know.

I was able to use what you suggested, but only if I add it to the same script command as the let arr one.


let arr = [

{ name: "Alex", str: 16, int: 14, dex: 12, con: 17 },

{ name: "Billie", str: 10, int: 17, dex: 17, con: 12 }

];

$gameVariables.setValue(395, arr[1].name);

If I try to use it in a subsequent script command, I get an error saying arr is not defined.

ObjectError.png

I tried changing let to const and it did the same thing.

If someone can tell me, what else am I missing here?

Is there a way to do this same thing without the let function in a script? I tried many times doing this through the Event Command Control Variable -> Script option, but it didn't work.

Again, thanks to everyone's patience in helping me learn!
@Trihan Thanks! I tried setValue in my experiments, but I had the wrong format.

Now, I will continue to show how little I know.

I was able to use what you suggested, but only if I add it to the same script command as the let arr one.


let arr = [

{ name: "Alex", str: 16, int: 14, dex: 12, con: 17 },

{ name: "Billie", str: 10, int: 17, dex: 17, con: 12 }

];

$gameVariables.setValue(395, arr[1].name);

If I try to use it in a subsequent script command, I get an error saying arr is not defined.

View attachment 251262

I tried changing let to const and it did the same thing.

If someone can tell me, what else am I missing here?

Is there a way to do this same thing without the let function in a script? I tried many times doing this through the Event Command Control Variable -> Script option, but it didn't work.

Again, thanks to everyone's patience in helping me learn!
The reason it doesn't work in a subsequent script command is that each script creates its own scope, so any identifiers you create in one don't carry over to another. You need to have everything in one script command, but MZ doesn't have a line limit so that shouldn't be an issue.
The reason it doesn't work in a subsequent script command is that each script creates its own scope, so any identifiers you create in one don't carry over to another. You need to have everything in one script command, but MZ doesn't have a line limit so that shouldn't be an issue.
Thanks for that explanation.

Unfortunately, I was planning on referring to the content of the array and objects at different times.

So it sounds like that won't work when making arrays and objects with scripts.

I was thinking I could make an array of objects that I could refer to continuously throughout the game.

Thanks again!
Thanks for that explanation.

Unfortunately, I was planning on referring to the content of the array and objects at different times.

So it sounds like that won't work when making arrays and objects with scripts.

I was thinking I could make an array of objects that I could refer to continuously throughout the game.

Thanks again!
If that's your goal, I would add the array as a property to $game_system. This has the benefit of automatically being saved in a save game too, since the full object itself is saved and doesn't do any checks on the properties you've added to it.
If that's your goal, I would add the array as a property to $game_system. This has the benefit of automatically being saved in a save game too, since the full object itself is saved and doesn't do any checks on the properties you've added to it.
Thanks again! That is beyond me at this moment. I was just trying to learn how to use objects in arrays because I didn't exactly understand how they worked and I wasn't sure if they would make what I was trying to do easier.

I will add learning how to work with properties to my list of things to explore!

Thanks!
Thanks again! That is beyond me at this moment. I was just trying to learn how to use objects in arrays because I didn't exactly understand how they worked and I wasn't sure if they would make what I was trying to do easier.

I will add learning how to work with properties to my list of things to explore!

Thanks!
It really isn't that complex. You just attach your variable to an object. So where you have

JavaScript:
let arr = [

{ name: "Alex", str: 16, int: 14, dex: 12, con: 17 },

{ name: "Billie", str: 10, int: 17, dex: 17, con: 12 }

];

You'd instead do

JavaScript:
$gameSystem.statArray = 
[

{ name: "Alex", str: 16, int: 14, dex: 12, con: 17 },

{ name: "Billie", str: 10, int: 17, dex: 17, con: 12 }

];

And then whenever you need to refer to it in a later script, you'd use $gameSystem.statArray and it will still have the value it did last time you set it because that object is global and exists throughout the game.
It really isn't that complex. You just attach your variable to an object. So where you have

JavaScript:
let arr = [

{ name: "Alex", str: 16, int: 14, dex: 12, con: 17 },

{ name: "Billie", str: 10, int: 17, dex: 17, con: 12 }

];

You'd instead do

JavaScript:
$gameSystem.statArray =
[

{ name: "Alex", str: 16, int: 14, dex: 12, con: 17 },

{ name: "Billie", str: 10, int: 17, dex: 17, con: 12 }

];

And then whenever you need to refer to it in a later script, you'd use $gameSystem.statArray and it will still have the value it did last time you set it because that object is global and exists throughout the game.
Oh, I will definitely give that a try!

Thanks for taking the extra time to give that additional help!
Oh, I will definitely give that a try!

Thanks for taking the extra time to give that additional help!
As an additional piece of information, there is another way to access properties using bracket notation:

$gameSystem["statArray"] is the same as $gameSystem.statArray.

A corollary to this is that your property names can have spaces in them, but if you decide to use one you will *only* be able to refer to them using bracket notation as dot notation doesn't work with spaces.
As an additional piece of information, there is another way to access properties using bracket notation:

$gameSystem["statArray"] is the same as $gameSystem.statArray.

A corollary to this is that your property names can have spaces in them, but if you decide to use one you will *only* be able to refer to them using bracket notation as dot notation doesn't work with spaces.
@Trihan I experimented with what you were suggesting and I believe I was able to do it and to add a new Object to the Array.

gameSystem.statArray.png
The one thing I am still looking at...it I don't keep getting distracted with other things...is whether you can sort something like this.

So going back to a Turn Order event, each Object in the Array has an actor/enemy name along with a variety of stats, but one of those is an Initiative stat. So being able to sort the Objects by the Initiative stat to see who goes first.

I started looking up Java Script tutorials, but got sidetracked. But at least I successfully tested the part above so that is definitely progress. You helped me understand how to create the Array with Objects, add a new Object, and retrieve data from the Objects.

Thanks for your help!
@Trihan I experimented with what you were suggesting and I believe I was able to do it and to add a new Object to the Array.

View attachment 251302
The one thing I am still looking at...it I don't keep getting distracted with other things...is whether you can sort something like this.

So going back to a Turn Order event, each Object in the Array has an actor/enemy name along with a variety of stats, but one of those is an Initiative stat. So being able to sort the Objects by the Initiative stat to see who goes first.

I started looking up Java Script tutorials, but got sidetracked. But at least I successfully tested the part above so that is definitely progress. You helped me understand how to create the Array with Objects, add a new Object, and retrieve data from the Objects.

Thanks for your help!
$gameSystem.statArray.sort((obj1, obj2) => obj1.init - obj2.init)
$gameSystem.statArray.sort((obj1, obj2) => obj1.init - obj2.init)
Oh, cool! I will give that a try next to see how it works.

Then I might have a 3rd and better way to determine initiative order.

Thanks!

Original thread: https://forums.rpgmakerweb.com/threads/how-to-use-objects-in-an-array-in-mz.154303/