Doing Math in an Event

Sorry if this is in the wrong forum topic, I didn't see one that perfectly fit my query.

In a scenario where the player can exchange collected resources for an item, how do I do the math to determine how many of the item to give the player?

Specifically, the player can exchange 10 'shrubs' for a healing 'Balm' (potion).

Currently, the player needs to interact with the herbalist for each Balm, meaning if you have 44 shrubs, you'll need to click through the herbalist dialog 4 times. Instead, I'd like the event to do the math based on the number of shrubs and give the player 4 Balms in one shot. In psuedo-code:

> Create variable based on number of shrubs (this is easy, we'll call it Num_Shrubs)

> Divide Num_Shrubs by 10, creating a new variable with the result (call it Num_Balms)

> Give player a number of Balms equal to Num_Balms, and take all but the remainder of shrubs from player.

The event works fine as it is, but you often have more than 1 Balm's worth of shrubs, and I'd like to make those interactions less annoyingly clicky for the player.

I've played around with the various Control Variables options, but nothings working quite as I expect, so I thought I'd ask the experts how to accomplish this functionally.

Thanks!
Do a script command and put

Code:
let shrub = $dataItems[id of shrub];
let balm = $dataItems[id of balm];
let numShrubs = $gameParty.numItems(shrub);
let numBalms = Math.floor(numShrubs / 10);
let shrubsLost = numBalms * 10;
$gameParty.loseItem(shrub, shrubsLost);
$gameParty.gainItem(balm, numBalms);
Have you tried something like this:
1595945313058.png

Where Poison is your shrubs
and #0002 is your balms.
And the variable is whatever you want to track y our items.

edit: you'll need to also add a step to remove the current shrubs in between the first control variable and the second. or you can just use the Trihan's script above.
Thanks for the quick help @Trihan and @Rubescen !!
One followup question on this - how do I inject the ultimately determined number of balms into a text block? Akin to using \N[1] to show the player name, only in this case the value of a variable.

e.g. :

You have recieved [variable] Balms!
If you do $gameVariables.setValue(id of free variable, numBalms) into the end of my snippet you can do \v[x] in a message where x is the variable's ID.
If you do $gameVariables.setValue(id of free variable, numBalms) into the end of my snippet you can do \v[x] in a message where x is the variable's ID.

Ah! "\v[x]" is exactly what I was after.

It's working perfectly now, thanks again for the quick help!

Original thread: https://forums.rpgmakerweb.com/threads/doing-math-in-an-event.124632/