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

Kemezryp

it's me
Veteran
Joined
Oct 25, 2015
Messages
47
Reaction score
9
First Language
Polish
Primarily Uses
RMMV
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...
 

Attachments

  • code.png
    code.png
    13.7 KB · Views: 11
  • console.png
    console.png
    13.3 KB · Views: 11

Andar

Veteran
Veteran
Joined
Mar 5, 2013
Messages
32,341
Reaction score
8,077
First Language
German
Primarily Uses
RMMV
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.
 

Shaz

Global Moderators
Global Mod
Joined
Mar 2, 2012
Messages
41,863
Reaction score
14,514
First Language
English
Primarily Uses
RMMV
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.
 

Kemezryp

it's me
Veteran
Joined
Oct 25, 2015
Messages
47
Reaction score
9
First Language
Polish
Primarily Uses
RMMV
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.
 
Last edited:

Andar

Veteran
Veteran
Joined
Mar 5, 2013
Messages
32,341
Reaction score
8,077
First Language
German
Primarily Uses
RMMV
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.
 

Kemezryp

it's me
Veteran
Joined
Oct 25, 2015
Messages
47
Reaction score
9
First Language
Polish
Primarily Uses
RMMV
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.
 

Another Fen

Veteran
Veteran
Joined
Jan 23, 2013
Messages
604
Reaction score
315
First Language
German
Primarily Uses
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;
 

Kemezryp

it's me
Veteran
Joined
Oct 25, 2015
Messages
47
Reaction score
9
First Language
Polish
Primarily Uses
RMMV
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!
 

Latest Threads

Latest Profile Posts

I am adamant about not having missable quests, which means having to write entirely new scenes at each plot progression that takes into account that time and events have passed since the quest could first be accepted while still having there be a quest to solve in the first place. It's worth every second.
Surprisingly useful and thought provoking
Working on animation. Trying to see if I can apply it meaningfully to my games!
ddwk.png

working on the sprite for Devali <3
My Game Development Livestream

Forum statistics

Threads
110,375
Messages
1,052,783
Members
143,418
Latest member
Kmeep
Top