MZ How to make Event subtract from a Variable once upon entering the Room?

So I'm trying to implement a hint system that tells the player if they missed any items in a room.

When you enter a room, a variable is set to the amount of items in the room. Each collected item lowers the variable by one and once the variable hits zero, consulting the hint system will tell you you got everything. In the debug room, this works.
So far, so good.


But now I'm seeing some complications with this moving forward.

I know I'll have to use that one variable for every room in the game. If there's more than one variable to check for, the hint system won't know which one is right and just display them all. That's why I made the system count down to zero.

This means that I need to set my variable to the correct number every time a room is entered, even if the player misses an item in a room, leaves and then comes back.


So what I was THINKING for this was that instead of having the initial action of picking up the item on Page 1 subtract from the variable, I could simply give every item that's been picked up already a command on Page 2 (aka the Self-Switch A ON-state after the item has been picked up) that subtracts from our variable, then ends the event.

This way, I can just have the warp zones leading into each room set the variable to the full amount for a room every time it's entered and have each picked up item subtract 1 until the amount is just right.

The problem is… I can't get this Page 2 eventing to activate on its own.

It DOES work just fine if I set the Trigger to Action Button, but it simply will not run at all if I set the Trigger as a Parallel event. The variable just stays as it is. (And Auto Run freezes the game, but I expected as much.)


So, how DO I run this? And if there's no way to run this, what's the alternative to achieving what I'm going for here?
If the item chest events or whatever are consecutive ids, you can loop through them to check which ones have self switch B ON, and then subtract that number from your variable. So when entering the room you run an event (autorun + erase event OR player touch depending on how your game/maps work), check event ids from X to Y, change the mapId, change the variable Z:

for (let i = X; i <= Y; i++) {
if ($gameSelfSwitches.value([mapId, i, 'B']))
$gameVariables.setValue(Z, $gameVariables.value(Z) - 1)
}

EDIT: And at the start of the event set the variable to the original value of the number of chests.
If you didn't want to use separate Variables for each room, I would suggest using a couple of Arrays and a couple of other Variables.

For instance, you could make one Array that holds the number of items in each room. You make another Array that holds the number of items found in each room.

As the player finds items, you increment your tracking Variable. When the player goes to check the Hint, you update the Array of the number of found items and compare the two values for that room. Total number against number found.

You would only need 2 Variables for the Arrays and then 2 Variables to be reused inside the room. One for total number of items in that room and one for total number found so far.

You just keep updating those 2 Variables from the values in the Array.

So, TotalArray = [5, 6, 10, 2, 5, 8] means that Room 1 has 5 items, Room 2 has 6 items, etc.

FoundArray = [3, 2, 0, 0, 0, 8] means that Room 1 still has 2 items to be found, Room 2 has 4 items to be found, etc.

When the player enters a room, you update the TotalItems Variable with the appropriate value from the TotalArray and update the FoundItems Variable with the appropriate value from the FoundArray.

At least, that is how I would try approaching it and it would only require 4 Variables. Unless I am completely misunderstanding what you are trying to do.

Good luck on your project!

Original thread: https://forums.rpgmakerweb.com/threads/how-to-make-event-subtract-from-a-variable-once-upon-entering-the-room.181242/