MV Cannot read property 'code' of undefined, enemy trait "code"

Hii, I've been working on some small code that changes some element if enemy has the "boss collapse" trait. I was doing great until I got stuck at reading "code", which is the id of trait specified in enemy.

So, I attached my code and some prints from my console. You could say that my goal is to make:
Code:
test = code;
where:
Code:
code = 63;

The problem is, using:
Code:
test = $dataEnemies[eId].traits[3].code;
gives me an error specified in the title. And it's very weird, since my console prints test.code as 63.

I'm out of ideas...
your error is not in the code, your problem is "of undefined".
it means that either dataEnemies or that enemies traits are undefined at the moment the code is executed.

so the question is where and under which context is your code executed?
A lot of the data is only loaded after a game is started, if your code is executed before that time it will result in the error despite the datastructures being correct after that time.
Better to show us the whole context of your script instead of just one line. As well as the full error message from the console.

Where are you putting this?

The error indicates that the enemy does not have a traits[3] element. Remember they start at 0, not 1, and you can't guarantee how many they have unless you have gone through and ensured that every enemy has at least 4 traits.
Sorry, I thought it was an error made out of my lack of JS knowledge.
I'm editing Pretty Sleek Gauges plugin: https://forums.rpgmakerweb.com/index.php?threads/pretty-sleek-gauges.90506/
My goals is to make width of enemy boss bar wider if the enemy has a trait of "Collapse Effect: Boss", which is id 63.
You can get entire code from the link if needed, here's my edited stuff:
JavaScript:
Window_EnemyHPBars.prototype.update = function() {
    Window_Base.prototype.update.call(this);
    enemies = $gameTroop.members();

    this.contents.clearRect(0, 0, Graphics.boxWidth, Graphics.boxHeight);
    for (var i = 0; i < enemies.length; i++) {
        if (this._enemySprites[i] === undefined) continue;
        if (!this._enemySprites[i].height || !this._enemySprites[i].width) continue;
        var width = EHPbarWidth, x, y;
        eId = enemies[i]._enemyId;
        var isBoss = false;
        test = $dataEnemies[eId].traits[3];
        //for (var j = 0; j < 4; j++){
        //    if ($dataEnemies[eId].traits[j].code == 63) {
        //        var isBoss = true;
        //        break
        //    }
        //}
        if (isBoss == true) {
            width = 360, x, y;
        }
        /*
        tempId = 16
        if (enemy(tempId).traits.code == 63) {
            console.out('boss');
        } else {
            console.out("bossn't");
        }
        */
        x = enemies[i].screenX() - (width + this.textPadding())/2 + EHPXOffset + (parseInt(enemies[i].enemy().meta.HPBarXOffset) || 0);
        y = enemies[i].screenY() - this.lineHeight() + EHPYOffset + (parseInt(enemies[i].enemy().meta.HPBarYOffset) || 0);
        if (showUpTop) y -= this._enemySprites[i].height * EHPYMultiplier;
        this.drawEnemyGauges(enemies[i], x, y, width);

    }
}
Sorry it's messy, I'm a beginner and it's still work in progress~

EDIT: whoops, forgot to add
The error indicates that the enemy does not have a traits[3] element. Remember they start at 0, not 1, and you can't guarantee how many they have unless you have gone through and ensured that every enemy has at least 4 traits.
It's a 4th trait. That's why it works well in the console, I guess.

@Andar I believe your point might be the case. I don't have any idea how to fix that yet, I'll think about it but any help is welcome!

EDIT2: I just tried doing console.log(test); in the code. Andar was right, the traits aren't defined at start. I guess I need to figure out how to force them to load now... OR I'll check another plugins' code for hints.
It's a 4th trait.
but has every enemy four traits? the error will also happen as soon as it checks an enemy that has less than four traits, that is what Shaz is concerned about.
but has every enemy four traits? the error will also happen as soon as it checks an enemy that has less than four traits, that is what Shaz is concerned about.
Yes, I set is specifically at 4 because it's 4th out of 4 traits. If I managed to make the "code" work, I'd put it into for loop that checks if there's any 63 in i.
That said, it's not an issue. I've done some testing and now I'm sure it's because the code checks for traits before they are defined. See my 2nd edit above.
Hard to tell from this position, my first guess would be that maybe you left an "empty" enemy in your troop?

The database categories are normally completely loaded during Scene_Boot before entering the title screen. Unless you are modifying your database entries later during runtime it should be fully available in your window.


Not quite on topic, but there are consolidating functions for every trait if you want to use them:
For example, enemies[i].collapseType() would give you an enemies collapse type from its traits (not sure if relevant, the definitions are in "rpg_objects.js" around line ~2440 - 2580, the code might be a bit tricky though)

var width = EHPbarWidth, x, y;
is just short for
var width = EHPbarWidth;
var x;
var y;
Hard to tell from this position, my first guess would be that maybe you left an "empty" enemy in your troop?
nah, all the numbers in the code are adjusted to that one fight for testing purposes (2 enemies of ids of 2 and 16 in that order, 16 is the boss), I've been doing a lot of stuff using enemy and troop ids, so I am pretty sure I left nothing behind.
The database categories are normally completely loaded during Scene_Boot before entering the title screen. Unless you are modifying your database entries later during runtime it should be fully available in your window.
Sounds helpful, I'll look around the code here later!

Not quite on topic, but there are consolidating functions for every trait if you want to use them:
For example, enemies.collapseType() would give you an enemies collapse type from its traits (not sure if relevant, the definitions are in "rpg_objects.js" around line ~2440 - 2580, the code might be a bit tricky though)
AAAAAAAAAAA You're a lifesaver!! This info is in the code the entire time and I can use it for checking if enemy is a boss! Thank Youuuu, my code works perfectly now!

Original thread: https://forums.rpgmakerweb.com/threads/cannot-read-property-code-of-undefined-enemy-trait-code.133804/