Hi folks,
I am a total JS novice, and have been writing some code to display some weapon/armor traits as strings using JS eval text codes from a plugin.
If it has a trait in the slot (traitSlot) my code is checking, it outputs it correctly, but if the item has only 4 traits set up in the database, it will (understandably) crash the game when it checks trait 5, with an 'undefined' message.
I tried doing some googling to see how I would check if it is undefined before checking that trait code, but nothing I have tried is working so far.
Here is my (very amateur, but mostly functional) code (for trait 0, and then this is repeated in other evals with "traitSlot" increased up to 9):
For this, I run a copy of it 10 times (with the variable 'traitSlot" increased each time) so I can place each one independently in my crafting description layout, but it works fine if I have all 10 traits that I'm checking set for that item, or if I don't run this code for any missing trait slots, however I want one generic description for all items, so I'd like it to show "up to" traits 0-9.
Currently, this crashes when it gets to dataItem.traits[traitSlot].code for a traitSlot it doesn't have, as "code" is undefined. For example, if the item has 4 traits, dataItem.traits[4].code will crash it (since that would be the 5th trait, which is undefined).
Is there some way to check if the trait code is defined before it sets the var? I tried some basic if statement comparisons, but it doesn;t seem to be that simple (checking if it is undefined causes the same crash, since it is still checking it).
Or alternatively, a way to get the number of traits an item has so I can only run all of this if const traitSlot is =< the number of traits the item has?
Thanks!
I am a total JS novice, and have been writing some code to display some weapon/armor traits as strings using JS eval text codes from a plugin.
If it has a trait in the slot (traitSlot) my code is checking, it outputs it correctly, but if the item has only 4 traits set up in the database, it will (understandably) crash the game when it checks trait 5, with an 'undefined' message.
I tried doing some googling to see how I would check if it is undefined before checking that trait code, but nothing I have tried is working so far.
Here is my (very amateur, but mostly functional) code (for trait 0, and then this is repeated in other evals with "traitSlot" increased up to 9):
JavaScript:
const recipe = $cgmzTemp.getRecipeTempData(SceneManager._scene._displayWindow._recipe._name);
const product = recipe.products[0];
const dataItem = (product.Type === 'weapon') ? $dataWeapons[product.ID] : $dataArmors[product.ID];
const traitSlot = 0
const traitCode = dataItem.traits[traitSlot].code;
//ex params
if (traitCode === 22) {
const paramValue = (Math.round(dataItem.traits[traitSlot].value * 100));
//Hit Rate
if (paramID === 0) {
if (paramValue > 0) {
return `\\c[6]Hit Rate \\c[3]+${paramValue}%\\c[0]`;
} else if (paramValue < 0) {
return `\\c[6]Hit Rate \\c[18]${paramValue}%\\c[0]`;
}
//Evasion
} else if (paramID === 1) {
if (paramValue > 0) {
return `\\c[6]Evasion \\c[3]+${paramValue}%\\c[0]`;
} else if (paramValue < 0) {
return `\\c[6]Evasion \\c[18]${paramValue}%\\c[0]`;
}
//Crit Rate
} else if (paramID === 2) {
if (paramValue > 0) {
return `\\c[6]Crit Rate \\c[3]+${paramValue}%\\c[0]`;
} else if (paramValue < 0) {
return `\\c[6]Crit Rate \\c[18]${paramValue}%\\c[0]`;
}
//M. Reflect
} else if (paramID === 5) {
if (paramValue > 0) {
return `\\c[6]Magic Reflect \\c[3]+${paramValue}%\\c[0]`;
} else if (paramValue < 0) {
return `\\c[6]Magic Reflect \\c[18]${paramValue}%\\c[0]`;
};
//Counter
} else if (paramID === 6) {
if (paramValue > 0) {
return `\\c[6]Counter Attack \\c[3]+${paramValue}%\\c[0]`;
} else if (paramValue < 0) {
return `\\c[6]Counter Attack \\c[18]${paramValue}%\\c[0]`;
}
}
//sp params
} else if (traitCode === 23) {
const paramValue = (Math.round(dataItem.traits[traitSlot].value * 10));
//Recovery Effect
if (paramID === 2) {
if (paramValue > 0) {
return `\\c[6]Recovery Effect \\c[3]+${paramValue}%\\c[0]`;
} else if (paramValue < 0) {
return `\\c[6]Recovery Effect \\c[18]${paramValue}%\\c[0]`;
}
//Food Effect
} else if (paramID === 3) {
if (paramValue > 0) {
return `\\c[6]Food Effect \\c[3]+${paramValue}%\\c[0]`;
} else if (paramValue < 0) {
return `\\c[6]Food Effect \\c[18]${paramValue}%\\c[0]`;
}
//MP Cost
} else if (paramID === 4) {
if (paramValue > 0) {
return `\\c[6]MP Cost \\c[3]+${paramValue}%\\c[0]`;
} else if (paramValue < 0) {
return `\\c[6]MP Cost \\c[18]${paramValue}%\\c[0]`;
}
//Physical Damage
} else if (paramID === 6) {
if (paramValue > 0) {
return `\\c[6]Physical Damage \\c[3]+${paramValue}%\\c[0]`;
} else if (paramValue < 0) {
return `\\c[6]Physical Damage \\c[18]${paramValue}%\\c[0]`;
}
//Magical Damage
} else if (paramID === 7) {
if (paramValue > 0) {
return `\\c[6]Magical Damage \\c[3]+${paramValue}%\\c[0]`;
} else if (paramValue < 0) {
return `\\c[6]Magical Damage \\c[18]${paramValue}%\\c[0]`;
}
//EXP Rate
} else if (paramID === 3) {
if (paramValue > 0) {
return `\\c[6]EXP Rate \\c[3]+${paramValuee}%\\c[0]`;
} else if (paramValue < 0) {
return `\\c[6]EXP Rate \\c[18]${paramValue}%\\c[0]`;
}
}
//Elem Rates
} else if (traitCode === 11) {
const paramValue = (Math.round(dataItem.traits[traitSlot].value * 10));
//Recovery Effect
if (paramID === 1) {
if (paramValue > 0) {
return `\\c[6]Normal Resistance \\c[3]+${paramValue}%\\c[0]`;
} else if (paramValue < 0) {
return `\\c[6]Normal Resistance \\c[18]${paramValue}%\\c[0]`;
}
//Food Effect
} else if (paramID === 2) {
if (paramValue > 0) {
return `\\c[6]Curse Resistance \\c[3]+${paramValue}%\\c[0]`;
} else if (paramValue < 0) {
return `\\c[6]Curse Resistance \\c[18]${paramValue}%\\c[0]`;
}
//MP Cost
} else if (paramID === 3) {
if (paramValue > 0) {
return `\\c[6]Insight Resistance \\c[3]+${paramValue}%\\c[0]`;
} else if (paramValue < 0) {
return `\\c[6]Insight Resistance \\c[18]${paramValue}%\\c[0]`;
}
//Physical Damage
} else if (paramID === 4) {
if (paramValue > 0) {
return `\\c[6]Insanity Resistance \\c[3]+${paramValue}%\\c[0]`;
} else if (paramValue < 0) {
return `\\c[6]Insanity Resistance \\c[18]${paramValue}%\\c[0]`;
}
//Magical Damage
} else if (paramID === 5) {
if (paramValue > 0) {
return `\\c[6]Light Resistance \\c[3]+${paramValue}%\\c[0]`;
} else if (paramValue < 0) {
return `\\c[6]Light Resistance \\c[18]${paramValue}%\\c[0]`;
}
//EXP Rate
} else if (paramID === 6) {
if (paramValue > 0) {
return `\\c[6]Dark Resistance \\c[3]+${paramValuee}%\\c[0]`;
} else if (paramValue < 0) {
return `\\c[6]Dark Resistance \\c[18]${paramValue}%\\c[0]`;
}
}
}
};
For this, I run a copy of it 10 times (with the variable 'traitSlot" increased each time) so I can place each one independently in my crafting description layout, but it works fine if I have all 10 traits that I'm checking set for that item, or if I don't run this code for any missing trait slots, however I want one generic description for all items, so I'd like it to show "up to" traits 0-9.
Currently, this crashes when it gets to dataItem.traits[traitSlot].code for a traitSlot it doesn't have, as "code" is undefined. For example, if the item has 4 traits, dataItem.traits[4].code will crash it (since that would be the 5th trait, which is undefined).
Is there some way to check if the trait code is defined before it sets the var? I tried some basic if statement comparisons, but it doesn;t seem to be that simple (checking if it is undefined causes the same crash, since it is still checking it).
Or alternatively, a way to get the number of traits an item has so I can only run all of this if const traitSlot is =< the number of traits the item has?
Thanks!