MZ Can someone please explain $dataItems.effects[0] and how to add/remove effects of an item?

Hello,
Ive been trying for several days to figure out how to make potions work in my rpg. I have it set up to give an effect on the creation of the potion to an extent. I am currently replacing the effects with different numbers but this causes weird ascetic issues. I have the code:

```
<On Creation Eval>
if ($gameVariables.value(18) != 0) {
item.effects[0].value1 = 5;
item.effects[0].value2 = $gameVariables.value(18);
} else {
item.effects[0].value1 = 0;
item.effects[0].value2 = 0;
};
if ($gameVariables.value(19) != 0) {
item.effects[1].value1 = 5;
item.effects[1].value2 = $gameVariables.value(19);
} else {
item.effects[0].value1 = 0;
item.effects[0].value2 = 0;
};
if ($gameVariables.value(20) != 0) {
item.effects[2].value1 = 5;
item.effects[2].value2 = $gameVariables.value(20);

} else {
item.effects[2].value1 = 0;
item.effects[2].value2 = 0;
};

</On Creation Eval>```

I feel like there has to be a better way to do this. Any help would be appreciated. This kindve works but it applies buffs that last forever with 0 turns remaining.
You can't.

$data- arrays are the database entries. They're read from your JSON files every time you run your game, and they apply to all objects in your game created from those entries.

They are not for runtime editing.

If you're trying to create potions that have randomized effects, MZ doesn't have any version of independent items - that means all items of a given database ID are the same and can't be individually edited.

So you'll have to actually create in your Items section of the database the possible results and randomly determine which of those discrete items the player gets from crafting.
That looks like a plugin notetag...YEP Item Core? If you're using independent items then it should be OK.

Each Effect has properties:
  • code - determines the effect type, e.g. 11 for Recover HP.
  • Secondary values like dataId, value1, value2 - these determine how the effect behaves.
See the "Feature_Effect Code" sheet of the document linked here for details:
If you're using independent items then it should be OK.
I didn't think the independent items worked correctly with MZ, even with FOSSIL. Am I wrong?
I didn't think the independent items worked correctly with MZ, even with FOSSIL. Am I wrong?
I haven't tested it, but it supposedly works with FOSSIL. Alternatively, there's also this patch made by ramza. There's also a standalone I independent items plugin by Dungeonmind.
I haven't tested it, but it supposedly works with FOSSIL. Alternatively, there's also this patch made by ramza. There's also a standalone I independent items plugin by Dungeonmind.
Okay, then! Disregard me and do what @caethyril said.
yeah, im using Yamzas plugin, ill try this. Thanks for yalls help.
Good luck! 👋

I've moved this thread to Plugin Support (from MZ Support). Thank you.

JavaScript:
if ($gameVariables.value(18) > 0) {
     item.effects[0].code = 31;
     item.effects[0].value1 = 5;
     item.effects[0].value2 = $gameVariables.value(18);
  } else if ($gameVariables.value(18) < 0) {
     item.effects[0].code = 32;
     item.effects[0].value1 = 5;
     item.effects[0].value2 = $gameVariables.value(18);
  } else {
     item.effects[0].value1 = 0;
     item.effects[0].value2 = 0;
};
  if ($gameVariables.value(19) > 0) {
     item.effects[0].code = 31;
     item.effects[0].value1 = 5;
     item.effects[0].value2 = $gameVariables.value(19);
  } else if ($gameVariables.value(19) < 0) {
      item.effects[0].code = 32;
      item.effects[0].value1 = 5;
      item.effects[0].value2 = $gameVariables.value(19);
  } else {
     item.effects[0].value1 = 0;
     item.effects[0].value2 = 0;
};
  if ($gameVariables.value(20) > 0) {
     item.effects[0].code = 31;
     item.effects[0].value1 = 5;
     item.effects[0].value2 = $gameVariables.value(20);
} else if ($gameVariables.value(20) < 0) {
      item.effects[0].code = 32;
      item.effects[0].value1 = 5;
      item.effects[0].value2 = $gameVariables.value(20);
} else {
     item.effects[0].value1 = 0;
     item.effects[0].value2 = 0;
};

I did it, since I cant add effects directly I used 3 dummy effects that add 0 hp for 0 turns and then just overwrote them with buffs or debuffs when needed.
Just as a tip, you can save yourself some typing (and repetition of code lines) with ternary statements.

They use the format condition ? true statement : false statement
and can be chained together to achieve if ? true : else if ? that's true : else

So you could do something like:
Code:
let refValue = $gameVariables.value(18);
item.effects[0].code = refValue > 0 ? 31 : refValue < 0 ? 32 : 0;
item.effects[0].value1 = refValue ? 5 : 0;
item.effects[0].value2 = refValue ? refValue : 0;

refValue = $gameVariables.value(19);
etc.

and your code will be about one third its size.

Then, because you keep doing the same operations with the same item properties looking at different variables, you could condense all of it into one chunk by putting those variables into an array and using some of JavaScript's array functions, but that's more advanced.

Good luck!

Original thread: https://forums.rpgmakerweb.com/threads/can-someone-please-explain-dataitems-effects-0-and-how-to-add-remove-effects-of-an-item.164614/