MV Item conserve passive

Hi, I need help with the script for this passive skill:

Pharmacology
-> 50% chance to not consume potions when used.


im using Yanfly Plugins, with this script:


State Notetag:


<Custom Action Start Effect>

// Add the ID's of the potions that can be affected by pharmacology.

potions.push(1, 2, 3, 4);

potions.push(5, 6, 7, 8);

// Loop through each of the potion ID's.

for (var i = 0; i < potions.length; ++i) {

// Get the current ID.

var id = potions;

// If the party has this potion in its item bag...

if ($gameParty.numItems($dataItems[id]) > 0) {

// ...then set the value to true.

value = true;

// ...and break the loop.

break;

}

}

// Get current action.

var action = user.currentAction();

// Check if the current action is using item.

if (action && action.isItem()) {

var item = $dataItems[id];

// Chance for pharmacology to activate.

var conserveRate = 0.5;

// Check if conservation succeed.

if (Math.random() < conserveRate) {

$gameParty.getItem(item, 1);

}

}

</Custom Action Start Effect>


It didnt work...
Can someone help me where i did wrong?
I am still learning, pls enlighten me >.<
// Add the ID's of the potions that can be affected by pharmacology.

potions.push(1, 2, 3, 4);
you didnt define the potions array, so trying to use push() should have given an error in the console and would have invalidated the rest of the notetag immediately. also, if its only going to contain those 8 values, you could have defined as an array with the values in it from the start (or just check if the id of the item being used is less than 9 instead of using an array as in this case as theyre all the integers between 0 and 9).

// ...then set the value to true.

value = true;
the "value" doesnt do anything here. the notetag doesnt have a built in value variable so youre just making a global variable and not doing anything with it either.

$gameParty.getItem(item, 1);
Game_Party.getItem isnt a function either and yould result in an error.


you shouldnt need the loop at all nor to check if the player has one of the items already as the action has the id of the item being used and using the item requires having had the item in the first place. youd just need to check if the action is an item with one of your specified ids and run the random roll to give the party one back.
JavaScript:
Game_Battler.prototype.consumeItem = function(item) {
    if (!this.isStateAffected(id) || Math.random() > 0.5 {
        $gameParty.consumeItem(item);
    }
};
Or, you can save this piece of code as a .js file and install it as a plugin. replace 'id' with the state number of your pharmacology state
Hi, I need help with the script for this passive skill:

Pharmacology
-> 50% chance to not consume potions when used.


im using Yanfly Plugins, with this script:


State Notetag:


<Custom Action Start Effect>

// Add the ID's of the potions that can be affected by pharmacology.

potions.push(1, 2, 3, 4);

potions.push(5, 6, 7, 8);

// Loop through each of the potion ID's.

for (var i = 0; i < potions.length; ++i) {

// Get the current ID.

var id = potions;

// If the party has this potion in its item bag...

if ($gameParty.numItems($dataItems[id]) > 0) {

// ...then set the value to true.

value = true;

// ...and break the loop.

break;

}

}

// Get current action.

var action = user.currentAction();

// Check if the current action is using item.

if (action && action.isItem()) {

var item = $dataItems[id];

// Chance for pharmacology to activate.

var conserveRate = 0.5;

// Check if conservation succeed.

if (Math.random() < conserveRate) {

$gameParty.getItem(item, 1);

}

}


</Custom Action Start Effect>


It didnt work...
Can someone help me where i did wrong?
I am still learning, pls enlighten me >.<
Just setup a common event that runs when they use the item only the common event runs. It heals and plays the animation exactly, except you will set up a variable that runs only on a 1 and it will random number from 1 - 2. So if it random numbers up 1, the Conditional branch you setup in the common event will remove 1 of the item, and if a 2, then it won't remove the item at all.
JavaScript:
Game_Battler.prototype.consumeItem = function(item) {
    if (!this.isStateAffected(id) || Math.random() > 0.5 {
        $gameParty.consumeItem(item);
    }
};
Or, you can save this piece of code as a .js file and install it as a plugin. replace 'id' with the state number of your pharmacology state
I copied this, changed the id into proper state id, turns it into js file, installed it, but still not working. I wonder what i did wrong. Do i have to put any script on the state?

Just setup a common event that runs when they use the item only the common event runs. It heals and plays the animation exactly, except you will set up a variable that runs only on a 1 and it will random number from 1 - 2. So if it random numbers up 1, the Conditional branch you setup in the common event will remove 1 of the item, and if a 2, then it won't remove the item at all.
Could you guide me with this please?
you didnt define the potions array, so trying to use push() should have given an error in the console and would have invalidated the rest of the notetag immediately. also, if its only going to contain those 8 values, you could have defined as an array with the values in it from the start (or just check if the id of the item being used is less than 9 instead of using an array as in this case as theyre all the integers between 0 and 9).


the "value" doesnt do anything here. the notetag doesnt have a built in value variable so youre just making a global variable and not doing anything with it either.


Game_Party.getItem isnt a function either and yould result in an error.


you shouldnt need the loop at all nor to check if the player has one of the items already as the action has the id of the item being used and using the item requires having had the item in the first place. youd just need to check if the action is an item with one of your specified ids and run the random roll to give the party one back.
Frankly, i just copy pasted yanfly's tips and tricks script, rearrange it to see if it works. lol.

Is it easier if i put the script to items instead? something like: if player use the item and is stateaffected with pharmacology, rolls a number, if true, items is received, if not it is consumed as it supposed to.

what script should i put in the item notetag?
Isn't there just a missing parenthesis ) in the if condition, after 0.5 ?
I copied this, changed the id into proper state id, turns it into js file, installed it, but still not working. I wonder what i did wrong. Do i have to put any script on the state?
theres a minor typo there
Code:
Game_Battler.prototype.consumeItem = function(item) {
    if (!this.isStateAffected(id) || Math.random() > 0.5) {
        $gameParty.consumeItem(item);
    }
};
edit: ninja'd 🤣

What Gensun gave after the correction should work, but to answer it...
Is it easier if i put the script to items instead? something like: if player use the item and is stateaffected with pharmacology, rolls a number, if true, items is received, if not it is consumed as it supposed to.

what script should i put in the item notetag?
that'd also work fine. I cant remember if <custom execution> from the skill core works on items offhand, but assuming it doesnt you could do something like with the skill core:

Code:
<After Eval>
if (user.isStateAffected(x) && Math.random() < .5 && !this.refund) {
  $gameParty.gainItem($dataItems[item.id], 1);
  this.refund = true;
}
</After Eval>
replace the x for the id of the state and it should give a 50% chance to refund the player 1 of the item when used
Oh wow, it works. Thank you so much for the help guys, I really appreciate it.

:LZScheeze::LZScheeze::LZScheeze::LZScheeze::LZScheeze::LZScheeze::LZScheeze::LZScheeze::LZScheeze::LZScheeze::LZScheeze::LZScheeze::LZScheeze:

I might need another of your help in the future tho.

Original thread: https://forums.rpgmakerweb.com/threads/item-conserve-passive.181107/