MZ Problems with inheriting damage elements

Hi folks,

I am having some trouble with inherited attack elements from the weapon or the enemy parameters.

I have set things up in VisuStella Core to change the damage color based on the damage element.

To get the element, I am using:
JavaScript:
BattleManager._action.item().damage.elementId
Which, as far as I know, should get the element of the damage being dealt (not the damage listed on the item/weapon/skill/etc.).

I have an enemy set up like this:
enemy.png


And, they are using a skill set up like this:
skill.png


But, when they attack with that skill, even though it is set to "normal attack" element, it deals non-elemental damage, despite the enemy having the "Light" element parameter. I tried changing this to "None" element, and "Normal" element (element 1) and it wouldn't inherit the enemy element in any case.

Similarly, I have a weapon set up like this:
wespon.png


And, when attacking with that weapon using a skill set up the same as above, it always deals normal damage if the skill has either normal, none or normal damage.

Is there something I am missing here? If I set the element in the skill itself, it works fine and the damage colors change to reflect the element (so I know my script for that is working), but I can never get any non-elemental skill to inherit a weapon or enemy element type.

I've done plenty of googling and it seems like this should be working, as far as I can tell.

Any help is much appreciated!
JavaScript:
BattleManager._action.item().damage.elementId
Which, as far as I know, should get the element of the damage being dealt (not the damage listed on the item/weapon/skill/etc.).
Nope! That gives you the damage type listed on the item or skill. It does not check any sorts of traits, and in particular definitely doesn't check your weapon. And in fact, it'll sometimes return an invalid element – this is for skills or items that have their element set to "Normal Attack", which is a special code that makes it collect Attack Element traits from everywhere to determine the element.

Of course, if that code returns a number greater than 0, then it is, in fact, the real element that applies for that skill or item. I don't think there's any way to get the element in the other case though, because by default it accounts for possibly multiple elements, so there isn't just a single element to pick out in the first place (unless VisuStella changes that somehow?).

You could probably get sort of close with something like this:
JavaScript:
a.traitsSet(Game_BattlerBase.TRAIT_ATTACK_ELEMENT)[0] || 0
I'm not sure where you're using this code, so you might need to replace a with b or something else.

That just grabs all the Attack Element traits and picks the first one, which isn't much different from picking a random one. There's probably a way to get even closer, but I'd have to look into it in more detail.

it deals non-elemental damage
I think there's a pretty good chance that you're misinterpreting something here? The code you used returns either 0 or -1 (I don't remember which) if the skill's element is set to "Normal Attack". Of course, there is no element 0 or -1, so whatever you're using to look up a colour for that element won't find anything.
Nope! That gives you the damage type listed on the item or skill. It does not check any sorts of traits, and in particular definitely doesn't check your weapon. And in fact, it'll sometimes return an invalid element – this is for skills or items that have their element set to "Normal Attack", which is a special code that makes it collect Attack Element traits from everywhere to determine the element.

Of course, if that code returns a number greater than 0, then it is, in fact, the real element that applies for that skill or item. I don't think there's any way to get the element in the other case though, because by default it accounts for possibly multiple elements, so there isn't just a single element to pick out in the first place (unless VisuStella changes that somehow?).

You could probably get sort of close with something like this:
JavaScript:
a.traitsSet(Game_BattlerBase.TRAIT_ATTACK_ELEMENT)[0] || 0
I'm not sure where you're using this code, so you might need to replace a with b or something else.

That just grabs all the Attack Element traits and picks the first one, which isn't much different from picking a random one. There's probably a way to get even closer, but I'd have to look into it in more detail.


I think there's a pretty good chance that you're misinterpreting something here? The code you used returns either 0 or -1 (I don't remember which) if the skill's element is set to "Normal Attack". Of course, there is no element 0 or -1, so whatever you're using to look up a colour for that element won't find anything.
Ahh, wow, I am learning a lot right now, haha.

As far as the code and where I'm using it goes: it is the color settings for Visu Stella Core, which has a "Damage Colors" parameters where you can enter JS to change the color of the damage numbers. By default, it has some JS set up to change it based on HP/MP damage or healing and a default color. I modified it (in a very amateur way) to also change the color further based on the element if it is HP damage.

This is the code I am using for that:

JavaScript:
// Set the variables used in this function.
let colorType = arguments[0];
let elemType = BattleManager._action.item().damage.elementId;

// Check the value of the color type
// and return an appropriate color.
switch (colorType) {

    case 0: 
// HP damage
        if (elemType === 1){
            return "#FFFFFF";
        } else if (elemType === 2){
            return "#5D4CA6";
        } else if (elemType === 3){
            return "#4DF0EB";
        } else if (elemType === 4){
            return "#ED521F";
        } else if (elemType === 5){
            return "#E4E825";
        } else if (elemType === 6){
            return "#B719E0";
        } else {
            return "#FFFFFF";
        }

    case 1:
// HP recover
        return "#b9ffb5";

    case 2:
// MP damage
        return "#bb88bb";

    case 3:
// MP recover
        return "#80b0ff";

    default:    
        return "#FFFFFF";

}

The idea is, if the damage type is HP damage (which the plugin sets as argument 0), then change the color further based on the element of the damage.

I am not 100% sure how I would use the script you mentioned here though, as this script determines the color to change any damage number to (both player and enemy damage), so using a. or b. for instance is tricky.

I don't mind only getting the first element, as my game only ever has one element (either the skill has an element set, or the enemy has an element, or a weapon does, but there aren't any cases where an enemy has 2 elements set, or the battler has two pieces of equipment that add elements, as only weapons do, and they only add 1).
Alright, so then you can add the code I gave you before right at the top:
JavaScript:
let colorType = arguments[0];
let elemType = BattleManager._action.item().damage.elementId;
if (elemType < 0) {
        elemType = BattleManager._subject.traitsSet(Game_BattlerBase.TRAIT_ATTACK_ELEMENT)[0] || 0;
}

Note: BattleManager._subject is just a guess. It definitely exists, but I'm not sure if it's the correct actor/enemy at the time this code runs. I think it's very likely that VisuStella exposes what you need as an official parameter, but I don't know what that would be.
Alright, so then you can add the code I gave you before right at the top:
JavaScript:
let colorType = arguments[0];
let elemType = BattleManager._action.item().damage.elementId;
if (elemType < 0) {
        elemType = BattleManager._subject.traitsSet(Game_BattlerBase.TRAIT_ATTACK_ELEMENT)[0] || 0;
}

Note: BattleManager._subject is just a guess. It definitely exists, but I'm not sure if it's the correct actor/enemy at the time this code runs. I think it's very likely that VisuStella exposes what you need as an official parameter, but I don't know what that would be.
Thanks so much, that worked perfectly!
Alright, so then you can add the code I gave you before right at the top:
JavaScript:
let colorType = arguments[0];
let elemType = BattleManager._action.item().damage.elementId;
if (elemType < 0) {
        elemType = BattleManager._subject.traitsSet(Game_BattlerBase.TRAIT_ATTACK_ELEMENT)[0] || 0;
}

Note: BattleManager._subject is just a guess. It definitely exists, but I'm not sure if it's the correct actor/enemy at the time this code runs. I think it's very likely that VisuStella exposes what you need as an official parameter, but I don't know what that would be.
Hmm, my players have been getting a crash repeatedly after adding this code. There seems to be something happening where the traitSet isn't always available. Oddly enough, I am having trouble replicating it, so I am not sure exactly what situation causes it.

Is there some way to add a condition to only get this traitSet data if its available so that it doesn;t crash if the BattleManager._subject is null?

This is the error message they keep sending:
Crash.jpg
I think it should work if you change _subject.traitsSet to _subject?.traitsSet (ie, just add a ?). That should make the crash case fall back to the final (white) case.
I think it should work if you change _subject.traitsSet to _subject.?traitsSet (ie, just add a ?). That should make the crash case fall back to the final (white) case.
Unfortunately, this just gives the error:

SyntaxError
Unexpected token '?'

The full script is currently:

Spoiler: CODE
JavaScript:
// Set the variables used in this function.
let colorType = arguments[0];
let elemType = BattleManager._action.item().damage.elementId;
if (elemType < 0) {
        elemType = BattleManager._subject.?traitsSet(Game_BattlerBase.TRAIT_ATTACK_ELEMENT)[0] || 0;
}

// Check the value of the color type
// and return an appropriate color.
switch (colorType) {

    case 0:
// HP damage
        if (elemType === 1){
        //Normal
            return "#FFFFFF";
        } else if (elemType === 2){
        //Curse
            return "#625FB0";
        } else if (elemType === 3){
        //Insight
            return "#4DF0EB";
        } else if (elemType === 4){
        //Insanity
            return "#ED521F";
        } else if (elemType === 5){
        //Light
            return "#FAFF3B";
        } else if (elemType === 6){
        //Darkness
            return "#B719E0";
        //Default
        } else {
            return "#FFFFFF";
        }

    case 1:
// HP recover
        return "#b9ffb5";

    case 2:
// MP damage
        return "#bb88bb";

    case 3:
// MP recover
        return "#80b0ff";

    default:   
        return "#FFFFFF";

}
Sorry about that, I misplaced the ? – it goes before the period, not after.
Sorry about that, I misplaced the ? – it goes before the period, not after.
Thanks! That fixed the crash.

I will give this a go and see if that crash stops popping up.

Original thread: https://forums.rpgmakerweb.com/threads/problems-with-inheriting-damage-elements.181198/