Is it possible to have a switch activate when X items are in the inventory?

Hello, I've been trying to use plugins to add a limited inventory to my game, but none are able to answer the question of what happens when an event gives you an item, which is a problem because all items in my game are received through events. So I was wondering, is it possible to have a switch activate when a certain amount of items are in the inventory? And when I say items, I mean individual items, not item slots, so even duplicate items would count towards the total.

So let's say the desired limit is 10. The switch would be off if the player has 9 or less items, but would be on if the player has 10 or more items. That way, whenever the player receives an item, a conditional branch could be used to prevent the acquisition of the item if they have 10 or more already, allowing the player to empty out their inventory and go back to the event to get the item.

I think this could be done with variables, though I'm not sure how, thank you.
Control Variables -> Game Data -> Weapon

Here is a quick mock-up for your event that adds the weapon:
1696904445980.png


You can do this for armor and items as well.
There are a number of ways to handle this.

It should be as trivial as one line of code to make any plugin that supports a limited inventory change event commands that add multiple items so you don't go over your limit. This does not, of course, help you with the part where you want the leftovers remaining in the chest, but it would prevent you from overflowing.

As far as that part goes, I don't see how a switch is useful, per se.

It makes more sense to me that your event checks how many of the item the party currently has, and if what you would make them pick up would go over the limit, the chest shuts again (as @TESTOSTERONE demonstrated).

You could get fancy/realistic by putting that into a loop, using a counter to say there are X of these items available and counting down as you check and add them to the party's inventory one at a time. Then, if the check fails because the party hit their limit, you store the remaining number of items in a variable on the event (this requires use of a self variables plugin).
Control Variables -> Game Data -> Weapon

Here is a quick mock-up for your event that adds the weapon:
View attachment 279651

You can do this for armor and items as well.
Thank you, but could this be done for number of all items, no matter what kind of item it is? I should also mention, the player cannot access the weapon, armor, or key item tab in my game, so that wouldn't be an issue.
It would help if you told us exactly which plugin you were using.

Any of them will have a way in their code to look at how items out of their max the party is carrying - that script call may even be given to you in the instructions. But you'd have to use a script call in the conditional branch.
There are a number of ways to handle this.

It should be as trivial as one line of code to make any plugin that supports a limited inventory change event commands that add multiple items so you don't go over your limit. This does not, of course, help you with the part where you want the leftovers remaining in the chest, but it would prevent you from overflowing.

As far as that part goes, I don't see how a switch is useful, per se.

It makes more sense to me that your event checks how many of the item the party currently has, and if what you would make them pick up would go over the limit, the chest shuts again (as @TESTOSTERONE demonstrated).

You could get fancy/realistic by putting that into a loop, using a counter to say there are X of these items available and counting down as you check and add them to the party's inventory one at a time. Then, if the check fails because the party hit their limit, you store the remaining number of items in a variable on the event (this requires use of a self variables plugin).
Ah, I wanted to use a switch as I wanted the game to check for ALL items the user has, not just one kind. Then The conditional branch could either prompt the player to not pick up the item, allowing them to remove some of their items, or even if I wanted to be evil, preventing the player from obtaining the item.
It would help if you told us exactly which plugin you were using.

Any of them will have a way in their code to look at how items out of their max the party is carrying - that script call may even be given to you in the instructions. But you'd have to use a script call in the conditional branch.
Oh, I'm not using any plugins now, I got rid of them, I didn't need any of their other features, I just wanted a switch functionality.
You can use script command:
Code:
var totalItems = 0;

for(var i = 0; i < $dataItems.length; i++){
totalItems += $gameParty.numItems($dataItems[i]);
}

$gameVariables.setValue(1,totalItems);

This will set the variable with ID 1 to the total items in the inventory.

See here for more.
You can use script command:
Code:
var totalItems = 0;

for(var i = 0; i < $dataItems.length; i++){
totalItems += $gameParty.numItems($dataItems[i]);
}

$gameVariables.setValue(1,totalItems);

This will set the variable with ID 1 to the total items in the inventory.

See here for more.
Thank you! Do I save this as a .js file and put it in as a plugin?
No, you use a script command as he said.

If you only want a switch, replace the bottom line of that code with
Code:
$gameSwitches.setValue(X, totalItems>Y);
where X is the ID of the switch and Y is the max number of items allowed.
You use this:
1696905882664.png
I'm sorry I don't know how these things work still )=
Would I put in this script in every event I use it in?
Correct, as @TESTOSTERONE said it sets a variable (or toggles a switch with what I gave you in my last post).

Therefore, just like every other time you set a variable or switch in your game, you need to run it every time you want to use it.

Or you could make a parallel common event, put a Wait of a few frames, then this script call so it's running constantly, but that seems wasteful for how infrequently it matters.
Correct, as @TESTOSTERONE said it sets a variable (or toggles a switch with what I gave you in my last post).

Therefore, just like every other time you set a variable or switch in your game, you need to run it every time you want to use it.
Ah got it, sorry.
Correct, as @TESTOSTERONE said it sets a variable (or toggles a switch with what I gave you in my last post).

Therefore, just like every other time you set a variable or switch in your game, you need to run it every time you want to use it.

Or you could make a parallel common event, put a Wait of a few frames, then this script call so it's running constantly, but that seems wasteful for how infrequently it matters.
Thank you! The script works perfectly... As long as you don't have 10 of one item but that shouldn't ever happen, you get an item when you start up the game.

Edit: Whoops I set the max items to 10 instead of 9, it works perfectly, Thanks to both of you!
@Misk-ivus , please avoid double posting, as it is against the forum rules. You can use the "Edit" function on your posts to add additional information you've forgotten or respond to multiple people. You can review our forum rules here. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/is-it-possible-to-have-a-switch-activate-when-x-items-are-in-the-inventory.161874/