Checking and Comparing Item IDs in Player's Inventory

So, one of the 'features' for my game I am working on is that the player 'holds' items (they equip one item at a time from their inventory and a variable tells other objects what that item is). This works fine, but my issue comes in one of the ways to navigate the player's inventory. I am pretty new to RPGmaker as a whole, so I'm sure it's possible the answer is obvious and I've just missed it, but I need help with the following:

If the player presses Q or E (I have Yanfly's button common events plugin, along with the rest of Yanfly's set if that is relevant), they will go back and forward an item in their inventory, respectively. This is based on the ID of the items. So, if the player has items 1, 2, 4, 10, and 20, and they are holding item 4, then hitting Q will make them hold item 2, and hitting E will make them hold item 10. This system would also include a failsafe for if they already have the lowest or highest ID- if they hold item 20, E would send them to 1, and holding 1 and hitting Q would send them to 20. Coding-wise, once the nearest numbers are identified, they go to a previous item and next item variable respectively, and changing items will lead to another check to see which numbers are now closest, changing the variables accordingly. The part I am struggling with is finding a way to cross-compare the IDs and identify the two directly higher and lower than the current ID. If it is relavant, there would not need to be checks for weapons or items, only armors.


Any help would be appreciated!
Welcome!

It's awfully hard to sort through and offer any corrections on event logic that's dumped in a big block of text 😛

So while some post formatting with line breaks would greatly help, we would still ask you to show screenshots of how you have your event set up.

they equip one item at a time from their inventory and a variable tells other objects what that item is
I'm a little confused as to why you need a variable doing this when you can always just check the equipment slot.
Welcome!

It's awfully hard to sort through and offer any corrections on event logic that's dumped in a big block of text 😛

So while some post formatting with line breaks would greatly help, we would still ask you to show screenshots of how you have your event set up.


I'm a little confused as to why you need a variable doing this when you can always just check the equipment slot.
There are no screenshots because the system isn't made yet. That's what I'm asking for help with! I'm pretty sure I know how it SHOULD work in rpgmaker:

* When common event inventory update occurs (this being triggered by losing/gaining items or by hitting Q or E), get all items of actor 1 and make array accordingly
* Get value of equipped item variable (this is always the ID of the current item)
* If equipped ID-X (X being a second variable used for calculations)= another entry in array, set ID-X to previous item. Every time ID-X isn't part of the array, change X by -1.
--Repeat until either ID-X returns an entry in the array, or until ID-X= 0.
----If ID-X=0, then begin checking M (this being the third and final variable, which is the highest item ID in the database) -1 until an entry is found.
* Once a value is returned, set it to previous item.
* If ID+X= entry in the array, set ID+1 to next item. This is very similar to the previous check, just ascending.
----If ID+X > M, begin checking 0+X until an entry is found.
* Once a value is returned, set it to next item.

But I don't know how to do all that in MV, because I don't know javascript. I am trying to get a grasp of it, of course, but I am not quite there yet. Really all I need is a way to check and store the inventory (arrays confuse the heck out of me and is the part I'm really struggling to wrap my head around), and some help and/or a guide for beginners with how calculations and "if/then"s are done in java. I should be able to, and genuinely want to, figure out the rest for myself! I won't learn anything if I'm just outright handed every bit of code I need, after all ^^!


As for the variable, that's just because it's easier for me, personally. I prefer to work with variables when I can, because that is what I am used to from other coding systems. I suppose it is a bit redundant, but I like to have the spare variables on hand!
There are no screenshots because the system isn't made yet. That's what I'm asking for help with!
Okay. That is not at all clear from your first post. When you say all of that stuff and then conclude with:
The part I am struggling with is...
it implies that you do not need help with the rest of it and you already have the working framework.

I'm pretty sure I know how it SHOULD work in rpgmaker:
...
But I don't know how to do all that in MV, because I don't know javascript.
I really think you're over-complicating this by trying to shove it into code. This is perfectly doable with the basic common event commands built into the engine. I don't see any reason you need arrays or anything more than a few script calls (which are readily found via Google or the pinned post of MV script calls).

You get the ID of the currently held armor (we'll say that's your equipment slot 5) and put it into a variable (we'll say ID 3). If you already have that somehow else, as you implied in your first post, then use that...but I'll show this for demonstration's purposes.
Code:
$gameParty.leader().equips()[4].id

Then you just check for having the item ID higher or lower, depending on which direction you're going. When you find it, equip it. If you get back to where you started, stop.

The script call for Conditional Branch to see if the party has an armor ID is
Code:
$gameParty.numItems($dataArmors[ID])
We'll be replacing the ID with the contents of variable 4, which holds the ID we're looking for:
Code:
$gameVariables.value(4)

So those combined are:
Code:
$gameParty.numItems($dataArmors[$gameVariables.value(4)])

Once you find it, the script call to equip your party leader is (again presuming slot 5)
Code:
$gameParty.leader().changeEquip(4, $dataArmors[$gameVariables.value(4)])
I'm going off of your original post, presuming 20 is the highest ID before you go back to look at 1. If that's not the case, just change the value in the Conditional Branch.

So:
1720639764168.png


For previous item, obviously, you just make the variable subtract 1 instead of add 1, and if it hits 0 set it to 20.

Original thread: https://forums.rpgmakerweb.com/threads/checking-and-comparing-item-ids-in-players-inventory.170168/