MZ Property undefined?

So I can't seem to get any kind of useful answer ANYWHERE about this, so I'm hoping someone here will have an answer.

To start off, I'm trying to set up array variables, which I will then populate. But when I try to set/read the value, I keep getting told cannot set/read property of undefined when I actually try to run the script.

I know that a variable is undefined if you try to use it without setting a value of any kind. The problem is that the script seems to be throwing a fit about undefined variables.... WHILE I'M TRYING TO DEFINE THEM!

For example, I'll declare a variable array in the global space of a plugin, setting it to [] when I do. But then when attempting to set a value in the array with a function, either with a push statement or simply writing example[0] = x, I get told it can't set property of undefined.

I've tried just about every variation I can think of to get it to work, but no dice. Anyone have a clue what I'm doing wrong?
1) show us the code that you're using for the definition, including where and when you're using it.
2) what code are you using to access the variable? if it is inside a plugin you usually need to reference the plugin itself, that is why a lot of plugins no longer work if their filename is changed
JavaScript:
Drak.ERec.Record = []; //this is in the global space

Drak.ERec.Initialize = function() { //where I'm setting the values
    for (i = 0; i < $dataTroops.length; i++) {
        Drak.ERec.Record[i].enemy = [];

        for (j = 0; j < $dataTroops[i].members().length; j++) {
            Drak.ERec.Record[i].enemy.push($dataTroops[i].members()[j].enemyId);
        };
    };
};

This is the one I'm currently working on, though even a simpler version where the array elements are simply numbers and not other arrays that I eventually abandoned in another project has the same problem. This was written as a test just to see if that would work before making it more complex, since eventually there will be another layer in each array.
So you've defined Drak.ERec.Record, but where are you defining the ERec property of Drak, or the Drak object itself?

Also I think you've slightly misunderstood how declaring properties works. If you define Drak.ERec.Record as an empty array, you can absolutely set Drak.ERec.Record at index i to a value, but to add a .enemy property to it you would first have to declare the index as an object.
Assuming you've posted the complete code and not just a snippet of it, the problem is likely here:
Code:
Drak.ERec.Record = [];

This will crash because "Drak" is undefined, and you're trying to access the property "ERec" of undefined. This also applies to "ERec" and "Record". The correct way to set up this variable would be either:
Code:
Drak = {};
Drak.ERec = {};
Drak.ERec.Record = [];

Or much more concise:
Code:
Drak = {
    ERec: {
        Record: []
    }
};
So you've defined Drak.ERec.Record, but where are you defining the ERec property of Drak, or the Drak object itself?
JavaScript:
var Drak = Drak || {};
Drak.ERec = Drak.ERec || {};
that sits above, with the rest of the standard plugin declarations.
Okay, what I said about having to declare the index reference as an object before you can add a .enemy property to it stands. 🙂
Okay, what I said about having to declare the index reference as an object before you can add a .enemy property to it stands. 🙂

By that, Trihan means the following line:
Code:
Drak.ERec.Record[i].enemy = [];
At this point, "Record" is empty, but you're trying to access the i-th element of the empty array. That i-th element does not exist (it's undefined).

Possible ways to fix this:
Code:
Drak.ERec.Record[i] = {};
Drak.ERec.Record[i].enemy = [];

or
Code:
Drak.ERec.Record[i] = { enemy: [] };

or
Code:
Drak.ERec.Record.push({ enemy: [] });
So I essentially have to set up all the objects for the array in reverse to get it all working right? Because inside the enemy object is going to be OTHER arrays, setting various values for each enemy in each troop.
So I essentially have to set up all the objects for the array in reverse to get it all working right? Because inside the enemy object is going to be OTHER arrays, setting various values for each enemy in each troop.
You don't have to change the order, but you do have to set
Code:
Record[i]
to something before you can access the "enemy" property.

That's because
Code:
Record = []
creates an empty array, and
Code:
Record[i]
is going to be undefined unless you set it to something first, like
Code:
Record[i] = {}
I think I get it. Javascript continues to defy my expectations regarding its use. The more I think I'm starting to figure it, out the more curveballs it throws at me. But the more I do it, the more I'll figure it out. Thank you guys so much! This thread can be closed!
When you want a thread closed, please report it. Don't assume the moderators are going to see a comment.

For this issue, it would also help if you provide the exact error message that you're getting - a screenshot of the console tab from the developer tools window (hit F8 when you get the error message) is ideal. So if you run into trouble again, post that along with your actual code in the very first post, and there's a good chance you'll get the solution more quickly.

Thread closed

Original thread: https://forums.rpgmakerweb.com/threads/property-undefined.134422/